@kruntime/komputer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (353) hide show
  1. package/README.md +368 -0
  2. package/dist/agent/approvals.d.ts +28 -0
  3. package/dist/agent/approvals.js +109 -0
  4. package/dist/agent/approvals.js.map +1 -0
  5. package/dist/agent/messages.d.ts +67 -0
  6. package/dist/agent/messages.js +213 -0
  7. package/dist/agent/messages.js.map +1 -0
  8. package/dist/agent/recovery.d.ts +13 -0
  9. package/dist/agent/recovery.js +112 -0
  10. package/dist/agent/recovery.js.map +1 -0
  11. package/dist/agent/session-utils.d.ts +17 -0
  12. package/dist/agent/session-utils.js +77 -0
  13. package/dist/agent/session-utils.js.map +1 -0
  14. package/dist/agent-session.d.ts +303 -0
  15. package/dist/agent-session.js +3031 -0
  16. package/dist/agent-session.js.map +1 -0
  17. package/dist/authority.d.ts +34 -0
  18. package/dist/authority.js +105 -0
  19. package/dist/authority.js.map +1 -0
  20. package/dist/content.d.ts +17 -0
  21. package/dist/content.js +69 -0
  22. package/dist/content.js.map +1 -0
  23. package/dist/cron.d.ts +20 -0
  24. package/dist/cron.js +25 -0
  25. package/dist/cron.js.map +1 -0
  26. package/dist/drive.d.ts +198 -0
  27. package/dist/drive.js +656 -0
  28. package/dist/drive.js.map +1 -0
  29. package/dist/events.d.ts +20 -0
  30. package/dist/events.js +149 -0
  31. package/dist/events.js.map +1 -0
  32. package/dist/fs-mode.d.ts +9 -0
  33. package/dist/fs-mode.js +98 -0
  34. package/dist/fs-mode.js.map +1 -0
  35. package/dist/index.d.ts +26 -0
  36. package/dist/index.js +15 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/komputer.d.ts +123 -0
  39. package/dist/komputer.js +733 -0
  40. package/dist/komputer.js.map +1 -0
  41. package/dist/kstate.d.ts +107 -0
  42. package/dist/kstate.js +529 -0
  43. package/dist/kstate.js.map +1 -0
  44. package/dist/model.d.ts +81 -0
  45. package/dist/model.js +824 -0
  46. package/dist/model.js.map +1 -0
  47. package/dist/node.d.ts +2 -0
  48. package/dist/node.js +3 -0
  49. package/dist/node.js.map +1 -0
  50. package/dist/path.d.ts +4 -0
  51. package/dist/path.js +38 -0
  52. package/dist/path.js.map +1 -0
  53. package/dist/pool.d.ts +56 -0
  54. package/dist/pool.js +172 -0
  55. package/dist/pool.js.map +1 -0
  56. package/dist/receipts.d.ts +44 -0
  57. package/dist/receipts.js +35 -0
  58. package/dist/receipts.js.map +1 -0
  59. package/dist/remote-node.d.ts +2 -0
  60. package/dist/remote-node.js +122 -0
  61. package/dist/remote-node.js.map +1 -0
  62. package/dist/remote.d.ts +84 -0
  63. package/dist/remote.js +11 -0
  64. package/dist/remote.js.map +1 -0
  65. package/dist/sandbox.d.ts +4 -0
  66. package/dist/sandbox.js +70 -0
  67. package/dist/sandbox.js.map +1 -0
  68. package/dist/shell/builtins/catalog.d.ts +6 -0
  69. package/dist/shell/builtins/catalog.js +12 -0
  70. package/dist/shell/builtins/catalog.js.map +1 -0
  71. package/dist/shell/builtins/commands/filesystem/basename.d.ts +2 -0
  72. package/dist/shell/builtins/commands/filesystem/basename.js +23 -0
  73. package/dist/shell/builtins/commands/filesystem/basename.js.map +1 -0
  74. package/dist/shell/builtins/commands/filesystem/cat.d.ts +2 -0
  75. package/dist/shell/builtins/commands/filesystem/cat.js +85 -0
  76. package/dist/shell/builtins/commands/filesystem/cat.js.map +1 -0
  77. package/dist/shell/builtins/commands/filesystem/cd.d.ts +2 -0
  78. package/dist/shell/builtins/commands/filesystem/cd.js +14 -0
  79. package/dist/shell/builtins/commands/filesystem/cd.js.map +1 -0
  80. package/dist/shell/builtins/commands/filesystem/chmod.d.ts +2 -0
  81. package/dist/shell/builtins/commands/filesystem/chmod.js +46 -0
  82. package/dist/shell/builtins/commands/filesystem/chmod.js.map +1 -0
  83. package/dist/shell/builtins/commands/filesystem/copy-directory.d.ts +4 -0
  84. package/dist/shell/builtins/commands/filesystem/copy-directory.js +48 -0
  85. package/dist/shell/builtins/commands/filesystem/copy-directory.js.map +1 -0
  86. package/dist/shell/builtins/commands/filesystem/cp.d.ts +2 -0
  87. package/dist/shell/builtins/commands/filesystem/cp.js +92 -0
  88. package/dist/shell/builtins/commands/filesystem/cp.js.map +1 -0
  89. package/dist/shell/builtins/commands/filesystem/dirname.d.ts +2 -0
  90. package/dist/shell/builtins/commands/filesystem/dirname.js +23 -0
  91. package/dist/shell/builtins/commands/filesystem/dirname.js.map +1 -0
  92. package/dist/shell/builtins/commands/filesystem/du.d.ts +2 -0
  93. package/dist/shell/builtins/commands/filesystem/du.js +93 -0
  94. package/dist/shell/builtins/commands/filesystem/du.js.map +1 -0
  95. package/dist/shell/builtins/commands/filesystem/find.d.ts +2 -0
  96. package/dist/shell/builtins/commands/filesystem/find.js +111 -0
  97. package/dist/shell/builtins/commands/filesystem/find.js.map +1 -0
  98. package/dist/shell/builtins/commands/filesystem/install.d.ts +2 -0
  99. package/dist/shell/builtins/commands/filesystem/install.js +89 -0
  100. package/dist/shell/builtins/commands/filesystem/install.js.map +1 -0
  101. package/dist/shell/builtins/commands/filesystem/ln.d.ts +2 -0
  102. package/dist/shell/builtins/commands/filesystem/ln.js +57 -0
  103. package/dist/shell/builtins/commands/filesystem/ln.js.map +1 -0
  104. package/dist/shell/builtins/commands/filesystem/ls.d.ts +2 -0
  105. package/dist/shell/builtins/commands/filesystem/ls.js +153 -0
  106. package/dist/shell/builtins/commands/filesystem/ls.js.map +1 -0
  107. package/dist/shell/builtins/commands/filesystem/mkdir.d.ts +2 -0
  108. package/dist/shell/builtins/commands/filesystem/mkdir.js +41 -0
  109. package/dist/shell/builtins/commands/filesystem/mkdir.js.map +1 -0
  110. package/dist/shell/builtins/commands/filesystem/mktemp.d.ts +2 -0
  111. package/dist/shell/builtins/commands/filesystem/mktemp.js +94 -0
  112. package/dist/shell/builtins/commands/filesystem/mktemp.js.map +1 -0
  113. package/dist/shell/builtins/commands/filesystem/mv.d.ts +2 -0
  114. package/dist/shell/builtins/commands/filesystem/mv.js +41 -0
  115. package/dist/shell/builtins/commands/filesystem/mv.js.map +1 -0
  116. package/dist/shell/builtins/commands/filesystem/pwd.d.ts +2 -0
  117. package/dist/shell/builtins/commands/filesystem/pwd.js +2 -0
  118. package/dist/shell/builtins/commands/filesystem/pwd.js.map +1 -0
  119. package/dist/shell/builtins/commands/filesystem/realpath.d.ts +3 -0
  120. package/dist/shell/builtins/commands/filesystem/realpath.js +61 -0
  121. package/dist/shell/builtins/commands/filesystem/realpath.js.map +1 -0
  122. package/dist/shell/builtins/commands/filesystem/rm.d.ts +2 -0
  123. package/dist/shell/builtins/commands/filesystem/rm.js +38 -0
  124. package/dist/shell/builtins/commands/filesystem/rm.js.map +1 -0
  125. package/dist/shell/builtins/commands/filesystem/stat.d.ts +2 -0
  126. package/dist/shell/builtins/commands/filesystem/stat.js +141 -0
  127. package/dist/shell/builtins/commands/filesystem/stat.js.map +1 -0
  128. package/dist/shell/builtins/commands/filesystem/touch.d.ts +2 -0
  129. package/dist/shell/builtins/commands/filesystem/touch.js +143 -0
  130. package/dist/shell/builtins/commands/filesystem/touch.js.map +1 -0
  131. package/dist/shell/builtins/commands/filesystem.d.ts +2 -0
  132. package/dist/shell/builtins/commands/filesystem.js +44 -0
  133. package/dist/shell/builtins/commands/filesystem.js.map +1 -0
  134. package/dist/shell/builtins/commands/network/curl.d.ts +2 -0
  135. package/dist/shell/builtins/commands/network/curl.js +25 -0
  136. package/dist/shell/builtins/commands/network/curl.js.map +1 -0
  137. package/dist/shell/builtins/commands/network/http.d.ts +27 -0
  138. package/dist/shell/builtins/commands/network/http.js +226 -0
  139. package/dist/shell/builtins/commands/network/http.js.map +1 -0
  140. package/dist/shell/builtins/commands/network/wget.d.ts +2 -0
  141. package/dist/shell/builtins/commands/network/wget.js +32 -0
  142. package/dist/shell/builtins/commands/network/wget.js.map +1 -0
  143. package/dist/shell/builtins/commands/network.d.ts +2 -0
  144. package/dist/shell/builtins/commands/network.js +9 -0
  145. package/dist/shell/builtins/commands/network.js.map +1 -0
  146. package/dist/shell/builtins/commands/process/bg.d.ts +2 -0
  147. package/dist/shell/builtins/commands/process/bg.js +10 -0
  148. package/dist/shell/builtins/commands/process/bg.js.map +1 -0
  149. package/dist/shell/builtins/commands/process/fg.d.ts +2 -0
  150. package/dist/shell/builtins/commands/process/fg.js +14 -0
  151. package/dist/shell/builtins/commands/process/fg.js.map +1 -0
  152. package/dist/shell/builtins/commands/process/jobs.d.ts +2 -0
  153. package/dist/shell/builtins/commands/process/jobs.js +8 -0
  154. package/dist/shell/builtins/commands/process/jobs.js.map +1 -0
  155. package/dist/shell/builtins/commands/process/jobspec.d.ts +4 -0
  156. package/dist/shell/builtins/commands/process/jobspec.js +20 -0
  157. package/dist/shell/builtins/commands/process/jobspec.js.map +1 -0
  158. package/dist/shell/builtins/commands/process/kill.d.ts +2 -0
  159. package/dist/shell/builtins/commands/process/kill.js +35 -0
  160. package/dist/shell/builtins/commands/process/kill.js.map +1 -0
  161. package/dist/shell/builtins/commands/process/ps.d.ts +2 -0
  162. package/dist/shell/builtins/commands/process/ps.js +6 -0
  163. package/dist/shell/builtins/commands/process/ps.js.map +1 -0
  164. package/dist/shell/builtins/commands/process/sleep.d.ts +2 -0
  165. package/dist/shell/builtins/commands/process/sleep.js +6 -0
  166. package/dist/shell/builtins/commands/process/sleep.js.map +1 -0
  167. package/dist/shell/builtins/commands/process/wait.d.ts +2 -0
  168. package/dist/shell/builtins/commands/process/wait.js +21 -0
  169. package/dist/shell/builtins/commands/process/wait.js.map +1 -0
  170. package/dist/shell/builtins/commands/process.d.ts +2 -0
  171. package/dist/shell/builtins/commands/process.js +19 -0
  172. package/dist/shell/builtins/commands/process.js.map +1 -0
  173. package/dist/shell/builtins/commands/system/command.d.ts +2 -0
  174. package/dist/shell/builtins/commands/system/command.js +42 -0
  175. package/dist/shell/builtins/commands/system/command.js.map +1 -0
  176. package/dist/shell/builtins/commands/system/date.d.ts +2 -0
  177. package/dist/shell/builtins/commands/system/date.js +121 -0
  178. package/dist/shell/builtins/commands/system/date.js.map +1 -0
  179. package/dist/shell/builtins/commands/system/env.d.ts +2 -0
  180. package/dist/shell/builtins/commands/system/env.js +84 -0
  181. package/dist/shell/builtins/commands/system/env.js.map +1 -0
  182. package/dist/shell/builtins/commands/system/export.d.ts +2 -0
  183. package/dist/shell/builtins/commands/system/export.js +18 -0
  184. package/dist/shell/builtins/commands/system/export.js.map +1 -0
  185. package/dist/shell/builtins/commands/system/false.d.ts +2 -0
  186. package/dist/shell/builtins/commands/system/false.js +4 -0
  187. package/dist/shell/builtins/commands/system/false.js.map +1 -0
  188. package/dist/shell/builtins/commands/system/help.d.ts +2 -0
  189. package/dist/shell/builtins/commands/system/help.js +38 -0
  190. package/dist/shell/builtins/commands/system/help.js.map +1 -0
  191. package/dist/shell/builtins/commands/system/hostname.d.ts +2 -0
  192. package/dist/shell/builtins/commands/system/hostname.js +6 -0
  193. package/dist/shell/builtins/commands/system/hostname.js.map +1 -0
  194. package/dist/shell/builtins/commands/system/id.d.ts +2 -0
  195. package/dist/shell/builtins/commands/system/id.js +55 -0
  196. package/dist/shell/builtins/commands/system/id.js.map +1 -0
  197. package/dist/shell/builtins/commands/system/printenv.d.ts +2 -0
  198. package/dist/shell/builtins/commands/system/printenv.js +22 -0
  199. package/dist/shell/builtins/commands/system/printenv.js.map +1 -0
  200. package/dist/shell/builtins/commands/system/read.d.ts +2 -0
  201. package/dist/shell/builtins/commands/system/read.js +75 -0
  202. package/dist/shell/builtins/commands/system/read.js.map +1 -0
  203. package/dist/shell/builtins/commands/system/resolve-command.d.ts +3 -0
  204. package/dist/shell/builtins/commands/system/resolve-command.js +34 -0
  205. package/dist/shell/builtins/commands/system/resolve-command.js.map +1 -0
  206. package/dist/shell/builtins/commands/system/set.d.ts +2 -0
  207. package/dist/shell/builtins/commands/system/set.js +49 -0
  208. package/dist/shell/builtins/commands/system/set.js.map +1 -0
  209. package/dist/shell/builtins/commands/system/sh.d.ts +2 -0
  210. package/dist/shell/builtins/commands/system/sh.js +55 -0
  211. package/dist/shell/builtins/commands/system/sh.js.map +1 -0
  212. package/dist/shell/builtins/commands/system/source.d.ts +2 -0
  213. package/dist/shell/builtins/commands/system/source.js +42 -0
  214. package/dist/shell/builtins/commands/system/source.js.map +1 -0
  215. package/dist/shell/builtins/commands/system/test-expression.d.ts +2 -0
  216. package/dist/shell/builtins/commands/system/test-expression.js +93 -0
  217. package/dist/shell/builtins/commands/system/test-expression.js.map +1 -0
  218. package/dist/shell/builtins/commands/system/test.d.ts +4 -0
  219. package/dist/shell/builtins/commands/system/test.js +19 -0
  220. package/dist/shell/builtins/commands/system/test.js.map +1 -0
  221. package/dist/shell/builtins/commands/system/true.d.ts +2 -0
  222. package/dist/shell/builtins/commands/system/true.js +2 -0
  223. package/dist/shell/builtins/commands/system/true.js.map +1 -0
  224. package/dist/shell/builtins/commands/system/type.d.ts +2 -0
  225. package/dist/shell/builtins/commands/system/type.js +77 -0
  226. package/dist/shell/builtins/commands/system/type.js.map +1 -0
  227. package/dist/shell/builtins/commands/system/uname.d.ts +2 -0
  228. package/dist/shell/builtins/commands/system/uname.js +39 -0
  229. package/dist/shell/builtins/commands/system/uname.js.map +1 -0
  230. package/dist/shell/builtins/commands/system/unset.d.ts +2 -0
  231. package/dist/shell/builtins/commands/system/unset.js +9 -0
  232. package/dist/shell/builtins/commands/system/unset.js.map +1 -0
  233. package/dist/shell/builtins/commands/system/which.d.ts +2 -0
  234. package/dist/shell/builtins/commands/system/which.js +38 -0
  235. package/dist/shell/builtins/commands/system/which.js.map +1 -0
  236. package/dist/shell/builtins/commands/system/whoami.d.ts +2 -0
  237. package/dist/shell/builtins/commands/system/whoami.js +6 -0
  238. package/dist/shell/builtins/commands/system/whoami.js.map +1 -0
  239. package/dist/shell/builtins/commands/system.d.ts +2 -0
  240. package/dist/shell/builtins/commands/system.js +49 -0
  241. package/dist/shell/builtins/commands/system.js.map +1 -0
  242. package/dist/shell/builtins/commands/text/awk.d.ts +2 -0
  243. package/dist/shell/builtins/commands/text/awk.js +8 -0
  244. package/dist/shell/builtins/commands/text/awk.js.map +1 -0
  245. package/dist/shell/builtins/commands/text/base64.d.ts +2 -0
  246. package/dist/shell/builtins/commands/text/base64.js +69 -0
  247. package/dist/shell/builtins/commands/text/base64.js.map +1 -0
  248. package/dist/shell/builtins/commands/text/cut.d.ts +2 -0
  249. package/dist/shell/builtins/commands/text/cut.js +35 -0
  250. package/dist/shell/builtins/commands/text/cut.js.map +1 -0
  251. package/dist/shell/builtins/commands/text/diff.d.ts +2 -0
  252. package/dist/shell/builtins/commands/text/diff.js +83 -0
  253. package/dist/shell/builtins/commands/text/diff.js.map +1 -0
  254. package/dist/shell/builtins/commands/text/echo.d.ts +2 -0
  255. package/dist/shell/builtins/commands/text/echo.js +6 -0
  256. package/dist/shell/builtins/commands/text/echo.js.map +1 -0
  257. package/dist/shell/builtins/commands/text/grep.d.ts +2 -0
  258. package/dist/shell/builtins/commands/text/grep.js +123 -0
  259. package/dist/shell/builtins/commands/text/grep.js.map +1 -0
  260. package/dist/shell/builtins/commands/text/head.d.ts +2 -0
  261. package/dist/shell/builtins/commands/text/head.js +8 -0
  262. package/dist/shell/builtins/commands/text/head.js.map +1 -0
  263. package/dist/shell/builtins/commands/text/jq.d.ts +2 -0
  264. package/dist/shell/builtins/commands/text/jq.js +239 -0
  265. package/dist/shell/builtins/commands/text/jq.js.map +1 -0
  266. package/dist/shell/builtins/commands/text/printf.d.ts +2 -0
  267. package/dist/shell/builtins/commands/text/printf.js +3 -0
  268. package/dist/shell/builtins/commands/text/printf.js.map +1 -0
  269. package/dist/shell/builtins/commands/text/sed.d.ts +2 -0
  270. package/dist/shell/builtins/commands/text/sed.js +21 -0
  271. package/dist/shell/builtins/commands/text/sed.js.map +1 -0
  272. package/dist/shell/builtins/commands/text/sha256sum.d.ts +2 -0
  273. package/dist/shell/builtins/commands/text/sha256sum.js +63 -0
  274. package/dist/shell/builtins/commands/text/sha256sum.js.map +1 -0
  275. package/dist/shell/builtins/commands/text/sort.d.ts +2 -0
  276. package/dist/shell/builtins/commands/text/sort.js +134 -0
  277. package/dist/shell/builtins/commands/text/sort.js.map +1 -0
  278. package/dist/shell/builtins/commands/text/tail.d.ts +2 -0
  279. package/dist/shell/builtins/commands/text/tail.js +10 -0
  280. package/dist/shell/builtins/commands/text/tail.js.map +1 -0
  281. package/dist/shell/builtins/commands/text/tee.d.ts +2 -0
  282. package/dist/shell/builtins/commands/text/tee.js +29 -0
  283. package/dist/shell/builtins/commands/text/tee.js.map +1 -0
  284. package/dist/shell/builtins/commands/text/tr.d.ts +2 -0
  285. package/dist/shell/builtins/commands/text/tr.js +3 -0
  286. package/dist/shell/builtins/commands/text/tr.js.map +1 -0
  287. package/dist/shell/builtins/commands/text/uniq.d.ts +2 -0
  288. package/dist/shell/builtins/commands/text/uniq.js +8 -0
  289. package/dist/shell/builtins/commands/text/uniq.js.map +1 -0
  290. package/dist/shell/builtins/commands/text/wc.d.ts +2 -0
  291. package/dist/shell/builtins/commands/text/wc.js +30 -0
  292. package/dist/shell/builtins/commands/text/wc.js.map +1 -0
  293. package/dist/shell/builtins/commands/text/xargs.d.ts +2 -0
  294. package/dist/shell/builtins/commands/text/xargs.js +138 -0
  295. package/dist/shell/builtins/commands/text/xargs.js.map +1 -0
  296. package/dist/shell/builtins/commands/text.d.ts +2 -0
  297. package/dist/shell/builtins/commands/text.js +41 -0
  298. package/dist/shell/builtins/commands/text.js.map +1 -0
  299. package/dist/shell/builtins/help-text.d.ts +69 -0
  300. package/dist/shell/builtins/help-text.js +381 -0
  301. package/dist/shell/builtins/help-text.js.map +1 -0
  302. package/dist/shell/builtins/path.d.ts +28 -0
  303. package/dist/shell/builtins/path.js +184 -0
  304. package/dist/shell/builtins/path.js.map +1 -0
  305. package/dist/shell/builtins/registry.d.ts +3 -0
  306. package/dist/shell/builtins/registry.js +75 -0
  307. package/dist/shell/builtins/registry.js.map +1 -0
  308. package/dist/shell/builtins/run.d.ts +4 -0
  309. package/dist/shell/builtins/run.js +29 -0
  310. package/dist/shell/builtins/run.js.map +1 -0
  311. package/dist/shell/builtins/runtime.d.ts +83 -0
  312. package/dist/shell/builtins/runtime.js +46 -0
  313. package/dist/shell/builtins/runtime.js.map +1 -0
  314. package/dist/shell/builtins/text.d.ts +90 -0
  315. package/dist/shell/builtins/text.js +970 -0
  316. package/dist/shell/builtins/text.js.map +1 -0
  317. package/dist/shell/control.d.ts +31 -0
  318. package/dist/shell/control.js +232 -0
  319. package/dist/shell/control.js.map +1 -0
  320. package/dist/shell/expansion.d.ts +30 -0
  321. package/dist/shell/expansion.js +514 -0
  322. package/dist/shell/expansion.js.map +1 -0
  323. package/dist/shell/functions.d.ts +10 -0
  324. package/dist/shell/functions.js +108 -0
  325. package/dist/shell/functions.js.map +1 -0
  326. package/dist/shell/runtime.d.ts +44 -0
  327. package/dist/shell/runtime.js +276 -0
  328. package/dist/shell/runtime.js.map +1 -0
  329. package/dist/shell/time.d.ts +1 -0
  330. package/dist/shell/time.js +68 -0
  331. package/dist/shell/time.js.map +1 -0
  332. package/dist/state-backend.d.ts +61 -0
  333. package/dist/state-backend.js +275 -0
  334. package/dist/state-backend.js.map +1 -0
  335. package/dist/state-node.d.ts +2 -0
  336. package/dist/state-node.js +266 -0
  337. package/dist/state-node.js.map +1 -0
  338. package/dist/types.d.ts +462 -0
  339. package/dist/types.js +2 -0
  340. package/dist/types.js.map +1 -0
  341. package/dist/utils.d.ts +5 -0
  342. package/dist/utils.js +117 -0
  343. package/dist/utils.js.map +1 -0
  344. package/dist/vbash.d.ts +40 -0
  345. package/dist/vbash.js +389 -0
  346. package/dist/vbash.js.map +1 -0
  347. package/dist/vfs.d.ts +64 -0
  348. package/dist/vfs.js +248 -0
  349. package/dist/vfs.js.map +1 -0
  350. package/dist/vprocess.d.ts +139 -0
  351. package/dist/vprocess.js +756 -0
  352. package/dist/vprocess.js.map +1 -0
  353. package/package.json +28 -0
@@ -0,0 +1,970 @@
1
+ export function formatPrintf(args) {
2
+ if (!args.length)
3
+ return '';
4
+ const format = decodePrintfEscapes(args[0]);
5
+ const values = args.slice(1);
6
+ const conversions = printfConversions(format);
7
+ if (!conversions.length)
8
+ return format;
9
+ let valueIndex = 0;
10
+ let output = '';
11
+ do {
12
+ output += format.replace(/%%|%([-+ 0#]*)(\d+)?(?:\.(\d+))?([sd])/g, (match, flags, width, precision, type) => {
13
+ if (match === '%%')
14
+ return '%';
15
+ const raw = values[valueIndex++] ?? (type === 'd' ? '0' : '');
16
+ const rendered = type === 'd' ? formatInteger(raw) : String(raw);
17
+ return applyPrintfWidth(rendered, {
18
+ left: flags.includes('-'),
19
+ zero: flags.includes('0') && !flags.includes('-'),
20
+ width: width ? Number(width) : 0,
21
+ precision: precision ? Number(precision) : undefined,
22
+ });
23
+ });
24
+ } while (valueIndex < values.length);
25
+ return output;
26
+ }
27
+ function decodePrintfEscapes(format) {
28
+ return format
29
+ .replace(/\\n/g, '\n')
30
+ .replace(/\\t/g, '\t')
31
+ .replace(/\\r/g, '\r')
32
+ .replace(/\\\\/g, '\\');
33
+ }
34
+ function printfConversions(format) {
35
+ return [...format.matchAll(/%%|%[-+ 0#]*\d*(?:\.\d+)?[sd]/g)]
36
+ .filter((match) => match[0] !== '%%')
37
+ .map((match) => match[0]);
38
+ }
39
+ function formatInteger(value) {
40
+ const parsed = Number.parseInt(String(value), 10);
41
+ return Number.isFinite(parsed) ? String(parsed) : '0';
42
+ }
43
+ function applyPrintfWidth(value, options) {
44
+ const truncated = options.precision === undefined ? value : value.slice(0, options.precision);
45
+ if (!options.width || truncated.length >= options.width)
46
+ return truncated;
47
+ const pad = (options.zero ? '0' : ' ').repeat(options.width - truncated.length);
48
+ return options.left ? `${truncated}${pad}` : `${pad}${truncated}`;
49
+ }
50
+ export function parseGrepArgs(args) {
51
+ const patterns = [];
52
+ let ignoreCase = false;
53
+ let invert = false;
54
+ let lineNumber = false;
55
+ let recursive = false;
56
+ let count = false;
57
+ let filesWithMatches = false;
58
+ let quiet = false;
59
+ let fixedStrings = false;
60
+ let onlyMatching = false;
61
+ let noFilename = false;
62
+ let withFilename = false;
63
+ let maxCount;
64
+ let beforeContext = 0;
65
+ let afterContext = 0;
66
+ const includes = [];
67
+ const excludes = [];
68
+ const excludeDirs = [];
69
+ const rest = [];
70
+ for (let index = 0; index < args.length; index += 1) {
71
+ const arg = args[index];
72
+ if (arg === '--') {
73
+ rest.push(...args.slice(index + 1));
74
+ break;
75
+ }
76
+ if (arg === '--count') {
77
+ count = true;
78
+ continue;
79
+ }
80
+ if (arg === '--files-with-matches') {
81
+ filesWithMatches = true;
82
+ continue;
83
+ }
84
+ if (arg === '--quiet' || arg === '--silent') {
85
+ quiet = true;
86
+ continue;
87
+ }
88
+ if (arg === '--only-matching') {
89
+ onlyMatching = true;
90
+ continue;
91
+ }
92
+ if (arg === '--no-filename') {
93
+ noFilename = true;
94
+ continue;
95
+ }
96
+ if (arg === '--with-filename') {
97
+ withFilename = true;
98
+ continue;
99
+ }
100
+ if (arg === '--include') {
101
+ const value = args[++index];
102
+ if (value === undefined)
103
+ throw new Error('grep: option requires an argument: --include');
104
+ includes.push(value);
105
+ continue;
106
+ }
107
+ if (arg.startsWith('--include=')) {
108
+ includes.push(arg.slice('--include='.length));
109
+ continue;
110
+ }
111
+ if (arg === '--exclude') {
112
+ const value = args[++index];
113
+ if (value === undefined)
114
+ throw new Error('grep: option requires an argument: --exclude');
115
+ excludes.push(value);
116
+ continue;
117
+ }
118
+ if (arg.startsWith('--exclude=')) {
119
+ excludes.push(arg.slice('--exclude='.length));
120
+ continue;
121
+ }
122
+ if (arg === '--exclude-dir') {
123
+ const value = args[++index];
124
+ if (value === undefined)
125
+ throw new Error('grep: option requires an argument: --exclude-dir');
126
+ excludeDirs.push(value);
127
+ continue;
128
+ }
129
+ if (arg.startsWith('--exclude-dir=')) {
130
+ excludeDirs.push(arg.slice('--exclude-dir='.length));
131
+ continue;
132
+ }
133
+ if (arg === '--fixed-strings') {
134
+ fixedStrings = true;
135
+ continue;
136
+ }
137
+ if (arg === '--extended-regexp') {
138
+ fixedStrings = false;
139
+ continue;
140
+ }
141
+ if (arg === '--max-count') {
142
+ maxCount = parseNonnegativeCount(args[++index], 'grep: option requires an argument: --max-count');
143
+ continue;
144
+ }
145
+ if (arg.startsWith('--max-count=')) {
146
+ maxCount = parseNonnegativeCount(arg.slice('--max-count='.length), 'grep: invalid max count');
147
+ continue;
148
+ }
149
+ if (arg === '--after-context') {
150
+ afterContext = parseNonnegativeCount(args[++index], 'grep: option requires an argument: --after-context');
151
+ continue;
152
+ }
153
+ if (arg.startsWith('--after-context=')) {
154
+ afterContext = parseNonnegativeCount(arg.slice('--after-context='.length), 'grep: invalid context count');
155
+ continue;
156
+ }
157
+ if (arg === '--before-context') {
158
+ beforeContext = parseNonnegativeCount(args[++index], 'grep: option requires an argument: --before-context');
159
+ continue;
160
+ }
161
+ if (arg.startsWith('--before-context=')) {
162
+ beforeContext = parseNonnegativeCount(arg.slice('--before-context='.length), 'grep: invalid context count');
163
+ continue;
164
+ }
165
+ if (arg === '--context') {
166
+ const context = parseNonnegativeCount(args[++index], 'grep: option requires an argument: --context');
167
+ beforeContext = context;
168
+ afterContext = context;
169
+ continue;
170
+ }
171
+ if (arg.startsWith('--context=')) {
172
+ const context = parseNonnegativeCount(arg.slice('--context='.length), 'grep: invalid context count');
173
+ beforeContext = context;
174
+ afterContext = context;
175
+ continue;
176
+ }
177
+ if (arg.startsWith('--'))
178
+ throw new Error(`grep: unsupported option: ${arg}`);
179
+ if (arg === '-e') {
180
+ const pattern = args[++index];
181
+ if (pattern === undefined)
182
+ throw new Error('grep: option requires an argument: -e');
183
+ patterns.push(pattern);
184
+ continue;
185
+ }
186
+ if (arg.startsWith('-e') && arg.length > 2) {
187
+ patterns.push(arg.slice(2));
188
+ continue;
189
+ }
190
+ if (arg === '-m') {
191
+ maxCount = parseNonnegativeCount(args[++index], 'grep: option requires an argument: -m');
192
+ continue;
193
+ }
194
+ if (arg.startsWith('-m') && arg.length > 2) {
195
+ maxCount = parseNonnegativeCount(arg.slice(2), 'grep: invalid max count');
196
+ continue;
197
+ }
198
+ if (arg === '-A') {
199
+ afterContext = parseNonnegativeCount(args[++index], 'grep: option requires an argument: -A');
200
+ continue;
201
+ }
202
+ if (arg.startsWith('-A') && arg.length > 2) {
203
+ afterContext = parseNonnegativeCount(arg.slice(2), 'grep: invalid context count');
204
+ continue;
205
+ }
206
+ if (arg === '-B') {
207
+ beforeContext = parseNonnegativeCount(args[++index], 'grep: option requires an argument: -B');
208
+ continue;
209
+ }
210
+ if (arg.startsWith('-B') && arg.length > 2) {
211
+ beforeContext = parseNonnegativeCount(arg.slice(2), 'grep: invalid context count');
212
+ continue;
213
+ }
214
+ if (arg === '-C') {
215
+ const context = parseNonnegativeCount(args[++index], 'grep: option requires an argument: -C');
216
+ beforeContext = context;
217
+ afterContext = context;
218
+ continue;
219
+ }
220
+ if (arg.startsWith('-C') && arg.length > 2) {
221
+ const context = parseNonnegativeCount(arg.slice(2), 'grep: invalid context count');
222
+ beforeContext = context;
223
+ afterContext = context;
224
+ continue;
225
+ }
226
+ if (arg.startsWith('-') && arg.length > 1) {
227
+ for (const flag of arg.slice(1)) {
228
+ if (flag === 'i')
229
+ ignoreCase = true;
230
+ else if (flag === 'v')
231
+ invert = true;
232
+ else if (flag === 'n')
233
+ lineNumber = true;
234
+ else if (flag === 'r' || flag === 'R')
235
+ recursive = true;
236
+ else if (flag === 'c')
237
+ count = true;
238
+ else if (flag === 'l')
239
+ filesWithMatches = true;
240
+ else if (flag === 'q')
241
+ quiet = true;
242
+ else if (flag === 'o')
243
+ onlyMatching = true;
244
+ else if (flag === 'h')
245
+ noFilename = true;
246
+ else if (flag === 'H')
247
+ withFilename = true;
248
+ else if (flag === 'F')
249
+ fixedStrings = true;
250
+ else if (flag === 'E')
251
+ fixedStrings = false;
252
+ else
253
+ throw new Error(`grep: unsupported option: -${flag}`);
254
+ }
255
+ continue;
256
+ }
257
+ rest.push(arg);
258
+ }
259
+ if (!patterns.length) {
260
+ const pattern = rest.shift();
261
+ if (pattern !== undefined)
262
+ patterns.push(pattern);
263
+ }
264
+ if (!patterns.length)
265
+ throw new Error('grep requires a pattern');
266
+ return { patterns, paths: rest, fixedStrings, ignoreCase, invert, lineNumber, recursive, count, filesWithMatches, quiet, onlyMatching, noFilename, withFilename, maxCount, beforeContext, afterContext, includes, excludes, excludeDirs };
267
+ }
268
+ function parseNonnegativeCount(value, message) {
269
+ if (value === undefined)
270
+ throw new Error(message);
271
+ const count = Number(value);
272
+ if (!Number.isInteger(count) || count < 0)
273
+ throw new Error(message);
274
+ return count;
275
+ }
276
+ export function makeTextMatcher(patterns, options) {
277
+ const matchers = patterns.map((pattern) => makeSingleTextMatcher(pattern, options));
278
+ return (line) => matchers.some((matcher) => matcher(line));
279
+ }
280
+ function makeSingleTextMatcher(pattern, options) {
281
+ if (options.fixedStrings) {
282
+ const needle = options.ignoreCase ? pattern.toLowerCase() : pattern;
283
+ return (line) => (options.ignoreCase ? line.toLowerCase() : line).includes(needle);
284
+ }
285
+ try {
286
+ const regex = new RegExp(pattern, options.ignoreCase ? 'i' : '');
287
+ return (line) => regex.test(line);
288
+ }
289
+ catch {
290
+ const needle = options.ignoreCase ? pattern.toLowerCase() : pattern;
291
+ return (line) => (options.ignoreCase ? line.toLowerCase() : line).includes(needle);
292
+ }
293
+ }
294
+ export function extractTextMatches(patterns, line, options) {
295
+ const matches = patterns.flatMap((pattern) => extractSingleTextMatches(pattern, line, options));
296
+ return matches.length ? matches : (makeTextMatcher(patterns, options)(line) ? [line] : []);
297
+ }
298
+ function extractSingleTextMatches(pattern, line, options) {
299
+ if (options.fixedStrings)
300
+ return extractFixedTextMatches(pattern, line, options.ignoreCase);
301
+ try {
302
+ const flags = `g${options.ignoreCase ? 'i' : ''}`;
303
+ const regex = new RegExp(pattern, flags);
304
+ const output = [];
305
+ let match;
306
+ while ((match = regex.exec(line)) !== null) {
307
+ output.push(match[0]);
308
+ if (match[0] === '')
309
+ regex.lastIndex += 1;
310
+ }
311
+ return output;
312
+ }
313
+ catch {
314
+ return extractFixedTextMatches(pattern, line, options.ignoreCase);
315
+ }
316
+ }
317
+ function extractFixedTextMatches(pattern, line, ignoreCase) {
318
+ if (!pattern)
319
+ return [];
320
+ const haystack = ignoreCase ? line.toLowerCase() : line;
321
+ const needle = ignoreCase ? pattern.toLowerCase() : pattern;
322
+ const output = [];
323
+ let index = 0;
324
+ while (index <= haystack.length) {
325
+ const found = haystack.indexOf(needle, index);
326
+ if (found < 0)
327
+ break;
328
+ output.push(line.slice(found, found + pattern.length));
329
+ index = found + Math.max(1, pattern.length);
330
+ }
331
+ return output;
332
+ }
333
+ export function parseWcArgs(args) {
334
+ const fields = [];
335
+ const paths = [];
336
+ for (let index = 0; index < args.length; index += 1) {
337
+ const arg = args[index];
338
+ if (arg === '--') {
339
+ paths.push(...args.slice(index + 1));
340
+ break;
341
+ }
342
+ if (arg === '--lines') {
343
+ pushWcField(fields, 'lines');
344
+ continue;
345
+ }
346
+ if (arg === '--words') {
347
+ pushWcField(fields, 'words');
348
+ continue;
349
+ }
350
+ if (arg === '--bytes' || arg === '--chars') {
351
+ pushWcField(fields, 'bytes');
352
+ continue;
353
+ }
354
+ if (arg.startsWith('--'))
355
+ throw new Error(`wc: unsupported option: ${arg}`);
356
+ if (arg.startsWith('-') && arg.length > 1) {
357
+ for (const flag of arg.slice(1)) {
358
+ if (flag === 'l')
359
+ pushWcField(fields, 'lines');
360
+ else if (flag === 'w')
361
+ pushWcField(fields, 'words');
362
+ else if (flag === 'c' || flag === 'm')
363
+ pushWcField(fields, 'bytes');
364
+ else
365
+ throw new Error(`wc: unsupported option: -${flag}`);
366
+ }
367
+ continue;
368
+ }
369
+ paths.push(arg);
370
+ }
371
+ return { fields: fields.length ? fields : ['lines', 'words', 'bytes'], paths };
372
+ }
373
+ function pushWcField(fields, field) {
374
+ if (!fields.includes(field))
375
+ fields.push(field);
376
+ }
377
+ export function parseHeadTailArgs(args, fallback, command = 'head') {
378
+ let count = fallback;
379
+ let unit = 'lines';
380
+ let fromLine;
381
+ let fromByte;
382
+ const paths = [];
383
+ for (let index = 0; index < args.length; index += 1) {
384
+ const arg = args[index];
385
+ if (arg === '--') {
386
+ paths.push(...args.slice(index + 1));
387
+ break;
388
+ }
389
+ if (arg === '-n') {
390
+ const value = requireHeadTailNumber(args[++index], command, '-n');
391
+ if (value.startsWith('+')) {
392
+ const parsed = parseHeadTailNumber(value.slice(1), command, '-n');
393
+ count = parsed;
394
+ fromLine = Math.max(1, parsed);
395
+ continue;
396
+ }
397
+ count = parseHeadTailNumber(value, command, '-n');
398
+ continue;
399
+ }
400
+ if (arg === '-c' || arg === '--bytes') {
401
+ const value = requireHeadTailNumber(args[++index], command, arg);
402
+ unit = 'bytes';
403
+ if (value.startsWith('+')) {
404
+ const parsed = parseHeadTailNumber(value.slice(1), command, arg);
405
+ count = parsed;
406
+ fromByte = Math.max(1, parsed);
407
+ continue;
408
+ }
409
+ count = parseHeadTailNumber(value, command, arg);
410
+ continue;
411
+ }
412
+ if (/^--bytes=\+?\d+$/.test(arg)) {
413
+ const value = arg.slice('--bytes='.length);
414
+ unit = 'bytes';
415
+ if (value.startsWith('+')) {
416
+ const parsed = parseHeadTailNumber(value.slice(1), command, '--bytes');
417
+ count = parsed;
418
+ fromByte = Math.max(1, parsed);
419
+ continue;
420
+ }
421
+ count = parseHeadTailNumber(value, command, '--bytes');
422
+ continue;
423
+ }
424
+ if (/^-c\+?\d+$/.test(arg)) {
425
+ const value = arg.slice(2);
426
+ unit = 'bytes';
427
+ if (value.startsWith('+')) {
428
+ const parsed = parseHeadTailNumber(value.slice(1), command, '-c');
429
+ count = parsed;
430
+ fromByte = Math.max(1, parsed);
431
+ continue;
432
+ }
433
+ count = parseHeadTailNumber(value, command, '-c');
434
+ continue;
435
+ }
436
+ if (/^-\+\d+$/.test(arg)) {
437
+ const parsed = parseHeadTailNumber(arg.slice(2), command, '-n');
438
+ count = parsed;
439
+ fromLine = Math.max(1, parsed);
440
+ continue;
441
+ }
442
+ if (/^-\d+$/.test(arg)) {
443
+ count = parseHeadTailNumber(arg.slice(1), command, '-n');
444
+ continue;
445
+ }
446
+ if (arg.startsWith('-'))
447
+ throw new Error(`${command}: unsupported option: ${arg}`);
448
+ paths.push(arg);
449
+ }
450
+ return { count, unit, fromLine, fromByte, paths };
451
+ }
452
+ function requireHeadTailNumber(value, command, option) {
453
+ if (value === undefined)
454
+ throw new Error(`${command}: option requires an argument: ${option}`);
455
+ return value;
456
+ }
457
+ function parseHeadTailNumber(value, command, option) {
458
+ if (!/^\d+$/.test(value))
459
+ throw new Error(`${command}: invalid number for ${option}: ${value}`);
460
+ return Math.max(0, Number(value));
461
+ }
462
+ export function parseCutArgs(args) {
463
+ let mode = 'fields';
464
+ let delimiter = '\t';
465
+ let fields = [];
466
+ let chars = [];
467
+ const paths = [];
468
+ for (let index = 0; index < args.length; index += 1) {
469
+ const arg = args[index];
470
+ if (arg === '-d') {
471
+ delimiter = requireCutValue(args, ++index, '-d');
472
+ continue;
473
+ }
474
+ if (arg === '--delimiter') {
475
+ delimiter = requireCutValue(args, ++index, '--delimiter');
476
+ continue;
477
+ }
478
+ if (arg.startsWith('--delimiter=')) {
479
+ delimiter = arg.slice('--delimiter='.length);
480
+ if (!delimiter)
481
+ throw new Error('cut: --delimiter requires an argument');
482
+ continue;
483
+ }
484
+ if (arg === '-f') {
485
+ mode = 'fields';
486
+ fields = parseNumberSelections(requireCutValue(args, ++index, '-f'));
487
+ continue;
488
+ }
489
+ if (arg === '--fields') {
490
+ mode = 'fields';
491
+ fields = parseNumberSelections(requireCutValue(args, ++index, '--fields'));
492
+ continue;
493
+ }
494
+ if (arg.startsWith('--fields=')) {
495
+ mode = 'fields';
496
+ fields = parseNumberSelections(arg.slice('--fields='.length));
497
+ continue;
498
+ }
499
+ if (arg.startsWith('-f') && arg.length > 2) {
500
+ mode = 'fields';
501
+ fields = parseNumberSelections(arg.slice(2));
502
+ continue;
503
+ }
504
+ if (arg === '-c') {
505
+ mode = 'chars';
506
+ chars = parseNumberSelections(requireCutValue(args, ++index, '-c'));
507
+ continue;
508
+ }
509
+ if (arg === '--characters') {
510
+ mode = 'chars';
511
+ chars = parseNumberSelections(requireCutValue(args, ++index, '--characters'));
512
+ continue;
513
+ }
514
+ if (arg.startsWith('--characters=')) {
515
+ mode = 'chars';
516
+ chars = parseNumberSelections(arg.slice('--characters='.length));
517
+ continue;
518
+ }
519
+ if (arg.startsWith('-c') && arg.length > 2) {
520
+ mode = 'chars';
521
+ chars = parseNumberSelections(arg.slice(2));
522
+ continue;
523
+ }
524
+ if (arg === '--') {
525
+ paths.push(...args.slice(index + 1));
526
+ break;
527
+ }
528
+ if (arg.startsWith('-'))
529
+ throw new Error(`cut: unsupported option: ${arg}`);
530
+ if (!arg.startsWith('-'))
531
+ paths.push(arg);
532
+ }
533
+ if (mode === 'fields' && !fields.length)
534
+ fields = [{ start: 1, end: 1 }];
535
+ if (mode === 'chars' && !chars.length)
536
+ chars = [{ start: 1, end: 1 }];
537
+ return { mode, delimiter, fields, chars, paths };
538
+ }
539
+ function requireCutValue(args, index, option) {
540
+ const value = args[index];
541
+ if (value === undefined || value === '')
542
+ throw new Error(`cut: ${option} requires an argument`);
543
+ return value;
544
+ }
545
+ export function parseUniqArgs(args) {
546
+ let count = false;
547
+ let repeatedOnly = false;
548
+ let uniqueOnly = false;
549
+ let ignoreCase = false;
550
+ const paths = [];
551
+ for (let index = 0; index < args.length; index += 1) {
552
+ const arg = args[index];
553
+ if (arg === '--') {
554
+ paths.push(...args.slice(index + 1));
555
+ break;
556
+ }
557
+ if (arg === '--count') {
558
+ count = true;
559
+ continue;
560
+ }
561
+ if (arg === '--repeated') {
562
+ repeatedOnly = true;
563
+ continue;
564
+ }
565
+ if (arg === '--unique') {
566
+ uniqueOnly = true;
567
+ continue;
568
+ }
569
+ if (arg === '--ignore-case') {
570
+ ignoreCase = true;
571
+ continue;
572
+ }
573
+ if (arg.startsWith('--'))
574
+ throw new Error(`uniq: unrecognized option '${arg}'`);
575
+ if (arg.startsWith('-') && arg.length > 1) {
576
+ for (const flag of arg.slice(1)) {
577
+ if (flag === 'c')
578
+ count = true;
579
+ else if (flag === 'd')
580
+ repeatedOnly = true;
581
+ else if (flag === 'u')
582
+ uniqueOnly = true;
583
+ else if (flag === 'i')
584
+ ignoreCase = true;
585
+ else
586
+ throw new Error(`uniq: invalid option -- '${flag}'`);
587
+ }
588
+ continue;
589
+ }
590
+ paths.push(arg);
591
+ }
592
+ return { count, repeatedOnly, uniqueOnly, ignoreCase, paths };
593
+ }
594
+ export function parseSedArgs(args) {
595
+ let expression = '';
596
+ let quiet = false;
597
+ let inPlace = false;
598
+ let backupSuffix;
599
+ const paths = [];
600
+ for (let index = 0; index < args.length; index += 1) {
601
+ const arg = args[index];
602
+ if (arg === '-n') {
603
+ quiet = true;
604
+ continue;
605
+ }
606
+ if (arg === '-i' || arg === '--in-place') {
607
+ inPlace = true;
608
+ continue;
609
+ }
610
+ if (arg.startsWith('-i') && arg.length > 2) {
611
+ inPlace = true;
612
+ backupSuffix = arg.slice(2);
613
+ continue;
614
+ }
615
+ if (arg.startsWith('--in-place=')) {
616
+ inPlace = true;
617
+ backupSuffix = arg.slice('--in-place='.length);
618
+ continue;
619
+ }
620
+ if (arg === '-E' || arg === '-r')
621
+ continue;
622
+ if (arg === '-e') {
623
+ expression = args[++index] || '';
624
+ continue;
625
+ }
626
+ if (!expression)
627
+ expression = arg;
628
+ else
629
+ paths.push(arg);
630
+ }
631
+ return { expression, paths, quiet, inPlace, backupSuffix };
632
+ }
633
+ export function parseAwkArgs(args) {
634
+ let fieldSeparator;
635
+ const rest = [];
636
+ for (let index = 0; index < args.length; index += 1) {
637
+ const arg = args[index];
638
+ if (arg === '--') {
639
+ rest.push(...args.slice(index + 1));
640
+ break;
641
+ }
642
+ if (arg === '-F') {
643
+ fieldSeparator = decodeAwkSeparator(args[++index] || '');
644
+ continue;
645
+ }
646
+ if (arg.startsWith('-F') && arg.length > 2) {
647
+ fieldSeparator = decodeAwkSeparator(arg.slice(2));
648
+ continue;
649
+ }
650
+ rest.push(arg);
651
+ }
652
+ if (!rest.length)
653
+ throw new Error('awk requires a program');
654
+ return { program: rest[0], fieldSeparator, paths: rest.slice(1) };
655
+ }
656
+ export function applyUniq(input, options) {
657
+ const lines = splitLines(input);
658
+ const rows = [];
659
+ for (const line of lines) {
660
+ const key = options.ignoreCase ? line.toLowerCase() : line;
661
+ const last = rows.at(-1);
662
+ if (last?.key === key)
663
+ last.count += 1;
664
+ else
665
+ rows.push({ line, key, count: 1 });
666
+ }
667
+ const output = rows
668
+ .filter((row) => !options.repeatedOnly || row.count > 1)
669
+ .filter((row) => !options.uniqueOnly || row.count === 1)
670
+ .map((row) => options.count ? `${String(row.count).padStart(7)} ${row.line}` : row.line);
671
+ return output.length ? `${output.join('\n')}\n` : '';
672
+ }
673
+ export function applySed(input, expression, options = {}) {
674
+ const printed = applySedPrint(input, expression);
675
+ if (printed !== undefined)
676
+ return printed;
677
+ const deleted = applySedDelete(input, expression, options);
678
+ if (deleted !== undefined)
679
+ return deleted;
680
+ const substitution = parseSedSubstitution(expression);
681
+ if (!substitution)
682
+ throw new Error(`unsupported sed expression: ${expression}`);
683
+ const { pattern, replacement, flags } = substitution;
684
+ const regex = new RegExp(pattern, flags.includes('g') ? 'g' : '');
685
+ const rows = [];
686
+ for (const line of input.split('\n')) {
687
+ const replaced = line.replace(regex, replacement);
688
+ if (!options.quiet)
689
+ rows.push(replaced);
690
+ else if (flags.includes('p') && replaced !== line)
691
+ rows.push(replaced);
692
+ }
693
+ return rows.join('\n');
694
+ }
695
+ export function applyAwk(input, program, options = {}) {
696
+ const print = parseAwkPrintProgram(program);
697
+ if (!print)
698
+ throw new Error(`unsupported awk program: ${program}`);
699
+ const fields = print.fields.length ? print.fields : ['$0'];
700
+ const rows = splitLines(input).flatMap((line, index) => {
701
+ if (!matchesAwkCondition(line, index + 1, print.condition))
702
+ return [];
703
+ const parts = splitAwkFields(line, options.fieldSeparator);
704
+ return [fields.map((field) => {
705
+ if (field === '$0')
706
+ return line;
707
+ if (field === 'NR')
708
+ return String(index + 1);
709
+ if (field === 'NF')
710
+ return String(parts.length);
711
+ if (field === '$NF')
712
+ return parts.at(-1) ?? '';
713
+ const match = field.match(/^\$(\d+)$/);
714
+ if (match)
715
+ return parts[Number(match[1]) - 1] ?? '';
716
+ return field.replace(/^["']|["']$/g, '');
717
+ }).join(' ')];
718
+ });
719
+ return rows.length ? `${rows.join('\n')}\n` : '';
720
+ }
721
+ function parseAwkPrintProgram(program) {
722
+ const print = program.match(/^(?:(NR\s*(?:==|!=|>=|<=|>|<)\s*\d+|\/.+\/)\s*)?\{\s*print(?:\s+(.+?))?\s*\}$/);
723
+ if (!print)
724
+ return undefined;
725
+ return {
726
+ condition: print[1],
727
+ fields: print[2] ? print[2].split(/\s*,\s*|\s+/).filter(Boolean) : [],
728
+ };
729
+ }
730
+ function matchesAwkCondition(line, lineNumber, condition) {
731
+ if (!condition)
732
+ return true;
733
+ const nr = condition.match(/^NR\s*(==|!=|>=|<=|>|<)\s*(\d+)$/);
734
+ if (nr) {
735
+ const value = Number(nr[2]);
736
+ if (nr[1] === '==')
737
+ return lineNumber === value;
738
+ if (nr[1] === '!=')
739
+ return lineNumber !== value;
740
+ if (nr[1] === '>=')
741
+ return lineNumber >= value;
742
+ if (nr[1] === '<=')
743
+ return lineNumber <= value;
744
+ if (nr[1] === '>')
745
+ return lineNumber > value;
746
+ if (nr[1] === '<')
747
+ return lineNumber < value;
748
+ }
749
+ const regex = condition.match(/^\/(.+)\/$/);
750
+ if (regex)
751
+ return new RegExp(regex[1]).test(line);
752
+ return false;
753
+ }
754
+ function splitAwkFields(line, fieldSeparator) {
755
+ if (fieldSeparator === undefined || fieldSeparator === ' ') {
756
+ const trimmed = line.trim();
757
+ return trimmed ? trimmed.split(/\s+/) : [];
758
+ }
759
+ return line.split(fieldSeparator);
760
+ }
761
+ function decodeAwkSeparator(value) {
762
+ return value
763
+ .replace(/\\t/g, '\t')
764
+ .replace(/\\n/g, '\n')
765
+ .replace(/\\s/g, ' ');
766
+ }
767
+ export function translateText(input, args) {
768
+ const deleteMode = args[0] === '-d';
769
+ const offset = deleteMode ? 1 : 0;
770
+ const source = expandTrSet(args[offset] || '');
771
+ if (deleteMode) {
772
+ const remove = new Set([...source]);
773
+ return [...input].filter((char) => !remove.has(char)).join('');
774
+ }
775
+ const target = expandTrSet(args[offset + 1] || '');
776
+ if (!source)
777
+ return input;
778
+ const mapping = new Map();
779
+ for (let index = 0; index < source.length; index += 1) {
780
+ mapping.set(source[index], target[index] ?? target.at(-1) ?? '');
781
+ }
782
+ return [...input].map((char) => mapping.get(char) ?? char).join('');
783
+ }
784
+ function parseSedSubstitution(expression) {
785
+ if (!expression.startsWith('s') || expression.length < 4)
786
+ return undefined;
787
+ const delimiter = expression[1];
788
+ const parts = [];
789
+ let current = '';
790
+ let escaped = false;
791
+ for (let index = 2; index < expression.length; index += 1) {
792
+ const char = expression[index];
793
+ if (escaped) {
794
+ current += char;
795
+ escaped = false;
796
+ continue;
797
+ }
798
+ if (char === '\\') {
799
+ current += char;
800
+ escaped = true;
801
+ continue;
802
+ }
803
+ if (char === delimiter && parts.length < 2) {
804
+ parts.push(current);
805
+ current = '';
806
+ continue;
807
+ }
808
+ current += char;
809
+ }
810
+ parts.push(current);
811
+ if (parts.length !== 3)
812
+ return undefined;
813
+ return { pattern: parts[0], replacement: translateSedReplacement(parts[1]), flags: parts[2] };
814
+ }
815
+ function translateSedReplacement(value) {
816
+ let output = '';
817
+ for (let index = 0; index < value.length; index += 1) {
818
+ const char = value[index];
819
+ if (char === '\\') {
820
+ const next = value[++index];
821
+ if (next === undefined) {
822
+ output += '\\';
823
+ }
824
+ else if (/^\d$/.test(next)) {
825
+ output += `$${next}`;
826
+ }
827
+ else if (next === 'n') {
828
+ output += '\n';
829
+ }
830
+ else {
831
+ output += next;
832
+ }
833
+ continue;
834
+ }
835
+ if (char === '&') {
836
+ output += '$&';
837
+ continue;
838
+ }
839
+ output += char === '$' ? '$$' : char;
840
+ }
841
+ return output;
842
+ }
843
+ function applySedPrint(input, expression) {
844
+ const lines = splitLines(input);
845
+ if (expression === 'p')
846
+ return lines.length ? `${lines.join('\n')}\n` : '';
847
+ const range = expression.match(/^(\d+|\$)(?:,(\d+|\$))?p$/);
848
+ if (range) {
849
+ const start = sedAddress(range[1], lines.length);
850
+ const end = sedAddress(range[2] || range[1], lines.length);
851
+ const selected = lines.filter((_, index) => index + 1 >= start && index + 1 <= end);
852
+ return selected.length ? `${selected.join('\n')}\n` : '';
853
+ }
854
+ const match = expression.match(/^\/(.+)\/p$/);
855
+ if (match) {
856
+ const regex = new RegExp(match[1]);
857
+ const selected = lines.filter((line) => regex.test(line));
858
+ return selected.length ? `${selected.join('\n')}\n` : '';
859
+ }
860
+ return undefined;
861
+ }
862
+ function applySedDelete(input, expression, options = {}) {
863
+ const address = parseSedAddressExpression(expression, 'd');
864
+ if (!address)
865
+ return undefined;
866
+ if (options.quiet)
867
+ return '';
868
+ const lines = splitLines(input);
869
+ const selected = selectedSedLineIndexes(lines, address);
870
+ const output = lines.filter((_, index) => !selected.has(index + 1));
871
+ return output.length ? `${output.join('\n')}\n` : '';
872
+ }
873
+ function parseSedAddressExpression(expression, command) {
874
+ const escaped = command.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
875
+ const range = expression.match(new RegExp(`^(\\d+|\\$)(?:,(\\d+|\\$))?${escaped}$`));
876
+ if (range)
877
+ return { start: range[1], end: range[2] };
878
+ const pattern = expression.match(new RegExp(`^/(.+)/${escaped}$`));
879
+ if (pattern)
880
+ return { start: '1', pattern: pattern[1] };
881
+ return undefined;
882
+ }
883
+ function selectedSedLineIndexes(lines, address) {
884
+ if (address.pattern) {
885
+ const regex = new RegExp(address.pattern);
886
+ const indexes = lines
887
+ .map((line, index) => regex.test(line) ? index + 1 : undefined)
888
+ .filter((index) => index !== undefined);
889
+ return new Set(indexes);
890
+ }
891
+ const start = sedAddress(address.start, lines.length);
892
+ const end = sedAddress(address.end || address.start, lines.length);
893
+ return new Set(lines.map((_, index) => index + 1).filter((number) => number >= start && number <= end));
894
+ }
895
+ function sedAddress(value, lineCount) {
896
+ return value === '$' ? lineCount : Number(value);
897
+ }
898
+ export function takeLines(input, count, side, fromLine) {
899
+ const lines = splitLines(input);
900
+ const selected = fromLine && side === 'tail'
901
+ ? lines.slice(fromLine - 1)
902
+ : side === 'head'
903
+ ? lines.slice(0, count)
904
+ : lines.slice(Math.max(0, lines.length - count));
905
+ return selected.length ? `${selected.join('\n')}\n` : '';
906
+ }
907
+ export function takeBytes(input, count, side, fromByte) {
908
+ const bytes = new TextEncoder().encode(input);
909
+ const selected = fromByte && side === 'tail'
910
+ ? bytes.slice(fromByte - 1)
911
+ : side === 'head'
912
+ ? bytes.slice(0, count)
913
+ : bytes.slice(Math.max(0, bytes.length - count));
914
+ return new TextDecoder().decode(selected);
915
+ }
916
+ function parseNumberSelections(value) {
917
+ const selections = [];
918
+ for (const part of value.split(',')) {
919
+ if (!part)
920
+ continue;
921
+ const range = part.match(/^(\d+)-(\d+)$/);
922
+ if (range) {
923
+ const start = Number(range[1]);
924
+ const end = Number(range[2]);
925
+ if (start > 0 && end >= start)
926
+ selections.push({ start, end });
927
+ continue;
928
+ }
929
+ const suffix = part.match(/^(\d+)-$/);
930
+ if (suffix) {
931
+ const start = Number(suffix[1]);
932
+ if (start > 0)
933
+ selections.push({ start });
934
+ continue;
935
+ }
936
+ const prefix = part.match(/^-(\d+)$/);
937
+ if (prefix) {
938
+ const end = Number(prefix[1]);
939
+ if (end > 0)
940
+ selections.push({ start: 1, end });
941
+ continue;
942
+ }
943
+ const single = Number(part);
944
+ if (Number.isFinite(single) && single > 0)
945
+ selections.push({ start: single, end: single });
946
+ }
947
+ return selections;
948
+ }
949
+ function expandTrSet(value) {
950
+ const range = value.match(/^(.?)-(.?)$/);
951
+ if (!range || range[1].length !== 1 || range[2].length !== 1)
952
+ return value;
953
+ const start = range[1].charCodeAt(0);
954
+ const end = range[2].charCodeAt(0);
955
+ if (start > end)
956
+ return value;
957
+ let output = '';
958
+ for (let code = start; code <= end; code += 1)
959
+ output += String.fromCharCode(code);
960
+ return output;
961
+ }
962
+ export function splitLines(input) {
963
+ if (!input)
964
+ return [];
965
+ const lines = input.split('\n');
966
+ if (lines.at(-1) === '')
967
+ lines.pop();
968
+ return lines;
969
+ }
970
+ //# sourceMappingURL=text.js.map