@preventive/triage 1.0.0-alpha.1 → 1.0.0-alpha.10

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 (56) hide show
  1. package/api/reap.ts +79 -0
  2. package/common/save-error-reason.ts +20 -7
  3. package/common/server-info.ts +30 -0
  4. package/out/brotli-fallback.js +1 -1
  5. package/out/client-admin.js +28 -0
  6. package/out/client-managed.js +1 -0
  7. package/out/client-sync.js +16 -13
  8. package/out/graph.js +5 -4
  9. package/out/index.html +4 -15
  10. package/out/prism.js +2 -2
  11. package/out/terminal.js +32 -28
  12. package/out/view.css +1 -1
  13. package/out/view.js +78 -52
  14. package/package.json +70 -54
  15. package/{server → server-common}/origin.ts +5 -5
  16. package/{server → server-e2e}/auth.ts +5 -1
  17. package/{server → server-e2e}/bus-receiver.ts +8 -8
  18. package/server-e2e/cli.js +22 -0
  19. package/{server → server-e2e}/config.ts +21 -8
  20. package/{server → server-e2e}/db-neon.ts +31 -25
  21. package/{server → server-e2e}/db-revision-sql.ts +7 -10
  22. package/{server → server-e2e}/db-stmt.ts +2 -2
  23. package/{server → server-e2e}/db.ts +96 -135
  24. package/{server → server-e2e}/http.ts +97 -9
  25. package/{server → server-e2e}/hub.ts +7 -8
  26. package/{server → server-e2e}/index.ts +67 -43
  27. package/{server → server-e2e}/lifecycle.ts +14 -5
  28. package/{server → server-e2e}/npm-proxy.ts +1 -1
  29. package/{server → server-e2e}/objstore/blob-fs.ts +6 -8
  30. package/{server → server-e2e}/objstore/blob-vercel.ts +69 -36
  31. package/{server → server-e2e}/objstore/blob.ts +24 -9
  32. package/server-e2e/objstore/fetch-mint-guard.ts +74 -0
  33. package/{server → server-e2e}/objstore/handlers.ts +13 -15
  34. package/{server → server-e2e}/objstore/init.ts +47 -13
  35. package/{server → server-e2e}/objstore/reaper.ts +31 -11
  36. package/server-e2e/objstore/rest-deny.ts +28 -0
  37. package/server-e2e/objstore/rest-mint.ts +224 -0
  38. package/{server → server-e2e}/objstore/rest.ts +110 -93
  39. package/{server → server-e2e}/objstore/sign.ts +105 -0
  40. package/{server → server-e2e}/objstore/store-neon.ts +19 -19
  41. package/{server → server-e2e}/objstore/store.ts +98 -118
  42. package/{server → server-e2e}/objstore/tokens.ts +9 -12
  43. package/{server → server-e2e}/peer.ts +7 -9
  44. package/{server → server-e2e}/pubsub.ts +21 -31
  45. package/{server → server-e2e}/sign.ts +12 -14
  46. package/{server → server-e2e}/sse-server.ts +105 -73
  47. package/{server → server-e2e}/sse-session.ts +30 -16
  48. package/{server → server-e2e}/static.ts +22 -17
  49. package/{server → server-e2e}/sync-handlers.ts +172 -117
  50. package/{server → server-e2e}/util.ts +9 -0
  51. package/{server → server-e2e}/ws-server.ts +29 -23
  52. package/strip-types-loader.js +94 -0
  53. /package/{server → server-e2e}/config.example.json +0 -0
  54. /package/{server → server-e2e}/neon-driver.ts +0 -0
  55. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  56. /package/{server → server-e2e}/validation.ts +0 -0
package/out/terminal.js CHANGED
@@ -1,39 +1,43 @@
1
- var Z=globalThis[Symbol.for("@rray/frontend")];if(!Z)throw new Error("@rray/frontend global not installed; view.js must run before lazy bundles import this wrapper");var{LitElement:Q,html:C,nothing:q,unsafeCSS:J,render:Sn,StateElement:vn,classMap:yn,repeat:$n,styleMap:kn}=Z;function tt(t,e){if(t.length===0)return{argv:[],quoted:new Set};let r=[t[0]],n=new Set;e.has(0)&&n.add(0);for(let s=1;s<t.length;s++){let i=t[s];if(e.has(s)){n.add(r.length),r.push(i);continue}for(let o of et(i))r.push(o)}return{argv:r,quoted:n}}function et(t){for(let e=0;e<t.length;e++){if(t[e]!=="{")continue;let r=Pt(t,e);if(r===-1)continue;let n=Bt(t.slice(e+1,r));if(n.length<2)continue;let s=t.slice(0,e),i=t.slice(r+1),o=[];for(let u of n)o.push(...et(s+u+i));return o}return[t]}function Pt(t,e){let r=1;for(let n=e+1;n<t.length;n++)if(t[n]==="{")r++;else if(t[n]==="}"&&(r--,r===0))return n;return-1}function Bt(t){let e=[],r=0,n=0;for(let s=0;s<t.length;s++)t[s]==="{"?r++:t[s]==="}"?r--:t[s]===","&&r===0&&(e.push(t.slice(n,s)),n=s+1);return e.push(t.slice(n)),e}function nt(t){let e=Xt(t);for(let s of e)if(s.kind==="amp")throw new Error("background processes (`&`) are not supported");for(;e.length>0&&e.at(-1).kind==="semi";)e.pop();let{steps:r,consumed:n}=st(e,0,!1);if(n!==e.length)throw new Error("unexpected `)`");return rt(r),r}function rt(t){for(let e of t){if(e.stages.length===0)throw new Error("empty pipeline stage");for(let r of e.stages)if(r.group)rt(r.group);else if(r.argv.length===0)throw new Error("empty pipeline stage")}}function st(t,e,r){let n=[{gate:"first",stages:[]}],s={argv:[],quoted:new Set},i=e;for(;i<t.length;){let o=t[i];if(o.kind==="paren_close"){if(!r)throw new Error("unexpected `)`");return Ht(n,s,i+1)}if(o.kind==="paren_open"){if(s.group||s.argv.length>0)throw new Error("unexpected `(`");let u=st(t,i+1,!0);s.group=u.steps,i=u.consumed;continue}if(o.kind==="pipe"||o.kind==="and"||o.kind==="or"||o.kind==="semi"){n.at(-1).stages.push(s),s={argv:[],quoted:new Set},o.kind==="and"?n.push({gate:"and",stages:[]}):o.kind==="or"?n.push({gate:"or",stages:[]}):o.kind==="semi"&&n.push({gate:"seq",stages:[]}),i++;continue}if(o.kind==="redir"){i=zt(s,t,i)+1;continue}if(s.group)throw new Error("unexpected token after `)`");o.quoted&&s.quoted.add(s.argv.length),s.argv.push(o.value),i++}if(r)throw new Error("unmatched `(`");return n.at(-1).stages.push(s),{steps:n,consumed:i}}function Ht(t,e,r){let n=t.at(-1);if(!e.group&&e.argv.length===0&&!Gt(e)&&n.stages.length===0){if(t.length===1)throw new Error("empty subshell `()`");t.pop()}else n.stages.push(e);return{steps:t,consumed:r}}function Gt(t){return!!(t.stdoutToNull||t.stderrToNull||t.mergeStderrToStdout||t.mergeStdoutToStderr)}function zt(t,e,r){let n=e[r],s=n.fd==="1"?"":n.fd;if(n.toFd)return n.fd==="2"&&n.toFd==="1"?t.mergeStderrToStdout=!0:n.fd==="1"&&n.toFd==="2"&&(t.mergeStdoutToStderr=!0),r;let i=e[r+1],o=s+">"+(n.append?">":"");if(n.append)throw new Error(`filesystem is read-only \u2014 \`${o}\` append is not supported; use \`|\` to pipe or \`${s}>/dev/null\` to discard`);if(!i||i.kind!=="word")throw new Error(`redirect \`${o}\` requires a target`);if(i.value!=="/dev/null")throw new Error(`filesystem is read-only \u2014 use \`|\` to pipe between commands, or \`${o}/dev/null\` to discard`);return n.fd==="1"?t.stdoutToNull=!0:t.stderrToNull=!0,r+1}function Xt(t){let e=[],r="",n=null,s=!1,i=!1,o=()=>{s&&e.push({kind:"word",value:r,quoted:i}),r="",s=!1,i=!1};for(let u=0;u<t.length;u++){let c=t[u];if(n){c===n?n=null:r+=c;continue}if(c==="'"||c==='"'){n=c,s=!0,i=!0;continue}if(!s&&(c==="1"||c==="2")&&t[u+1]===">"){if(o(),t[u+2]==="&"){let l=t[u+3],f=t[u+4];if((l==="1"||l==="2")&&(f===void 0||/[\s|&>;()]/u.test(f))){e.push({kind:"redir",fd:c,toFd:l}),u+=3;continue}throw new Error(`redirect \`${c}>&\` requires fd 1 or 2 followed by a token boundary`)}t[u+2]===">"?(e.push({kind:"redir",fd:c,append:!0}),u+=2):(e.push({kind:"redir",fd:c,append:!1}),u+=1);continue}if(c==="|"){o(),t[u+1]==="|"?(e.push({kind:"or"}),u++):e.push({kind:"pipe"});continue}if(c==="&"){o(),t[u+1]==="&"?(e.push({kind:"and"}),u++):e.push({kind:"amp"});continue}if(c===">"){o(),t[u+1]===">"?(e.push({kind:"redir",fd:"1",append:!0}),u++):e.push({kind:"redir",fd:"1",append:!1});continue}if(c===";"){o(),e.push({kind:"semi"});continue}if(c==="("){o(),e.push({kind:"paren_open"});continue}if(c===")"){o(),e.push({kind:"paren_close"});continue}if(/\s/u.test(c)){o();continue}r+=c,s=!0}if(n)throw new Error(`unterminated ${n==="'"?"single":"double"} quote`);return o(),e}function p(t,e={}){let r=D(e.short),n=D(e.long),s=D(e.valueShort),i=D(e.valueLong),o=D(e.repeatable),u=e.stopAtFirstPositional??!1,c=new Set,l=new Map,f=[];for(let a=0;a<t.length;a++){let d=t[a];if(d==="--"){f.push(...t.slice(a+1));break}if(/^-+$/u.test(d)){f.push(d);continue}if(d.startsWith("--")&&d.length>2){let m=d.slice(2);if(i.has(m)||o.has(m))O(l,o,m,it(t,++a,`--${m}`));else if(n.has(m))c.add(m);else throw new Error(`unknown option: --${m}`);continue}if(d.startsWith("-")&&d.length>1&&!/^-\d/u.test(d)){a=Vt(t,a,r,s,o,c,l);continue}if(u){f.push(...t.slice(a));break}f.push(d)}return{flags:c,values:l,positional:f}}function D(t){return t instanceof Set?t:new Set(t??[])}function it(t,e,r){if(e>=t.length)throw new Error(`${r} requires an argument`);return t[e]}function O(t,e,r,n){if(!e.has(r)){t.set(r,n);return}let s=t.get(r);s?s.push(n):t.set(r,[n])}function Vt(t,e,r,n,s,i,o){let u=t[e].slice(1);for(let c=0;c<u.length;c++){let l=u[c];if(n.has(l)||s.has(l))return c+1<u.length?(O(o,s,l,u.slice(c+1)),e):(O(o,s,l,it(t,++e,`-${l}`)),e);if(!r.has(l))throw new Error(`unknown option: -${l}`);i.add(l)}return e}function M(t){let e=t.startsWith("/"),r=t.split("/").filter(Boolean),n=[];for(let s of r)if(s!=="."){if(s===".."){n.length>0&&n.at(-1)!==".."?n.pop():e||n.push("..");continue}n.push(s)}return e?"/"+n.join("/"):n.length===0?".":n.join("/")}function w(t,e){return e.startsWith("/")?M(e):M(t+"/"+e)}function I(t){let e=M(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return r<0?".":r===0?"/":e.slice(0,r)}function F(t){let e=M(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return e.slice(r+1)}function A(t,e){return t==="/"?"/"+e:t+"/"+e}function L(t,e){return t==="/"?e.slice(1):e.slice(t.length+1)}function ot(t){let e=new Map,r=t instanceof Map?t.entries():Object.entries(t??{});for(let[i,o]of r)typeof o=="string"&&e.set(M("/"+String(i)),o);let n=new Map([["/",{dirs:[],files:[]}]]);for(let i of e.keys()){let o=I(i);Kt(n,o),n.get(o).files.push(F(i))}for(let i of n.values())i.dirs.sort(),i.files.sort();let s={isFile:i=>e.has(i),isDir:i=>n.has(i),readFile:i=>e.get(i),listDir:i=>{let o=n.get(i);if(!o)throw new Error(`not a directory: ${i}`);return o},walkFiles:i=>Ut(s,i)};return s}function*P(t,e,r=Number.POSITIVE_INFINITY){if(t.isFile(e)){yield{path:e,kind:"file",depth:0};return}if(!t.isDir(e))return;yield{path:e,kind:"dir",depth:0};let n=[{path:e,depth:0}];for(let s=0;s<n.length;s++){let i=n[s];if(i.depth>=r)continue;let{dirs:o,files:u}=t.listDir(i.path),c=i.depth+1;for(let l of o){let f=A(i.path,l);yield{path:f,kind:"dir",depth:c},n.push({path:f,depth:c})}for(let l of u)yield{path:A(i.path,l),kind:"file",depth:c}}}function*Ut(t,e){for(let r of P(t,e))r.kind==="file"&&(yield r.path)}function Kt(t,e){let r=[],n=e;for(;n!=="/"&&!t.has(n);)r.push(n),n=I(n);for(let s=r.length-1;s>=0;s--){let i=r[s];t.set(i,{dirs:[],files:[]}),t.get(I(i)).dirs.push(F(i))}}var g=(t="")=>({stdout:t,stderr:"",exitCode:0}),h=(t,e=1)=>({stdout:"",stderr:t.endsWith(`
1
+ var Q=globalThis[Symbol.for("@rray/frontend")];if(!Q)throw new Error("@rray/frontend global not installed; view.js must run before lazy bundles import this wrapper");var{LitElement:tt,html:M,nothing:H,unsafeCSS:et,render:jn,StateElement:qn,classMap:Pn,repeat:Hn,styleMap:Bn}=Q;function nt(t,e){if(t.length===0)return{argv:[],quoted:new Set};let r=[t[0]],n=new Set;e.has(0)&&n.add(0);for(let s=1;s<t.length;s++){let o=t[s];if(e.has(s)){n.add(r.length),r.push(o);continue}for(let i of rt(o))r.push(i)}return{argv:r,quoted:n}}function rt(t){for(let e=0;e<t.length;e++){if(t[e]!=="{")continue;let r=Gt(t,e);if(r===-1)continue;let n=Ut(t.slice(e+1,r));if(n.length<2)continue;let s=t.slice(0,e),o=t.slice(r+1),i=[];for(let u of n)i.push(...rt(s+u+o));return i}return[t]}function Gt(t,e){let r=1;for(let n=e+1;n<t.length;n++)if(t[n]==="{")r++;else if(t[n]==="}"&&(r--,r===0))return n;return-1}function Ut(t){let e=[],r=0,n=0;for(let s=0;s<t.length;s++)t[s]==="{"?r++:t[s]==="}"?r--:t[s]===","&&r===0&&(e.push(t.slice(n,s)),n=s+1);return e.push(t.slice(n)),e}function st(t){let e=Vt(t);for(let s of e)if(s.kind==="amp")throw new Error("background processes (`&`) are not supported");for(;e.length>0&&e.at(-1).kind==="semi";)e.pop();let{steps:r,consumed:n}=it(e,0,!1);if(n!==e.length)throw new Error("unexpected `)`");return ot(r),r}function ot(t){for(let e of t){if(e.stages.length===0)throw new Error("empty pipeline stage");for(let r of e.stages)if(r.group)ot(r.group);else if(r.argv.length===0)throw new Error("empty pipeline stage")}}function it(t,e,r){let n=[{gate:"first",stages:[]}],s={argv:[],quoted:new Set},o=e;for(;o<t.length;){let i=t[o];if(i.kind==="paren_close"){if(!r)throw new Error("unexpected `)`");return zt(n,s,o+1)}if(i.kind==="paren_open"){if(s.group||s.argv.length>0)throw new Error("unexpected `(`");let u=it(t,o+1,!0);s.group=u.steps,o=u.consumed;continue}if(i.kind==="pipe"||i.kind==="and"||i.kind==="or"||i.kind==="semi"){n.at(-1).stages.push(s),s={argv:[],quoted:new Set},i.kind==="and"?n.push({gate:"and",stages:[]}):i.kind==="or"?n.push({gate:"or",stages:[]}):i.kind==="semi"&&n.push({gate:"seq",stages:[]}),o++;continue}if(i.kind==="redir"){o=Xt(s,t,o)+1;continue}if(s.group)throw new Error("unexpected token after `)`");i.quoted&&s.quoted.add(s.argv.length),s.argv.push(i.value),o++}if(r)throw new Error("unmatched `(`");return n.at(-1).stages.push(s),{steps:n,consumed:o}}function zt(t,e,r){let n=t.at(-1);if(!e.group&&e.argv.length===0&&!Yt(e)&&n.stages.length===0){if(t.length===1)throw new Error("empty subshell `()`");t.pop()}else n.stages.push(e);return{steps:t,consumed:r}}function Yt(t){return!!(t.stdoutToNull||t.stderrToNull||t.mergeStderrToStdout||t.mergeStdoutToStderr)}function Xt(t,e,r){let n=e[r],s=n.fd==="1"?"":n.fd;if(n.toFd)return n.fd==="2"&&n.toFd==="1"?t.mergeStderrToStdout=!0:n.fd==="1"&&n.toFd==="2"&&(t.mergeStdoutToStderr=!0),r;let o=e[r+1],i=s+">"+(n.append?">":"");if(n.append)throw new Error(`filesystem is read-only \u2014 \`${i}\` append is not supported; use \`|\` to pipe or \`${s}>/dev/null\` to discard`);if(!o||o.kind!=="word")throw new Error(`redirect \`${i}\` requires a target`);if(o.value!=="/dev/null")throw new Error(`filesystem is read-only \u2014 use \`|\` to pipe between commands, or \`${i}/dev/null\` to discard`);return n.fd==="1"?t.stdoutToNull=!0:t.stderrToNull=!0,r+1}function Vt(t){let e=[],r="",n=null,s=!1,o=!1,i=()=>{s&&e.push({kind:"word",value:r,quoted:o}),r="",s=!1,o=!1};for(let u=0;u<t.length;u++){let c=t[u];if(n){c===n?n=null:r+=c;continue}if(c==="'"||c==='"'){n=c,s=!0,o=!0;continue}if(!s&&(c==="1"||c==="2")&&t[u+1]===">"){if(i(),t[u+2]==="&"){let a=t[u+3],l=t[u+4];if((a==="1"||a==="2")&&(l===void 0||/[\s|&>;()]/u.test(l))){e.push({kind:"redir",fd:c,toFd:a}),u+=3;continue}throw new Error(`redirect \`${c}>&\` requires fd 1 or 2 followed by a token boundary`)}t[u+2]===">"?(e.push({kind:"redir",fd:c,append:!0}),u+=2):(e.push({kind:"redir",fd:c,append:!1}),u+=1);continue}if(c==="|"){i(),t[u+1]==="|"?(e.push({kind:"or"}),u++):e.push({kind:"pipe"});continue}if(c==="&"){i(),t[u+1]==="&"?(e.push({kind:"and"}),u++):e.push({kind:"amp"});continue}if(c===">"){i(),t[u+1]===">"?(e.push({kind:"redir",fd:"1",append:!0}),u++):e.push({kind:"redir",fd:"1",append:!1});continue}if(c===";"){i(),e.push({kind:"semi"});continue}if(c==="("){i(),e.push({kind:"paren_open"});continue}if(c===")"){i(),e.push({kind:"paren_close"});continue}if(/\s/u.test(c)){i();continue}r+=c,s=!0}if(n)throw new Error(`unterminated ${n==="'"?"single":"double"} quote`);return i(),e}function m(t,e={}){let r=F(e.short),n=F(e.long),s=F(e.valueShort),o=F(e.valueLong),i=F(e.repeatable),u=e.stopAtFirstPositional??!1,c=new Set,a=new Map,l=[];for(let f=0;f<t.length;f++){let h=t[f];if(h==="--"){l.push(...t.slice(f+1));break}if(/^-+$/u.test(h)){l.push(h);continue}if(h.startsWith("--")&&h.length>2){let p=h.slice(2);if(o.has(p)||i.has(p))B(a,i,p,ut(t,++f,`--${p}`));else if(n.has(p))c.add(p);else throw new Error(`unknown option: --${p}`);continue}if(h.startsWith("-")&&h.length>1&&!/^-\d/u.test(h)){f=Zt(t,f,r,s,i,c,a);continue}if(u){l.push(...t.slice(f));break}l.push(h)}return{flags:c,values:a,positional:l}}function F(t){return t instanceof Set?t:new Set(t??[])}function ut(t,e,r){if(e>=t.length)throw new Error(`${r} requires an argument`);return t[e]}function B(t,e,r,n){if(!e.has(r)){t.set(r,n);return}let s=t.get(r);s?s.push(n):t.set(r,[n])}function Zt(t,e,r,n,s,o,i){let u=t[e].slice(1);for(let c=0;c<u.length;c++){let a=u[c];if(n.has(a)||s.has(a))return c+1<u.length?(B(i,s,a,u.slice(c+1)),e):(B(i,s,a,ut(t,++e,`-${a}`)),e);if(!r.has(a))throw new Error(`unknown option: -${a}`);o.add(a)}return e}function I(t){let e=t.startsWith("/"),r=t.split("/").filter(Boolean),n=[];for(let s of r)if(s!=="."){if(s===".."){n.length>0&&n.at(-1)!==".."?n.pop():e||n.push("..");continue}n.push(s)}return e?"/"+n.join("/"):n.length===0?".":n.join("/")}function x(t,e){return e.startsWith("/")?I(e):I(t+"/"+e)}function W(t){let e=I(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return r<0?".":r===0?"/":e.slice(0,r)}function D(t){let e=I(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return e.slice(r+1)}function A(t,e){return t==="/"?"/"+e:t+"/"+e}function L(t,e){return t==="/"?e.slice(1):e.slice(t.length+1)}function ct(t){let e=new Map,r=t instanceof Map?t.entries():Object.entries(t??{});for(let[o,i]of r)typeof i=="string"&&e.set(I("/"+String(o)),i);let n=new Map([["/",{dirs:[],files:[]}]]);for(let o of e.keys()){let i=W(o);Kt(n,i),n.get(i).files.push(D(o))}for(let o of n.values())o.dirs.sort(),o.files.sort();let s={isFile:o=>e.has(o),isDir:o=>n.has(o),readFile:o=>e.get(o),listDir:o=>{let i=n.get(o);if(!i)throw new Error(`not a directory: ${o}`);return i},walkFiles:o=>Jt(s,o)};return s}function*G(t,e,r=Number.POSITIVE_INFINITY){if(t.isFile(e)){yield{path:e,kind:"file",depth:0};return}if(!t.isDir(e))return;yield{path:e,kind:"dir",depth:0};let n=[{path:e,depth:0}];for(let s=0;s<n.length;s++){let o=n[s];if(o.depth>=r)continue;let{dirs:i,files:u}=t.listDir(o.path),c=o.depth+1;for(let a of i){let l=A(o.path,a);yield{path:l,kind:"dir",depth:c},n.push({path:l,depth:c})}for(let a of u)yield{path:A(o.path,a),kind:"file",depth:c}}}function*Jt(t,e){for(let r of G(t,e))r.kind==="file"&&(yield r.path)}function Kt(t,e){let r=[],n=e;for(;n!=="/"&&!t.has(n);)r.push(n),n=W(n);for(let s=r.length-1;s>=0;s--){let o=r[s];t.set(o,{dirs:[],files:[]}),t.get(W(o)).dirs.push(D(o))}}var w=(t="")=>({stdout:t,stderr:"",exitCode:0}),d=(t,e=1)=>({stdout:"",stderr:t.endsWith(`
2
2
  `)?t:t+`
3
- `,exitCode:e}),k=t=>h(`usage: ${t}`,2);function S(t){if(t==="")return[];let e=t.split(`
4
- `);return e.at(-1)===""&&e.pop(),e}var v=t=>t.length===0?"":t.join(`
3
+ `,exitCode:e}),k=t=>d(`usage: ${t}`,2);function v(t){if(t==="")return[];let e=t.split(`
4
+ `);return e.at(-1)===""&&e.pop(),e}var $=t=>t.length===0?"":t.join(`
5
5
  `)+`
6
- `;function B(t,e,r){let n=[],s="",i=!1;for(let o of e){let u=w(r.cwd,o);if(r.fs.isDir(u)){s+=`${t}: ${o}: is a directory
7
- `,i=!0;continue}if(!r.fs.isFile(u)){s+=`${t}: ${o}: no such file or directory
8
- `,i=!0;continue}n.push({name:o,content:r.fs.readFile(u)})}return{inputs:n,stderr:s,failed:i}}function E(t,e,r,n){return e.length===0?{inputs:[{name:null,content:r}],stderr:"",failed:!1}:B(t,e,n)}function W(t,e,r,n){let s=E(t,e,r,n);return{content:s.inputs.map(i=>i.content).join(""),stderr:s.stderr,failed:s.failed}}var y=(t,e)=>({stdout:t,stderr:e.stderr,exitCode:e.failed?1:0});function T(t,e){if(typeof t!="string"||!/^\d+$/u.test(t))return{error:h(`${e}: invalid count: ${t}`)};let r=Number(t);return Number.isSafeInteger(r)?{value:r}:{error:h(`${e}: out of range: ${t}`)}}function Yt(t,e,r){let{positional:n}=p(e),s=E("tac",n,t,r),i=[];for(let{content:o}of s.inputs)i.push(...S(o).toReversed());return y(v(i),s)}var ut=1e6;function Zt(t,e){let{positional:r}=p(e);if(r.length===0||r.length>3)return k("seq LAST | seq FIRST LAST | seq FIRST INCR LAST");let n=[];for(let f of r){if(!/^-?\d+$/u.test(f))return h(`seq: invalid integer: ${f}`);n.push(Number(f))}let s,i,o;if(n.length===1?(s=1,i=1,o=n[0]):n.length===2?(s=n[0],o=n[1],i=s<=o?1:-1):[s,i,o]=n,i===0)return h("seq: increment must be non-zero");let c=(i>0?s<=o:s>=o)?Math.floor(Math.abs(o-s)/Math.abs(i))+1:0;if(c>ut)return h(`seq: range too large: ${c} elements exceeds limit of ${ut}`);let l=[];if(i>0)for(let f=s;f<=o;f+=i)l.push(String(f));else for(let f=s;f>=o;f+=i)l.push(String(f));return g(v(l))}function Qt(t,e,r){let{values:n,positional:s}=p(e,{valueShort:["b"]}),i=n.get("b")??"t";if(i!=="a"&&i!=="t")return h(`nl: -b: only \`a\` and \`t\` are supported (got \`${i}\`)`);let o=E("nl",s,t,r),u=[],c=0;for(let{content:l}of o.inputs)for(let f of S(l))i==="a"||f!==""?(c++,u.push(`${String(c).padStart(6)} ${f}`)):u.push(f);return y(v(u),o)}function Jt(t,e,r){let{values:n,positional:s}=p(e,{valueShort:["d","f","c"]}),i=n.has("f"),o=n.has("c");if(i===o)return k("cut -f LIST [-d DELIM] [file...] | cut -c LIST [file...]");if(o&&n.has("d"))return h("cut: -d is only valid with -f");let u=te(i?n.get("f"):n.get("c"));if(u.error)return u.error;let c=n.get("d")??" ";if(i&&c.length!==1)return h("cut: -d delimiter must be a single character");let l=E("cut",s,t,r),f=[];for(let{content:a}of l.inputs)for(let d of S(a))f.push(i?ee(d,c,u.ranges):lt([...d],u.ranges).join(""));return y(v(f),l)}function te(t){let e=[];for(let r of t.split(",")){if(r==="")return{error:h(`cut: empty list item in \`${t}\``)};if(/^\d+$/u.test(r)){let o=Number(r);if(o<1)return{error:h("cut: list items must be >= 1")};e.push([o,o]);continue}let n=r.match(/^(\d*)-(\d*)$/u);if(!n||n[1]===""&&n[2]==="")return{error:h(`cut: invalid list item: ${r}`)};let s=n[1]===""?1:Number(n[1]),i=n[2]===""?Number.POSITIVE_INFINITY:Number(n[2]);if(s<1)return{error:h("cut: list items must be >= 1")};if(i<s)return{error:h(`cut: reversed range: ${r}`)};e.push([s,i])}return{ranges:e}}function lt(t,e){let r=new Set,n=[];for(let[s,i]of e)for(let o=s;o<=Math.min(i,t.length);o++)r.has(o)||(r.add(o),n.push(o));return n.sort((s,i)=>s-i),n.map(s=>t[s-1])}function ee(t,e,r){let n=t.split(e);return n.length===1?t:lt(n,r).join(e)}function ne(t,e){let{flags:r,positional:n}=p(e,{short:["d","s"]}),s=r.has("d"),i=r.has("s");if(s&&i)return h("tr: -d combined with -s is not supported");let o=s||i?1:2;if(n.length!==o)return k("tr SET1 SET2 | tr -d SET | tr -s SET");let u=ct(n[0]);if(u.error)return u.error;if(s){let a=new Set(u.chars);return g([...t].filter(d=>!a.has(d)).join(""))}if(i)return g(re(t,new Set(u.chars)));let c=ct(n[1]);if(c.error)return c.error;if(c.chars.length===0)return h("tr: SET2 must not be empty");let l=new Map,f=c.chars.at(-1);for(let a=0;a<u.chars.length;a++)l.set(u.chars[a],c.chars[a]??f);return g([...t].map(a=>l.get(a)??a).join(""))}function ct(t){let e=[...t],r=[],n=0,s=()=>{if(e[n]!=="\\")return e[n++];if(n+1>=e.length)return null;let i=e[n+1];return n+=2,i==="n"?`
9
- `:i==="t"?" ":i==="\\"?"\\":i==="0"?"\0":i};for(;n<e.length;){let i=s();if(i===null)return{error:h("tr: trailing backslash in set")};if(e[n]==="-"&&n+1<e.length){n++;let o=s();if(o===null)return{error:h("tr: trailing backslash in set")};let u=i.codePointAt(0),c=o.codePointAt(0);if(c<u)return{error:h(`tr: reversed range: ${i}-${o}`)};for(let l=u;l<=c;l++)r.push(String.fromCodePoint(l))}else r.push(i)}return{chars:r}}function re(t,e){let r="",n=null;for(let s of t)e.has(s)&&s===n||(r+=s,n=s);return r}function se(t,e,r){let{positional:n}=p(e);if(n.length===0)return k("which COMMAND...");let s=[],i=0;for(let o of n)r.hasCommand(o)?s.push(`/usr/bin/${o}`):(s.push(`${o} not found`),i=1);return{stdout:v(s),stderr:"",exitCode:i}}var ft={cut:Jt,tac:Yt,tr:ne,seq:Zt,nl:Qt,which:se};var at=/[*?]/u;function G(t){return new RegExp("^"+t.replace(/[.+^${}()|[\]\\]/gu,"\\$&").replace(/\*/gu,".*").replace(/\?/gu,".")+"$","u")}function ht(t,e,r){if(t.length===0)return[];let n=[t[0]];for(let s=1;s<t.length;s++){let i=t[s];if(e.has(s)||!at.test(i)){n.push(i);continue}let o=ie(i,r);o.length>0?n.push(...o):n.push(i)}return n}function ie(t,e){let r=t.startsWith("/"),n=t.startsWith("./"),s=t.endsWith("/")&&t.length>1,i=t.split("/").filter(Boolean);if(i.length===0)return r?["/"]:[];let o=[r?"/":"."];for(let u=0;u<i.length;u++){let c=i[u];if(!at.test(c)){o=o.map(l=>H(l,c));continue}o=ue(o,c,u===i.length-1,e)}return s?o=o.filter(u=>e.fs.isDir(w(e.cwd,u))).map(u=>u==="/"?u:u+"/"):o=o.filter(u=>oe(u,e)),n&&(o=o.map(u=>u.startsWith("./")?u:"./"+u)),o.sort(),o}function oe(t,e){let r=w(e.cwd,t);return e.fs.isFile(r)||e.fs.isDir(r)}function ue(t,e,r,n){let s=G(e),i=e.startsWith("."),o=c=>!i&&c.startsWith(".")?!1:s.test(c),u=[];for(let c of t){let l=w(n.cwd,c);if(!n.fs.isDir(l))continue;let{dirs:f,files:a}=n.fs.listDir(l);for(let d of f)o(d)&&u.push(H(c,d));if(r)for(let d of a)o(d)&&u.push(H(c,d))}return u}function H(t,e){return t==="."?e:A(t,e)}var dt=new Set(["name","type","path","mindepth","maxdepth"]);function pt(t,e,r){let n=ce(e);if(n.error)return n.error;let{starts:s,minDepth:i,maxDepth:o,groups:u}=n,c=[];for(let l of s){let f=w(r.cwd,l);if(!r.fs.isDir(f)&&!r.fs.isFile(f))return h(`find: ${l}: no such file or directory`);for(let a of P(r.fs,f,o)){if(a.depth<i)continue;let d=ge(l,f,a.path);de(u,{kind:a.kind,path:d})&&c.push(d)}}return g(c.length===0?"":c.join(`
6
+ `;function U(t,e,r){let n=[],s="",o=!1;for(let i of e){let u=x(r.cwd,i);if(r.fs.isDir(u)){s+=`${t}: ${i}: is a directory
7
+ `,o=!0;continue}if(!r.fs.isFile(u)){s+=`${t}: ${i}: no such file or directory
8
+ `,o=!0;continue}n.push({name:i,content:r.fs.readFile(u)})}return{inputs:n,stderr:s,failed:o}}function E(t,e,r,n){return e.length===0?{inputs:[{name:null,content:r}],stderr:"",failed:!1}:U(t,e,n)}function R(t,e,r,n){let s=E(t,e,r,n);return{content:s.inputs.map(o=>o.content).join(""),stderr:s.stderr,failed:s.failed}}var T=(t,e)=>({stdout:t,stderr:e.stderr,exitCode:e.failed?1:0});function _(t,e){if(typeof t!="string"||!/^\d+$/u.test(t))return{error:d(`${e}: invalid count: ${t}`)};let r=Number(t);return Number.isSafeInteger(r)?{value:r}:{error:d(`${e}: out of range: ${t}`)}}function Qt(t,e,r){let{positional:n}=m(e),s=E("tac",n,t,r),o=[];for(let{content:i}of s.inputs)o.push(...v(i).toReversed());return T($(o),s)}var at=1e6;function te(t,e){let{positional:r}=m(e);if(r.length===0||r.length>3)return k("seq LAST | seq FIRST LAST | seq FIRST INCR LAST");let n=[];for(let l of r){if(!/^-?\d+$/u.test(l))return d(`seq: invalid integer: ${l}`);n.push(Number(l))}let s,o,i;if(n.length===1?(s=1,o=1,i=n[0]):n.length===2?(s=n[0],i=n[1],o=s<=i?1:-1):[s,o,i]=n,o===0)return d("seq: increment must be non-zero");let c=(o>0?s<=i:s>=i)?Math.floor(Math.abs(i-s)/Math.abs(o))+1:0;if(c>at)return d(`seq: range too large: ${c} elements exceeds limit of ${at}`);let a=[];if(o>0)for(let l=s;l<=i;l+=o)a.push(String(l));else for(let l=s;l>=i;l+=o)a.push(String(l));return w($(a))}function ee(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["b"]}),o=n.get("b")??"t";if(o!=="a"&&o!=="t")return d(`nl: -b: only \`a\` and \`t\` are supported (got \`${o}\`)`);let i=E("nl",s,t,r),u=[],c=0;for(let{content:a}of i.inputs)for(let l of v(a))o==="a"||l!==""?(c++,u.push(`${String(c).padStart(6)} ${l}`)):u.push(l);return T($(u),i)}function ne(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["d","f","c"]}),o=n.has("f"),i=n.has("c");if(o===i)return k("cut -f LIST [-d DELIM] [file...] | cut -c LIST [file...]");if(i&&n.has("d"))return d("cut: -d is only valid with -f");let u=re(o?n.get("f"):n.get("c"));if(u.error)return u.error;let c=n.get("d")??" ";if(o&&c.length!==1)return d("cut: -d delimiter must be a single character");let a=E("cut",s,t,r),l=[];for(let{content:f}of a.inputs)for(let h of v(f))l.push(o?se(h,c,u.ranges):ft([...h],u.ranges).join(""));return T($(l),a)}function re(t){let e=[];for(let r of t.split(",")){if(r==="")return{error:d(`cut: empty list item in \`${t}\``)};if(/^\d+$/u.test(r)){let i=Number(r);if(i<1)return{error:d("cut: list items must be >= 1")};e.push([i,i]);continue}let n=r.match(/^(\d*)-(\d*)$/u);if(!n||n[1]===""&&n[2]==="")return{error:d(`cut: invalid list item: ${r}`)};let s=n[1]===""?1:Number(n[1]),o=n[2]===""?Number.POSITIVE_INFINITY:Number(n[2]);if(s<1)return{error:d("cut: list items must be >= 1")};if(o<s)return{error:d(`cut: reversed range: ${r}`)};e.push([s,o])}return{ranges:e}}function ft(t,e){let r=new Set,n=[];for(let[s,o]of e)for(let i=s;i<=Math.min(o,t.length);i++)r.has(i)||(r.add(i),n.push(i));return n.sort((s,o)=>s-o),n.map(s=>t[s-1])}function se(t,e,r){let n=t.split(e);return n.length===1?t:ft(n,r).join(e)}function oe(t,e){let{flags:r,positional:n}=m(e,{short:["d","s"]}),s=r.has("d"),o=r.has("s");if(s&&o)return d("tr: -d combined with -s is not supported");let i=s||o?1:2;if(n.length!==i)return k("tr SET1 SET2 | tr -d SET | tr -s SET");let u=lt(n[0]);if(u.error)return u.error;if(s){let f=new Set(u.chars);return w([...t].filter(h=>!f.has(h)).join(""))}if(o)return w(ie(t,new Set(u.chars)));let c=lt(n[1]);if(c.error)return c.error;if(c.chars.length===0)return d("tr: SET2 must not be empty");let a=new Map,l=c.chars.at(-1);for(let f=0;f<u.chars.length;f++)a.set(u.chars[f],c.chars[f]??l);return w([...t].map(f=>a.get(f)??f).join(""))}function lt(t){let e=[...t],r=[],n=0,s=()=>{if(e[n]!=="\\")return e[n++];if(n+1>=e.length)return null;let o=e[n+1];return n+=2,o==="n"?`
9
+ `:o==="t"?" ":o==="\\"?"\\":o==="0"?"\0":o};for(;n<e.length;){let o=s();if(o===null)return{error:d("tr: trailing backslash in set")};if(e[n]==="-"&&n+1<e.length){n++;let i=s();if(i===null)return{error:d("tr: trailing backslash in set")};let u=o.codePointAt(0),c=i.codePointAt(0);if(c<u)return{error:d(`tr: reversed range: ${o}-${i}`)};for(let a=u;a<=c;a++)r.push(String.fromCodePoint(a))}else r.push(o)}return{chars:r}}function ie(t,e){let r="",n=null;for(let s of t)e.has(s)&&s===n||(r+=s,n=s);return r}function ue(t,e,r){let{positional:n}=m(e);if(n.length===0)return k("which COMMAND...");let s=[],o=0;for(let i of n)r.hasCommand(i)?s.push(`/usr/bin/${i}`):(s.push(`${i} not found`),o=1);return{stdout:$(s),stderr:"",exitCode:o}}function ce(t,e,r){let{positional:n}=m(e);return n.length>0?d(`whoami: extra operand: ${n[0]}`):w((r.user??"user")+`
10
+ `)}function ae(t,e){let{flags:r,positional:n}=m(e,{short:["u"]});if(n.length>1)return d(`date: at most one +FORMAT argument is supported (got: ${n[1]})`);let s="%a %b %e %T %Z %Y";if(n.length===1){if(!n[0].startsWith("+"))return k("date [-u] [+FORMAT]");s=n[0].slice(1)}return w(pe(new Date,s,r.has("u"))+`
11
+ `)}var le=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],fe=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],he=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],de=["January","February","March","April","May","June","July","August","September","October","November","December"];function pe(t,e,r){let n=r?{Y:t.getUTCFullYear(),M:t.getUTCMonth(),D:t.getUTCDate(),h:t.getUTCHours(),m:t.getUTCMinutes(),s:t.getUTCSeconds(),w:t.getUTCDay()}:{Y:t.getFullYear(),M:t.getMonth(),D:t.getDate(),h:t.getHours(),m:t.getMinutes(),s:t.getSeconds(),w:t.getDay()},s=(o,i="0")=>String(o).padStart(2,i);return e.replace(/%./gu,o=>{switch(o[1]){case"Y":return String(n.Y);case"m":return s(n.M+1);case"d":return s(n.D);case"e":return s(n.D," ");case"H":return s(n.h);case"M":return s(n.m);case"S":return s(n.s);case"T":return`${s(n.h)}:${s(n.m)}:${s(n.s)}`;case"F":return`${n.Y}-${s(n.M+1)}-${s(n.D)}`;case"s":return String(Math.floor(t.getTime()/1e3));case"a":return le[n.w];case"A":return fe[n.w];case"b":return he[n.M];case"B":return de[n.M];case"Z":return me(t,r);case"z":return ge(t,r);case"n":return`
12
+ `;case"t":return" ";case"%":return"%";default:return o}})}function me(t,e){if(e)return"UTC";try{return new Intl.DateTimeFormat("en-US",{timeZoneName:"short"}).formatToParts(t).find(s=>s.type==="timeZoneName")?.value??"Local"}catch{return"Local"}}function ge(t,e){if(e)return"+0000";let r=-t.getTimezoneOffset(),n=r>=0?"+":"-",s=Math.abs(r);return`${n}${String(Math.floor(s/60)).padStart(2,"0")}${String(s%60).padStart(2,"0")}`}var ht={cut:ne,tac:Qt,tr:oe,seq:te,nl:ee,which:ue},dt={whoami:ce,date:ae};var mt=/[*?]/u,pt=/[.+*?^${}()|[\]\\]/u;function Y(t){let e="^";for(let r=0;r<t.length;r++){let n=t[r];if(n==="\\"&&r+1<t.length){let s=t[r+1];e+=pt.test(s)?"\\"+s:s,r++}else n==="*"?e+=".*":n==="?"?e+=".":pt.test(n)?e+="\\"+n:e+=n}return new RegExp(e+"$","u")}function gt(t,e,r){if(t.length===0)return[];let n=[t[0]];for(let s=1;s<t.length;s++){let o=t[s];if(e.has(s)||!mt.test(o)){n.push(o);continue}let i=we(o,r);i.length>0?n.push(...i):n.push(o)}return n}function we(t,e){let r=t.startsWith("/"),n=t.startsWith("./"),s=t.endsWith("/")&&t.length>1,o=t.split("/").filter(Boolean);if(o.length===0)return r?["/"]:[];let i=[r?"/":"."];for(let u=0;u<o.length;u++){let c=o[u];if(!mt.test(c)){i=i.map(a=>z(a,c));continue}i=be(i,c,u===o.length-1,e)}return s?i=i.filter(u=>e.fs.isDir(x(e.cwd,u))).map(u=>u==="/"?u:u+"/"):i=i.filter(u=>xe(u,e)),n&&(i=i.map(u=>u.startsWith("./")?u:"./"+u)),i.sort(),i}function xe(t,e){let r=x(e.cwd,t);return e.fs.isFile(r)||e.fs.isDir(r)}function be(t,e,r,n){let s=Y(e),o=e.startsWith("."),i=c=>!o&&c.startsWith(".")?!1:s.test(c),u=[];for(let c of t){let a=x(n.cwd,c);if(!n.fs.isDir(a))continue;let{dirs:l,files:f}=n.fs.listDir(a);for(let h of l)i(h)&&u.push(z(c,h));if(r)for(let h of f)i(h)&&u.push(z(c,h))}return u}function z(t,e){return t==="."?e:A(t,e)}var wt=new Set(["name","type","path","mindepth","maxdepth"]);function xt(t,e,r){let n=Se(e);if(n.error)return n.error;let{starts:s,minDepth:o,maxDepth:i,groups:u,hasExec:c,batches:a}=n,l=[],f="",h="",p=0;for(let g of s){let y=x(r.cwd,g);if(!r.fs.isDir(y)&&!r.fs.isFile(y)){h+=`find: ${g}: no such file or directory
13
+ `,p=1;continue}for(let S of G(r.fs,y,i)){if(S.depth<o)continue;let C=Ae(g,y,S.path),N=Ce(u,{kind:S.kind,path:C},r);f+=N.stdout,h+=N.stderr,N.matched&&!c&&l.push(C)}}for(let g of a){if(g.collected.length===0)continue;let y=g.args.slice(0,-1).concat(g.collected),S=r.dispatch(g.cmd,y,"");f+=S.stdout,h+=S.stderr,S.exitCode!==0&&(p=1)}return{stdout:(l.length===0?"":l.join(`
10
14
  `)+`
11
- `)}function ce(t){let e=le(t);return e.error?e:fe(e.tokens,e.minDepth,e.maxDepth)}function le(t){let e=[],r=0,n=Number.POSITIVE_INFINITY;for(let s=0;s<t.length;s++){let i=t[s],o=i==="-mindepth"||i==="--mindepth"?"mindepth":i==="-maxdepth"||i==="--maxdepth"?"maxdepth":null;if(o){if(s+1>=t.length)return{error:h(`find: -${o} requires a value`)};let u=T(t[s+1],`find: -${o}`);if(u.error)return u;o==="mindepth"?r=u.value:n=u.value,s++;continue}if(i==="--"){e.push(...t.slice(s));break}e.push(i)}return{tokens:e,minDepth:r,maxDepth:n}}function fe(t,e,r){let n=[[]],s=[],i=!1,o=!1,u=!1,c=null;for(let l=0;l<t.length;l++){let f=t[l];if(o){s.push(f);continue}if(f==="-not"||f==="!"){if(i)return{error:h("find: `-not` cannot precede another `-not`")};i=!0,u=!0;continue}if(f==="-a"||f==="-and"||f==="--and"){if(i)return{error:h("find: `-not` must be followed by a primary")};if(c)return{error:h(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:h("find: `-a` with no left-hand expression")};c="-a",u=!0;continue}if(f==="-o"||f==="-or"||f==="--or"){if(i)return{error:h("find: `-not` must be followed by a primary")};if(c)return{error:h(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:h("find: `-o` with no left-hand expression")};n.push([]),c="-o",u=!0;continue}let a=ae(f);if(a!==null){if(l+1>=t.length)return{error:h(`find: ${f} requires a value`)};let d=t[l+1],m=he(a,d);if(m.error)return m;let x={kind:a,value:d,negate:i};(a==="name"||a==="path")&&(x.re=G(d)),n.at(-1).push(x),i=!1,c=null,u=!0,l++;continue}if(f==="--"){o=!0;continue}if(i)return{error:h(`find: \`-not\` must be followed by a primary, got: ${f}`)};if(f.startsWith("-")&&f!=="-"&&!/^-\d/u.test(f))return{error:h(`find: unknown option: ${f}`)};if(u)return{error:h(`find: paths must precede expression: ${f}`)};s.push(f)}return i?{error:h("find: trailing `-not` with no primary")}:c?{error:h(`find: \`${c}\` with no right-hand expression`)}:{starts:s.length>0?s:["."],minDepth:e,maxDepth:r,groups:n}}function ae(t){return/^--?(?:min|max)depth$/u.test(t)?null:t.startsWith("--")&&dt.has(t.slice(2))?t.slice(2):t.startsWith("-")&&dt.has(t.slice(1))?t.slice(1):null}function he(t,e){return t==="type"&&e!=="f"&&e!=="d"?{error:h(`find: -type/--type expects 'f' or 'd', got: ${e}`)}:{}}function de(t,e){return t.some(r=>r.every(n=>pe(n,e)))}function pe(t,e){let r=me(t,e);return t.negate?!r:r}function me(t,e){return t.kind==="type"?t.value==="f"?e.kind==="file":e.kind==="dir":t.kind==="name"?t.re.test(F(e.path)):t.kind==="path"?t.re.test(e.path):!1}function ge(t,e,r){if(r===e)return t;let n=L(e,r);return t.endsWith("/")?t+n:t+"/"+n}function we(t,e,r){return p(e),g(r.cwd+`
12
- `)}function xe(t,e,r){let{positional:n}=p(e),s=n[0]??"/",i=w(r.cwd,s);return r.fs.isDir(i)?(r.cwd=i,g()):h(`cd: not a directory: ${s}`)}function be(t,e,r){let{flags:n,positional:s}=p(Se(e),{short:["l","a"]}),i={long:n.has("l"),all:n.has("a")},o=s.length>0?s:["."],u=[],c=[],l=0;for(let f=0;f<o.length;f++){let a=ve(o[f],o.length>1,i,r);if(a.error){l=1,c.push(a.error);continue}f>0&&o.length>1&&u.push(""),u.push(...a.lines)}return{stdout:u.length===0?"":u.join(`
15
+ `)+f,stderr:h,exitCode:p}}function Se(t){let e=ye(t);if(e.error)return e;let r=ve(e.tokens,e.minDepth,e.maxDepth);if(r.error)return r;let n=[];for(let s of r.groups)for(let o of s)o.kind==="exec"&&n.push(o);return{...r,hasExec:n.length>0,batches:n.filter(s=>s.mode==="batch")}}function ye(t){let e=[],r=0,n=Number.POSITIVE_INFINITY;for(let s=0;s<t.length;s++){let o=t[s],i=o==="-mindepth"||o==="--mindepth"?"mindepth":o==="-maxdepth"||o==="--maxdepth"?"maxdepth":null;if(i){if(s+1>=t.length)return{error:d(`find: -${i} requires a value`)};let u=_(t[s+1],`find: -${i}`);if(u.error)return u;i==="mindepth"?r=u.value:n=u.value,s++;continue}if(o==="--"){e.push(...t.slice(s));break}e.push(o)}return{tokens:e,minDepth:r,maxDepth:n}}function ve(t,e,r){let n=[[]],s=[],o=!1,i=!1,u=!1,c=null;for(let a=0;a<t.length;a++){let l=t[a];if(i){s.push(l);continue}if(l==="-not"||l==="!"){if(o)return{error:d("find: `-not` cannot precede another `-not`")};o=!0,u=!0;continue}if(l==="-a"||l==="-and"||l==="--and"){if(o)return{error:d("find: `-not` must be followed by a primary")};if(c)return{error:d(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:d("find: `-a` with no left-hand expression")};c="-a",u=!0;continue}if(l==="-o"||l==="-or"||l==="--or"){if(o)return{error:d("find: `-not` must be followed by a primary")};if(c)return{error:d(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:d("find: `-o` with no left-hand expression")};n.push([]),c="-o",u=!0;continue}if(l==="-exec"||l==="--exec"){let h=ke(t,a,o);if(h.error)return h;n.at(-1).push(h.pred),o=!1,c=null,u=!0,a=h.nextI;continue}let f=$e(l);if(f!==null){if(a+1>=t.length)return{error:d(`find: ${l} requires a value`)};let h=t[a+1],p=Te(f,h);if(p.error)return p;let b={kind:f,value:h,negate:o};(f==="name"||f==="path")&&(b.re=Y(h)),n.at(-1).push(b),o=!1,c=null,u=!0,a++;continue}if(l==="--"){i=!0;continue}if(o)return{error:d(`find: \`-not\` must be followed by a primary, got: ${l}`)};if(l.startsWith("-")&&l!=="-"&&!/^-\d/u.test(l))return{error:d(`find: unknown option: ${l}`)};if(u)return{error:d(`find: paths must precede expression: ${l}`)};s.push(l)}return o?{error:d("find: trailing `-not` with no primary")}:c?{error:d(`find: \`${c}\` with no right-hand expression`)}:{starts:s.length>0?s:["."],minDepth:e,maxDepth:r,groups:n}}function $e(t){return/^--?(?:min|max)depth$/u.test(t)?null:t.startsWith("--")&&wt.has(t.slice(2))?t.slice(2):t.startsWith("-")&&wt.has(t.slice(1))?t.slice(1):null}function Te(t,e){return t==="type"&&e!=="f"&&e!=="d"?{error:d(`find: -type/--type expects 'f' or 'd', got: ${e}`)}:{}}function ke(t,e,r){let n=e+1;for(;n<t.length&&t[n]!==";"&&t[n]!=="+";)n++;if(n>=t.length)return{error:d("find: -exec: missing terminator (`;` or `+`); use `';'` (quoted) \u2014 bare `\\;` is consumed by the shell as a step separator")};if(n===e+1)return{error:d("find: -exec: requires a command")};let s=t.slice(e+1,n),o=t[n]===";"?"each":"batch";if(o==="batch"){if(s.at(-1)!=="{}")return{error:d("find: -exec ... +: `{}` must be the last argument before `+`")};if(s.slice(0,-1).includes("{}"))return{error:d("find: -exec ... +: only one instance of `{}` is supported")};if(r)return{error:d("find: `-not -exec ... +` is not supported (the `+` form has no meaningful negation)")}}let i={kind:"exec",mode:o,cmd:s[0],args:s.slice(1),negate:r};return o==="batch"&&(i.collected=[]),{pred:i,nextI:n}}function Ce(t,e,r){let n="",s="",o=!1;for(let i of t){let u=!0;for(let c of i){let a=Ee(c,e,r);if(n+=a.stdout,s+=a.stderr,!a.matched){u=!1;break}}if(u){o=!0;break}}return{matched:o,stdout:n,stderr:s}}function Ee(t,e,r){let n=_e(t,e,r);return{...n,matched:t.negate?!n.matched:n.matched}}function _e(t,e,r){return t.kind==="type"?O(t.value==="f"?e.kind==="file":e.kind==="dir"):t.kind==="name"?O(t.re.test(D(e.path))):t.kind==="path"?O(t.re.test(e.path)):Me(t,e,r)}function O(t){return{matched:t,stdout:"",stderr:""}}function Me(t,e,r){if(t.mode==="batch")return t.collected.push(e.path),O(!0);let n=t.args.map(o=>o.replaceAll("{}",e.path)),s=r.dispatch(t.cmd,n,"");return{matched:s.exitCode===0,stdout:s.stdout,stderr:s.stderr}}function Ae(t,e,r){if(r===e)return t;let n=L(e,r);return t.endsWith("/")?t+n:t+"/"+n}function De(t,e,r){return m(e),w(r.cwd+`
16
+ `)}function Ne(t,e,r){let{positional:n}=m(e),s=n[0]??"/",o=x(r.cwd,s);return r.fs.isDir(o)?(r.cwd=o,w()):d(`cd: not a directory: ${s}`)}function Fe(t,e,r){let{flags:n,positional:s}=m(Le(e),{short:["l","a","R"]}),o={long:n.has("l"),all:n.has("a"),recursive:n.has("R")},i=s.length>0?s:["."],u=o.recursive?Ie(i,o.all,r):i,c=[],a=[],l=0;for(let f=0;f<u.length;f++){let h=Re(u[f],u.length>1,o,r);if(h.error){l=1,a.push(h.error);continue}f>0&&u.length>1&&c.push(""),c.push(...h.lines)}return{stdout:c.length===0?"":c.join(`
13
17
  `)+`
14
- `,stderr:c.length===0?"":c.join(`
18
+ `,stderr:a.length===0?"":a.join(`
15
19
  `)+`
16
- `,exitCode:l}}function Se(t){let e=[],r=!1;for(let n of t){if(r){e.push(n);continue}if(n==="--"){e.push(n),r=!0;continue}if(n!=="-1"){if(/^-[a-zA-Z0-9]+$/u.test(n)&&n.includes("1")&&/[a-zA-Z]/u.test(n)){e.push("-"+n.slice(1).replaceAll("1",""));continue}e.push(n)}}return e}function ve(t,e,r,n){let s=w(n.cwd,t);if(n.fs.isFile(s))return{lines:[z(t,n.fs.readFile(s).length,!1,r.long)]};if(!n.fs.isDir(s))return{error:`ls: ${t}: no such file or directory`};let i=e?[`${t}:`]:[],{dirs:o,files:u}=n.fs.listDir(s);for(let c of o)!r.all&&c.startsWith(".")||i.push(z(c,0,!0,r.long));for(let c of u){if(!r.all&&c.startsWith("."))continue;let l=A(s,c);i.push(z(c,n.fs.readFile(l).length,!1,r.long))}return{lines:i}}function z(t,e,r,n){let s=t+(r?"/":"");return n?`${r?"d":"-"} ${String(e).padStart(8)} ${s}`:s}function ye(t,e,r){let{positional:n}=p(e),s=n[0]??".",i=w(r.cwd,s);if(!r.fs.isDir(i))return h(`tree: ${s}: not a directory`);let o=[s];return $e(r.fs,i,o),g(o.join(`
20
+ `,exitCode:l}}function Ie(t,e,r){let n=[];for(let s of t){let o=x(r.cwd,s);if(!r.fs.isDir(o)){n.push(s);continue}let i=[{display:s,abs:o}];for(;i.length>0;){let{display:u,abs:c}=i.pop();n.push(u);let{dirs:a}=r.fs.listDir(c);for(let l=a.length-1;l>=0;l--){let f=a[l];!e&&f.startsWith(".")||i.push({display:We(u,f),abs:A(c,f)})}}}return n}function We(t,e){return t.endsWith("/")?t+e:t+"/"+e}function Le(t){let e=[],r=!1;for(let n of t){if(r){e.push(n);continue}if(n==="--"){e.push(n),r=!0;continue}if(n!=="-1"){if(/^-[a-zA-Z0-9]+$/u.test(n)&&n.includes("1")&&/[a-zA-Z]/u.test(n)){e.push("-"+n.slice(1).replaceAll("1",""));continue}e.push(n)}}return e}function Re(t,e,r,n){let s=x(n.cwd,t);if(n.fs.isFile(s))return{lines:[X(t,n.fs.readFile(s).length,!1,r.long)]};if(!n.fs.isDir(s))return{error:`ls: ${t}: no such file or directory`};let o=e||r.recursive?[`${t}:`]:[];r.all&&(o.push(bt(".",r.long)),o.push(bt("..",r.long)));let{dirs:i,files:u}=n.fs.listDir(s);for(let c of i)!r.all&&c.startsWith(".")||o.push(X(c,0,!0,r.long));for(let c of u){if(!r.all&&c.startsWith("."))continue;let a=A(s,c);o.push(X(c,n.fs.readFile(a).length,!1,r.long))}return{lines:o}}function bt(t,e){return e?`d ${String(0).padStart(8)} ${t}`:t}function X(t,e,r,n){let s=t+(r?"/":"");return n?`${r?"d":"-"} ${String(e).padStart(8)} ${s}`:s}function Oe(t,e,r){let{positional:n}=m(e),s=n[0]??".",o=x(r.cwd,s);if(!r.fs.isDir(o))return d(`tree: ${s}: not a directory`);let i=[s];return je(r.fs,o,i),w(i.join(`
17
21
  `)+`
18
- `)}function $e(t,e,r){let n=[{dir:e,prefix:"",items:mt(t,e),i:0}];for(;n.length>0;){let s=n.at(-1);if(s.i>=s.items.length){n.pop();continue}let{n:i,isDir:o}=s.items[s.i],u=s.i===s.items.length-1;if(r.push(s.prefix+(u?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ")+i+(o?"/":"")),s.i++,!o)continue;let c=A(s.dir,i);n.push({dir:c,prefix:s.prefix+(u?" ":"\u2502 "),items:mt(t,c),i:0})}}function mt(t,e){let{dirs:r,files:n}=t.listDir(e);return[...r.map(s=>({n:s,isDir:!0})),...n.map(s=>({n:s,isDir:!1}))]}function ke(t,e){let{positional:r}=p(e);return r.length===0?k("basename PATH"):g(F(r[0])+`
19
- `)}function Ee(t,e){let{positional:r}=p(e);return r.length===0?k("dirname PATH"):g(I(r[0])+`
20
- `)}var gt={pwd:we,cd:xe,ls:be,find:pt,tree:ye,basename:ke,dirname:Ee};var Te=/^(\d+)(?:,(\d+))?p$/u;function wt(t,e,r){let n;try{n=p(e,{short:["n"]})}catch{return X()}let{flags:s,positional:i}=n;if(!s.has("n")||i.length===0)return X();let o=Te.exec(i[0]);if(!o)return X();let u=Number(o[1]),c=o[2]===void 0?u:Number(o[2]);if(u<1||c<1)return h("sed: line numbers must be >= 1");if(c<u)return h(`sed: reversed range: ${u},${c}`);let l=i.slice(1);if(l.length>1)return h("sed: at most one input file is supported");let f=E("sed",l,t,r),a=f.inputs[0]?.content??"",m=S(a).slice(u-1,c);return y(m.length>0?m.join(`
22
+ `)}function je(t,e,r){let n=[{dir:e,prefix:"",items:St(t,e),i:0}];for(;n.length>0;){let s=n.at(-1);if(s.i>=s.items.length){n.pop();continue}let{n:o,isDir:i}=s.items[s.i],u=s.i===s.items.length-1;if(r.push(s.prefix+(u?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ")+o+(i?"/":"")),s.i++,!i)continue;let c=A(s.dir,o);n.push({dir:c,prefix:s.prefix+(u?" ":"\u2502 "),items:St(t,c),i:0})}}function St(t,e){let{dirs:r,files:n}=t.listDir(e);return[...r.map(s=>({n:s,isDir:!0})),...n.map(s=>({n:s,isDir:!1}))]}function qe(t,e){let{positional:r}=m(e);return r.length===0?k("basename PATH"):w(D(r[0])+`
23
+ `)}function Pe(t,e){let{positional:r}=m(e);return r.length===0?k("dirname PATH"):w(W(r[0])+`
24
+ `)}var yt={pwd:De,cd:Ne,ls:Fe,find:xt,tree:Oe,basename:qe,dirname:Pe};var He=/^(\d+)(?:,(\d+))?p$/u;function vt(t,e,r){let n;try{n=m(e,{short:["n"]})}catch{return j()}let{flags:s,positional:o}=n;if(!s.has("n")||o.length===0)return j();let i=Be(o[0]);if(i.error)return i.error;let{ranges:u}=i;if(u.length===0)return j();let c=o.slice(1),a=E("sed",c,t,r),l=a.inputs.flatMap(h=>v(h.content)),f=[];for(let h=0;h<l.length;h++){let p=h+1;for(let{start:b,end:g}of u)p>=b&&p<=g&&f.push(l[h])}return T(f.length>0?f.join(`
21
25
  `)+`
22
- `:"",f)}function X(){return h("sed: only `-n 'X[,Y]p'` (line range print) is supported")}function xt(t,e){let r=t[e+1];if(r===void 0)return-1;if("^$\\.*+?()[]{}|/bBdDsSwW".includes(r)||r>="1"&&r<="9")return 2;let n=i=>i!==void 0&&/[0-9A-Fa-f]/u.test(i),s=(i,o)=>{if(t[e+2]!==i)return 0;let u=t.indexOf(o,e+3);return u>e+3?u-e+1:0};if(r==="x")return n(t[e+2])&&n(t[e+3])?4:0;if(r==="u"){if(t[e+2]==="{"){let i=s("{","}"),o=i?t.slice(e+3,e+i-1):"";return o.length>=1&&o.length<=6&&[...o].every(n)&&parseInt(o,16)<=1114111?i:0}return[2,3,4,5].every(i=>n(t[e+i]))?6:0}return r==="p"||r==="P"?s("{","}"):r==="c"?/[A-Za-z]/u.test(t[e+2]??"")?3:0:r==="k"?s("<",">"):0}function St(t){let e="(){}+?|",r="",n=!1;for(let s=0;s<t.length;s++){let i=t[s];if(n){if(i==="\\"){let o=xt(t,s);if(o===-1)return{error:"trailing backslash (\\)"};if(o>0){r+=t.slice(s,s+o),s+=o-1;continue}r+=t[s+1],s++;continue}r+=i,i==="]"&&(n=!1);continue}if(i==="["){r+=i,n=!0,t[s+1]==="^"&&(r+="^",s++),t[s+1]==="]"&&(r+="\\]",s++);continue}if(i==="\\"){if(s+1>=t.length)return{error:"trailing backslash (\\)"};let o=t[s+1];if(e.includes(o)){r+=o,s++;continue}if(o==="<"||o===">"){r+="\\b",s++;continue}let u=xt(t,s);if(u>0){r+=t.slice(s,s+u),s+=u-1;continue}r+=o,s++;continue}if(i==="*"&&(s===0||t[s-1]==="^"&&bt(t,s-1))){r+="\\*";continue}if(i==="^"&&!bt(t,s)){r+="\\^";continue}if(i==="$"&&!(s===t.length-1||t[s+1]==="\\"&&(t[s+2]===")"||t[s+2]==="|"))){r+="\\$";continue}if(e.includes(i)){r+="\\"+i;continue}r+=i}return{source:r}}function bt(t,e){return e===0?!0:e>=2&&t[e-2]==="\\"&&(t[e-1]==="("||t[e-1]==="|")}var vt="[-i] [-v] [-n] [-r] [-w] [-o] [-E|-F|-G] [-l] [-L] [-c] [-h] [-H] [-A N] [-B N] [-C N]",_e=`grep ${vt} PATTERN [PATH...]
23
- or: grep ${vt} -e PATTERN ... [PATH...]`,Ce=["i","v","n","r","l","L","c","w","h","H","o","E","F","G"],Ne=["A","B","C"];function $t(t,e,r){let n;try{n=p(e,{short:Ce,valueShort:Ne,repeatable:["e"]})}catch(Ot){return h(`grep: ${Ot.message}`,2)}let{flags:s,values:i,positional:o}=n,u=i.get("e")??[],c,l;if(u.length>0)c=u,l=o;else if(o.length>0)c=[o[0]],l=o.slice(1);else return k(_e);let f=Ae(s);if(f)return f;let a=Fe(c,s);if(a.error)return a.error;let d=De(i);if(d.error)return d.error;let m=s.has("r"),x=Ie(m,t,l,r),b=Me(s,m,l.length),_=s.has("v"),$={showName:b,invert:_,showLine:s.has("n"),only:s.has("o"),after:d.after,before:d.before},N=s.has("l")?yt(x.inputs,a.res,_,!1):s.has("L")?yt(x.inputs,a.res,_,!0):s.has("c")?Oe(x.inputs,a.res,_,b):Re(x.inputs,a.res,$);return x.failed?{stdout:N.stdout,stderr:x.stderr+N.stderr,exitCode:2}:N}function Ae(t){if(t.has("h")&&t.has("H"))return h("grep: -h and -H are mutually exclusive");let e=["l","L","c"].filter(n=>t.has(n));if(e.length>1)return h(`grep: ${e.map(n=>`-${n}`).join(" / ")} are mutually exclusive`);let r=["E","F","G"].filter(n=>t.has(n));return r.length>1?h(`grep: ${r.map(n=>`-${n}`).join(" / ")} are mutually exclusive`):null}function Fe(t,e){let r=[],n=e.has("i")?"iu":"u";for(let s of t){let i;if(e.has("F"))i=RegExp.escape(s);else if(e.has("E"))i=s;else{let o=St(s);if(o.error)return{error:h(`grep: ${o.error}`,2)};i=o.source}e.has("w")&&(i=`\\b(?:${i})\\b`);try{r.push(new RegExp(i,n))}catch(o){let u=e.has("F")?`fixed-string /${n}`:e.has("E")?`ERE / ECMAScript /${n}`:`BRE /${n}`;return{error:h(`grep: invalid pattern (${u}): ${o.message}`,2)}}}return{res:r}}function R(t,e){return t.some(r=>r.test(e))}var V=()=>({stdout:"",stderr:"",exitCode:1});function De(t){let e=t.get("C"),r=e===void 0?null:T(e,"grep: -C");if(r?.error)return r;let n=T(t.get("A")??e??"0","grep: -A");if(n.error)return n;let s=T(t.get("B")??e??"0","grep: -B");return s.error?s:{after:n.value,before:s.value}}function Me(t,e,r){return t.has("h")?!1:t.has("H")?!0:e||r>1}function Ie(t,e,r,n){return t?Le("grep",r.length>0?r:["."],n):r.length>0?B("grep",r,n):{inputs:[{name:null,content:e}],stderr:"",failed:!1}}function Le(t,e,r){let n=[],s="",i=!1;for(let o of e){let u=w(r.cwd,o);if(r.fs.isFile(u)){n.push({name:o,content:r.fs.readFile(u)});continue}if(!r.fs.isDir(u)){s+=`${t}: ${o}: no such file or directory
24
- `,i=!0;continue}for(let c of r.fs.walkFiles(u))n.push({name:We(o,u,c),content:r.fs.readFile(c)})}return{inputs:n,stderr:s,failed:i}}function We(t,e,r){let n=L(e,r);return t==="."?n:t.endsWith("/")?t+n:t+"/"+n}function Re(t,e,r){let n=[],s=!1;for(let{name:o,content:u}of t){let c=S(u),l=je(c,e,o,r);l.matched&&(s=!0),l.lines.length>0&&n.push(l.lines.join(`
25
- `))}let i=n.join(`
26
- `);return s?g(i+(i?`
27
- `:"")):V()}function je(t,e,r,n){let{invert:s,after:i,before:o,only:u}=n,c=o>0||i>0,l=[],f=!1,a=-1;for(let d=0;d<t.length;d++){if(R(e,t[d])===s)continue;if(f=!0,u){l.push(...qe(t[d],r,d+1,e,n));continue}let m=Math.max(0,d-o),x=Math.min(t.length-1,d+i);c&&a>=0&&m>a+1&&l.push("--");for(let b=Math.max(m,a+1);b<=x;b++){let _=b===d||R(e,t[b])!==s;l.push(kt(t[b],r,b+1,_,n))}a=x}return{lines:l,matched:f}}function qe(t,e,r,n,s){let i=[];for(let c of n){let l=new RegExp(c.source,c.flags+"g");for(let f of t.matchAll(l))f[0].length>0&&i.push({index:f.index,text:f[0]})}i.sort((c,l)=>c.index-l.index||l.text.length-c.text.length);let o=[],u=0;for(let c of i)c.index<u||(o.push(kt(c.text,e,r,!0,s)),u=c.index+c.text.length);return o}function kt(t,e,r,n,s){let{showName:i,showLine:o}=s,u=n?":":"-",c=[];return i&&c.push(e??"(standard input)"),o&&c.push(String(r)),c.length>0?c.join(u)+u+t:t}function yt(t,e,r,n){let s=[];for(let{name:i,content:o}of t){let c=S(o).some(l=>R(e,l)!==r);(n?!c:c)&&s.push(i??"(standard input)")}return s.length>0?g(v(s)):V()}function Oe(t,e,r,n){let s=[],i=!1;for(let{name:o,content:u}of t){let c=S(u),l=0;for(let f of c)R(e,f)!==r&&l++;l>0&&(i=!0),n?s.push(`${o??"(standard input)"}:${l}`):s.push(String(l))}return s.length===0?V():{stdout:v(s),stderr:"",exitCode:i?0:1}}function Pe(t,e,r){let{flags:n,positional:s}=p(e,{short:["n"]}),i=W("cat",s,t,r);return y(n.has("n")?Be(i.content):i.content,i)}function Be(t){if(t==="")return"";let e=t.endsWith(`
26
+ `:"",a)}function Be(t){let e=[];for(let r of t.split(";")){if(r==="")continue;let n=He.exec(r);if(!n)return{error:j()};let s=Number(n[1]),o=n[2]===void 0?s:Number(n[2]);if(s<1)return{error:d("sed: line numbers must be >= 1")};if(o<s)return{error:d(`sed: reversed range: ${s},${o}`)};e.push({start:s,end:o})}return{ranges:e}}function j(){return d("sed: only `-n 'X[,Y]p'` (optionally `;`-joined into multi-range scripts) is supported")}function $t(t,e){let r=t[e+1];if(r===void 0)return-1;if("^$\\.*+?()[]{}|/bBdDsSwW".includes(r)||r>="1"&&r<="9")return 2;let n=o=>o!==void 0&&/[0-9A-Fa-f]/u.test(o),s=(o,i)=>{if(t[e+2]!==o)return 0;let u=t.indexOf(i,e+3);return u>e+3?u-e+1:0};if(r==="x")return n(t[e+2])&&n(t[e+3])?4:0;if(r==="u"){if(t[e+2]==="{"){let o=s("{","}"),i=o?t.slice(e+3,e+o-1):"";return i.length>=1&&i.length<=6&&[...i].every(n)&&parseInt(i,16)<=1114111?o:0}return[2,3,4,5].every(o=>n(t[e+o]))?6:0}return r==="p"||r==="P"?s("{","}"):r==="c"?/[A-Za-z]/u.test(t[e+2]??"")?3:0:r==="k"?s("<",">"):0}function kt(t){let e="(){}+?|",r="",n=!1;for(let s=0;s<t.length;s++){let o=t[s];if(n){if(o==="\\"){let i=$t(t,s);if(i===-1)return{error:"trailing backslash (\\)"};if(i>0){r+=t.slice(s,s+i),s+=i-1;continue}r+=t[s+1],s++;continue}r+=o,o==="]"&&(n=!1);continue}if(o==="["){r+=o,n=!0,t[s+1]==="^"&&(r+="^",s++),t[s+1]==="]"&&(r+="\\]",s++);continue}if(o==="\\"){if(s+1>=t.length)return{error:"trailing backslash (\\)"};let i=t[s+1];if(e.includes(i)){r+=i,s++;continue}if(i==="<"||i===">"){r+="\\b",s++;continue}let u=$t(t,s);if(u>0){r+=t.slice(s,s+u),s+=u-1;continue}r+=i,s++;continue}if(o==="*"&&(s===0||t[s-1]==="^"&&Tt(t,s-1))){r+="\\*";continue}if(o==="^"&&!Tt(t,s)){r+="\\^";continue}if(o==="$"&&!(s===t.length-1||t[s+1]==="\\"&&(t[s+2]===")"||t[s+2]==="|"))){r+="\\$";continue}if(e.includes(o)){r+="\\"+o;continue}r+=o}return{source:r}}function Tt(t,e){return e===0?!0:e>=2&&t[e-2]==="\\"&&(t[e-1]==="("||t[e-1]==="|")}var Ct="[-i] [-v] [-n] [-r|-R] [-w] [-o] [-E|-F|-G] [-l] [-L] [-c] [-h] [-H] [-A N] [-B N] [-C N]",Ge=`grep ${Ct} PATTERN [PATH...]
27
+ or: grep ${Ct} -e PATTERN ... [PATH...]`,Ue=["i","v","n","r","R","l","L","c","w","h","H","o","E","F","G"],ze=["A","B","C"];function _t(t,e,r){let n;try{n=m(e,{short:Ue,valueShort:ze,repeatable:["e"]})}catch(N){return d(`grep: ${N.message}`,2)}let{flags:s,values:o,positional:i}=n,u=o.get("e")??[],c,a;if(u.length>0)c=u,a=i;else if(i.length>0)c=[i[0]],a=i.slice(1);else return k(Ge);let l=Ye(s);if(l)return l;let f=Xe(c,s);if(f.error)return f.error;let h=Ve(o);if(h.error)return h.error;let p=s.has("r")||s.has("R"),b=Je(p,t,a,r),g=Ze(s,p,a.length),y=s.has("v"),S={showName:g,invert:y,showLine:s.has("n"),only:s.has("o"),after:h.after,before:h.before},C=s.has("l")?Et(b.inputs,f.res,y,!1):s.has("L")?Et(b.inputs,f.res,y,!0):s.has("c")?rn(b.inputs,f.res,y,g):tn(b.inputs,f.res,S);return b.failed?{stdout:C.stdout,stderr:b.stderr+C.stderr,exitCode:2}:C}function Ye(t){if(t.has("h")&&t.has("H"))return d("grep: -h and -H are mutually exclusive");let e=["l","L","c"].filter(n=>t.has(n));if(e.length>1)return d(`grep: ${e.map(n=>`-${n}`).join(" / ")} are mutually exclusive`);let r=["E","F","G"].filter(n=>t.has(n));return r.length>1?d(`grep: ${r.map(n=>`-${n}`).join(" / ")} are mutually exclusive`):null}function Xe(t,e){let r=[],n=e.has("i")?"iu":"u";for(let s of t){let o;if(e.has("F"))o=RegExp.escape(s);else if(e.has("E"))o=s;else{let i=kt(s);if(i.error)return{error:d(`grep: ${i.error}`,2)};o=i.source}e.has("w")&&(o=`\\b(?:${o})\\b`);try{r.push(new RegExp(o,n))}catch(i){let u=e.has("F")?`fixed-string /${n}`:e.has("E")?`ERE / ECMAScript /${n}`:`BRE /${n}`;return{error:d(`grep: invalid pattern (${u}): ${i.message}`,2)}}}return{res:r}}function q(t,e){return t.some(r=>r.test(e))}var V=()=>({stdout:"",stderr:"",exitCode:1});function Ve(t){let e=t.get("C"),r=e===void 0?null:_(e,"grep: -C");if(r?.error)return r;let n=_(t.get("A")??e??"0","grep: -A");if(n.error)return n;let s=_(t.get("B")??e??"0","grep: -B");return s.error?s:{after:n.value,before:s.value}}function Ze(t,e,r){return t.has("h")?!1:t.has("H")?!0:e||r>1}function Je(t,e,r,n){return t?Ke("grep",r.length>0?r:["."],n):r.length>0?U("grep",r,n):{inputs:[{name:null,content:e}],stderr:"",failed:!1}}function Ke(t,e,r){let n=[],s="",o=!1;for(let i of e){let u=x(r.cwd,i);if(r.fs.isFile(u)){n.push({name:i,content:r.fs.readFile(u)});continue}if(!r.fs.isDir(u)){s+=`${t}: ${i}: no such file or directory
28
+ `,o=!0;continue}for(let c of r.fs.walkFiles(u))n.push({name:Qe(i,u,c),content:r.fs.readFile(c)})}return{inputs:n,stderr:s,failed:o}}function Qe(t,e,r){let n=L(e,r);return t==="."?n:t.endsWith("/")?t+n:t+"/"+n}function tn(t,e,r){let n=[],s=!1;for(let{name:i,content:u}of t){let c=v(u),a=en(c,e,i,r);a.matched&&(s=!0),a.lines.length>0&&n.push(a.lines.join(`
29
+ `))}let o=n.join(`
30
+ `);return s?w(o+(o?`
31
+ `:"")):V()}function en(t,e,r,n){let{invert:s,after:o,before:i,only:u}=n,c=i>0||o>0,a=[],l=!1,f=-1;for(let h=0;h<t.length;h++){if(q(e,t[h])===s)continue;if(l=!0,u){a.push(...nn(t[h],r,h+1,e,n));continue}let p=Math.max(0,h-i),b=Math.min(t.length-1,h+o);c&&f>=0&&p>f+1&&a.push("--");for(let g=Math.max(p,f+1);g<=b;g++){let y=g===h||q(e,t[g])!==s;a.push(Mt(t[g],r,g+1,y,n))}f=b}return{lines:a,matched:l}}function nn(t,e,r,n,s){let o=[];for(let c of n){let a=new RegExp(c.source,c.flags+"g");for(let l of t.matchAll(a))l[0].length>0&&o.push({index:l.index,text:l[0]})}o.sort((c,a)=>c.index-a.index||a.text.length-c.text.length);let i=[],u=0;for(let c of o)c.index<u||(i.push(Mt(c.text,e,r,!0,s)),u=c.index+c.text.length);return i}function Mt(t,e,r,n,s){let{showName:o,showLine:i}=s,u=n?":":"-",c=[];return o&&c.push(e??"(standard input)"),i&&c.push(String(r)),c.length>0?c.join(u)+u+t:t}function Et(t,e,r,n){let s=[];for(let{name:o,content:i}of t){let c=v(i).some(a=>q(e,a)!==r);(n?!c:c)&&s.push(o??"(standard input)")}return s.length>0?w($(s)):V()}function rn(t,e,r,n){let s=[],o=!1;for(let{name:i,content:u}of t){let c=v(u),a=0;for(let l of c)q(e,l)!==r&&a++;a>0&&(o=!0),n?s.push(`${i??"(standard input)"}:${a}`):s.push(String(a))}return s.length===0?V():{stdout:$(s),stderr:"",exitCode:o?0:1}}function sn(t,e,r){let{flags:n,positional:s}=m(e,{short:["n"]}),o=R("cat",s,t,r);return T(n.has("n")?on(o.content):o.content,o)}function on(t){if(t==="")return"";let e=t.endsWith(`
28
32
  `)?`
29
33
  `:"";return(e?t.slice(0,-1).split(`
30
34
  `):t.split(`
31
35
  `)).map((n,s)=>`${String(s+1).padStart(6)} ${n}`).join(`
32
- `)+e}function He(t,e,r){let{values:n,positional:s}=p(e,{valueShort:["n"]});Ct(n,s);let i=T(n.get("n")??"10","head: -n");return i.error?i.error:Nt("head",t,s,r,o=>o.slice(0,i.value))}function Ge(t,e,r){let{values:n,positional:s}=p(e,{valueShort:["n"]});Ct(n,s);let i=T(n.get("n")??"10","tail: -n");return i.error?i.error:Nt("tail",t,s,r,o=>i.value===0?[]:o.slice(-i.value))}function Ct(t,e){t.has("n")||e[0]&&/^-\d+$/u.test(e[0])&&t.set("n",e.shift().slice(1))}function Nt(t,e,r,n,s){let i=E(t,r,e,n),o=i.inputs.length>1,u=[];for(let c=0;c<i.inputs.length;c++){let{name:l,content:f}=i.inputs[c],a=s(S(f)).join(`
33
- `);o?u.push(`${c>0?`
34
- `:""}==> ${l} <==
35
- ${a}${a?`
36
- `:""}`):a.length>0&&u.push(a+`
37
- `)}return y(u.join(""),i)}function ze(t,e,r){let{flags:n,positional:s}=p(e,{short:["l","w","c"]}),i=Xe(n),o=E("wc",s,t,r),u=[],c={l:0,w:0,c:0};for(let{name:l,content:f}of o.inputs){let a=Ue(f);u.push(Et(a,l,i)),c.l+=a.l,c.w+=a.w,c.c+=a.c}return o.inputs.length>1&&u.push(Et(c,"total",i)),y(v(u),o)}function Xe(t){return t.has("l")||t.has("w")||t.has("c")?{l:t.has("l"),w:t.has("w"),c:t.has("c")}:{l:!0,w:!0,c:!0}}var Ve=new TextEncoder;function Ue(t){return{l:(t.match(/\n/gu)??[]).length,w:(t.match(/\S+/gu)??[]).length,c:Ve.encode(t).length}}function Et(t,e,r){let n=[];return r.l&&n.push(String(t.l).padStart(7)),r.w&&n.push(String(t.w).padStart(7)),r.c&&n.push(String(t.c).padStart(7)),n.join(" ")+(e?" "+e:"")}function Ke(t,e,r){let{flags:n,positional:s}=p(e,{short:["n","r","u"]}),i=W("sort",s,t,r),o=S(i.content),u=n.has("n"),c=n.has("u");if(u){let l=o.map(f=>({line:f,key:Tt(f)}));l.sort(c?(f,a)=>f.key-a.key:(f,a)=>f.key-a.key||(f.line<a.line?-1:f.line>a.line?1:0)),o=l.map(f=>f.line)}else o.sort();if(c){let l=new Set;o=o.filter(f=>{let a=u?Tt(f):f;return l.has(a)?!1:(l.add(a),!0)})}return n.has("r")&&o.reverse(),y(v(o),i)}function Tt(t){let e=/^[ \t]*(-?(?:\d+\.?\d*|\.\d+))/u.exec(t);return e?Number(e[1]):0}function Ye(t,e,r){let{flags:n,positional:s}=p(e,{short:["c","d","u","i"]}),i=W("uniq",s,t,r),o=n.has("c"),u=n.has("d"),c=n.has("u"),l=n.has("i"),f=$=>l?$.toLowerCase():$,a=S(i.content),d=[],m=null,x=null,b=0,_=()=>{if(m===null)return;let $=b>=2;!(u&&c)&&(u?$:!c||!$)&&d.push(o?`${String(b).padStart(7)} ${m}`:m)};for(let $ of a){let N=f($);if(N===x){b++;continue}_(),m=$,x=N,b=1}return _(),y(v(d),i)}function Ze(t,e){let{flags:r,positional:n}=p(e,{short:["n","e","E"]}),s=n.join(" "),i=!r.has("n");if(r.has("e")){let o=Qe(s);s=o.text,o.stop&&(i=!1)}return g(i?s+`
38
- `:s)}function Qe(t){let e={a:7,b:8,e:27,f:12,n:10,r:13,t:9,v:11,"\\":92},r="";for(let n=0;n<t.length;n++){if(t[n]!=="\\"||n+1>=t.length){r+=t[n];continue}let s=t[++n];if(s==="c")return{text:r,stop:!0};if(s in e){r+=String.fromCodePoint(e[s]);continue}if(s==="0"){let i="";for(;i.length<3&&/[0-7]/u.test(t[n+1]??"");)i+=t[++n];r+=String.fromCodePoint(i===""?0:parseInt(i,8));continue}if(s==="x"&&/[0-9a-fA-F]/u.test(t[n+1]??"")){let i="";for(;i.length<2&&/[0-9a-fA-F]/u.test(t[n+1]??"");)i+=t[++n];r+=String.fromCodePoint(parseInt(i,16));continue}r+="\\"+s}return{text:r,stop:!1}}function Je(t,e,r){let{flags:n,values:s,positional:i}=p(e,{short:["r"],valueShort:["n"],stopAtFirstPositional:!0}),[o="echo",...u]=i,c=t.split(/\s+/u).filter(Boolean);if(c.length===0)return n.has("r")?g():r.dispatch(o,u,"");let l=s.has("n")?T(s.get("n"),"xargs: -n"):{value:c.length};return l.error?l.error:s.has("n")&&l.value===0?h("xargs: -n: must be at least 1"):tn(r,o,u,c,l.value)}function tn(t,e,r,n,s){let i="",o="",u=0;for(let c=0;c<n.length;c+=s){let l=t.dispatch(e,[...r,...n.slice(c,c+s)],"");i+=l.stdout,o+=l.stderr,l.exitCode!==0&&(u=l.exitCode)}return{stdout:i,stderr:o,exitCode:u}}function _t(){return g()}function en(){return{stdout:"",stderr:"",exitCode:1}}var At={cat:Pe,grep:$t,head:He,tail:Ge,wc:ze,sort:Ke,uniq:Ye,echo:Ze,xargs:Je},Ft={true:_t,false:en,":":_t};function Dt(t,e,r){let{index:n,pipe:s}=rn(t),i=t.slice(n),o=sn(i),u=i.slice(o),c=i.slice(0,o).trim(),l=c==="",f=l?"":r.resolveCommand(c.split(/\s+/u)[0]),a=t.slice(0,n+o),d=s&&a.endsWith("|")?" ":"";return nn(u,l,s,f,e,r).map(m=>a+d+m)}function nn(t,e,r,n,s,i){if(!e)return r?[]:on(t,s,n==="cd");let o=r?i.pipeNames:i.names;for(let u of i.binPrefixes)if(t.startsWith(u)){let c=t.slice(u.length);return o.filter(l=>l.startsWith(c)).map(l=>u+l)}return t.startsWith("/")||t.startsWith("./")?[]:o.filter(u=>u.startsWith(t))}function rn(t){let e=0,r=!1,n=0;for(;n<t.length;){let s=t[n];if(s==="|"){let i=t[n+1]==="|";n+=i?2:1,e=n,r=!i}else s==="&"&&t[n+1]==="&"?(n+=2,e=n,r=!1):s===";"?(n++,e=n,r=!1):n++}return{index:e,pipe:r}}function sn(t){for(let e=t.length-1;e>=0;e--)if(/\s/u.test(t[e]))return e+1;return 0}function on(t,e,r=!1){let n=t.lastIndexOf("/"),s=t.slice(0,n+1),i=t.slice(n+1),o=w(e.cwd,s);if(!e.fs.isDir(o))return[];let{dirs:u,files:c}=e.fs.listDir(o),l=i.startsWith("."),f=[];for(let a of u)!l&&a.startsWith(".")||a.startsWith(i)&&f.push(s+a+"/");if(r)return f;for(let a of c)!l&&a.startsWith(".")||a.startsWith(i)&&f.push(s+a);return f}var j={__proto__:null,...At,...gt,...ft},U={__proto__:null,sed:wt,...Ft},un=["ls","cd","cat","grep","find","head","tail","wc","tree","sort","uniq","cut","tr","nl","tac","xargs","echo","pwd","seq","which","basename","dirname"],Mt=fn(),cn=Mt.join(", "),ln=["grep","head","tail","wc","sort","uniq","cut","xargs","tr","nl","tac","cat"];function fn(){let t=new Set(Object.keys(j)),e=[];for(let r of un)t.delete(r)&&e.push(r);return e.push(...[...t].sort()),e}function It(t,e={}){let r=ot(t),s={cwd:e.cwd===void 0?"/":w("/",e.cwd),fs:r};if(s.dispatch=(i,o,u)=>Rt(i,o,u,s),s.hasCommand=i=>!!(j[i]||U[i]),!r.isDir(s.cwd))throw new Error(`createTerminal: cwd is not a directory: ${s.cwd}`);return{run:i=>an(i,s),cwd:()=>s.cwd,complete:i=>Dt(i,s,{names:Mt,pipeNames:ln,binPrefixes:Lt,resolveCommand:Wt})}}var Lt=["/usr/local/bin/","/usr/bin/","/bin/","/sbin/"];function Wt(t){for(let e of Lt)if(t.startsWith(e)){let r=t.slice(e.length);if(j[r]||U[r])return r}return t}function Rt(t,e,r,n){let s=Wt(t),i=j[s]??U[s];if(!i)return mn(t);try{return i(r,e,n)}catch(o){return h(`${t}: ${o.message}`)}}function an(t,e){try{let r=t.trim();if(r==="")return{stdout:"",stderr:"",exitCode:0,cwd:e.cwd};let n=nt(r);return{...jt(n,e,""),cwd:e.cwd}}catch(r){return{...h(`error: ${r.message}`),cwd:e.cwd}}}function jt(t,e,r){let n="",s="",i=0,o=!1;for(let u=0;u<t.length;u++){let c=t[u];if(c.gate==="and"&&i!==0||c.gate==="or"&&i===0)continue;let l=hn(c.stages,e,u===0?r:"");n+=l.stdout,s+=l.stderr,i=l.exitCode,o=!0}return o||(i=0),{stdout:n,stderr:s,exitCode:i}}function hn(t,e,r){let n=r,s="";for(let i=0;i<t.length;i++){let o=t[i],u=o.group?pn(o.group,e,n):dn(o,e,n),c=u.stdout,l=u.stderr;if(o.mergeStderrToStdout&&(c+=l,l=""),o.mergeStdoutToStderr&&(l+=c,c=""),o.stdoutToNull&&(c=""),o.stderrToNull&&(l=""),s+=l,i===t.length-1)return{stdout:c,stderr:s,exitCode:u.exitCode};n=c}return{stdout:"",stderr:s,exitCode:0}}function dn(t,e,r){let n=tt(t.argv,t.quoted??new Set),s=ht(n.argv,n.quoted,e);return Rt(s[0],s.slice(1),r,e)}function pn(t,e,r){let n=e.cwd;try{return jt(t,e,r)}finally{e.cwd=n}}function mn(t){return h(`${t}: command not found. Available: ${cn}`,127)}var qt=`:host{display:flex;flex-direction:column;height:100%;min-height:0;background:var(--bg, #0c0c0c);color:var(--text, #e6edf3);font-family:ui-monospace,SF Mono,Menlo,Consolas,monospace;font-size:13px;line-height:1.45}.output{flex:1 1 auto;overflow-y:auto;overscroll-behavior:none;-webkit-app-region:no-drag;app-region:no-drag;scrollbar-gutter:stable;padding:12px 14px 6px;cursor:text}.banner{color:var(--muted, #8b949e);margin-bottom:8px}.banner .cmd{color:var(--accent, #58a6ff)}.banner .cmd:hover{text-decoration:underline}.line{white-space:pre-wrap;overflow-wrap:break-word}.cwd{color:var(--accent, #58a6ff)}.sigil{color:var(--muted, #8b949e);margin:0 6px 0 4px}.stdout,.stderr{margin:0 0 4px;font:inherit;white-space:pre-wrap;overflow-wrap:break-word}.stderr{color:var(--critical, #d8413a)}.form{flex:0 0 auto;display:flex;align-items:center;padding:8px 14px 10px;border-top:1px solid var(--border, #2c2c2c)}.form:focus-within{border-top-color:var(--accent, #58a6ff)}.form .cwd{flex:0 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;user-select:none}.form .sigil{flex:0 0 auto;user-select:none}.input-wrap{flex:1 1 auto;min-width:0;position:relative;display:flex;align-items:center}.input{width:100%;background:transparent;border:none;outline:none;color:inherit;font:inherit;padding:0;position:relative}.ghost{position:absolute;inset:0;display:flex;align-items:center;font:inherit;white-space:pre;overflow:hidden;pointer-events:none;user-select:none;color:var(--muted, #8b949e);opacity:.55}.ghost-pad{color:transparent}
39
- `;function wn(t){let r=["ls","find /",`grep -r ${xn(t)} .`];if(!t||t.size===0)return r;let n=null;for(let s of t.keys()){let i=bn(s);(n===null||i.length<n.length)&&(n=i)}return n&&r.push(`cat ${n} | head -n 20`),r}var K=["TODO","FIXME","XXX","HACK","NOTE","BUG","export","import","require","//"];function xn(t){if(!t||t.size===0)return K[0];for(let e of K)for(let r of t.values())if(r.includes(e))return e;return K[0]}function bn(t){return t.startsWith("./")?t.slice(2):t.startsWith("/")?t.slice(1):t}var Y=class t extends Q{static properties={sources:{attribute:!1},_lines:{state:!0},_input:{state:!0},_cwd:{state:!0},_ghost:{state:!0}};static styles=J(qt);#n=null;#c=null;#e=[];#t=-1;#s="";#r=null;#i=null;static#a=200;constructor(){super(),this.sources=null,this._lines=[],this._input="",this._cwd="/",this._ghost=""}willUpdate(e){e.has("sources")&&this.sources!==this.#c&&(this.#n=this.sources?It(this.sources):null,this._cwd=this.#n?this.#n.cwd():"/",this._lines=[{kind:"banner",commands:wn(this.sources)}],this.#e=[],this.#t=-1,this.#s="",this.#r=null,this._input="",this.#c=this.sources),e.has("_input")&&(this.#u(),this.#f())}connectedCallback(){super.connectedCallback(),this.updateComplete.then(()=>this.#l())}disconnectedCallback(){super.disconnectedCallback(),this.#u()}firstUpdated(){this.#o()}updated(e){(e.has("_lines")||e.has("sources"))&&this.#l()}#o(){let e=this.renderRoot.querySelector(".input");e&&e.focus()}#l(){let e=this.renderRoot.querySelector(".output");e&&(e.scrollTop=e.scrollHeight)}#h=e=>{if(e.preventDefault(),!this.#n)return;let r=this._input,n=r.trim();if(n==="clear"){this._lines=[],this._input="",n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1;return}let s=this._cwd,i=this.#n.run(r);this._cwd=i.cwd;let o=[...this._lines,{kind:"prompt",cwd:s,text:r}];i.stdout&&o.push({kind:"stdout",text:i.stdout}),i.stderr&&o.push({kind:"stderr",text:i.stderr}),this._lines=o,n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1,this._input=""};#d=e=>{if(e.key==="Tab"&&!e.shiftKey){e.preventDefault(),this.#p();return}this.#r=null,e.key==="ArrowUp"?(e.preventDefault(),this.#g()):e.key==="ArrowDown"&&(e.preventDefault(),this.#w()),this.#u(),this.#f()};#p(){if(!this.#n)return;let e=this.#r;if(e&&e.items[e.index]===this._input){e.index=(e.index+1)%e.items.length,this._input=e.items[e.index];return}let r=this.#n.complete(this._input);if(r.length===0){this.#r=null;return}this._input=r[0],this.#r=r.length>1?{items:r,index:0}:null}#u(){this._ghost="",this.#i!==null&&(clearTimeout(this.#i),this.#i=null)}#f(){this.#i=setTimeout(()=>{this.#i=null,this.#m()},t.#a)}#m(){if(!this.#n||this.#r)return;let e=this._input,r=e.slice(-1),n=e===""||r===" "||r==="|",s=/[\w./]/u.test(r);if(!n&&!s)return;let i=this.#n.complete(e);if(i.length===0)return;let o=i[0];!o.startsWith(e)||o===e||s&&i.includes(e)||(this._ghost=o.slice(e.length))}#g(){this.#e.length!==0&&(this.#t<0?(this.#s=this._input,this.#t=this.#e.length-1):this.#t=Math.max(0,this.#t-1),this._input=this.#e[this.#t])}#w(){if(this.#t<0)return;let e=this.#t+1;e>=this.#e.length?(this.#t=-1,this._input=this.#s,this.#s=""):(this.#t=e,this._input=this.#e[e])}#x=e=>{this._input=e.target.value};#b=e=>{this._input=e,this.#t=-1,this.#s="",this.#o()};#S=()=>{let e=this.renderRoot.getSelection?.()??document.getSelection();e&&!e.isCollapsed||this.#o()};render(){return C`<div class="output" @click="${this.#S}">${this._lines.map(e=>this.#v(e))}</div><form class="form" @submit="${this.#h}"><span class="cwd">${this._cwd}</span><span class="sigil">$</span><div class="input-wrap">${this._ghost?C`<div class="ghost" aria-hidden="true"><span class="ghost-pad">${this._input}</span>${this._ghost}</div>`:q} <input class="input" type="text" aria-label="Terminal command" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" .value="${this._input}" @input="${this.#x}" @keydown="${this.#d}"></div></form>`}#v(e){return e.kind==="banner"?C`<div class="banner">Virtual shell over the bundle source tree. Try ${e.commands.map((r,n)=>C`${n>0?", ":""}\`<span class="cmd" @click="${()=>this.#b(r)}">${r}</span>\``)}. ↑/↓ for history, Tab to complete.</div>`:e.kind==="prompt"?C`<div class="line"><span class="cwd">${e.cwd}</span><span class="sigil">$</span>${e.text}</div>`:e.kind==="stdout"?C`<pre class="stdout">${e.text}</pre>`:e.kind==="stderr"?C`<pre class="stderr">${e.text}</pre>`:q}};customElements.define("bundle-terminal",Y);function vr(t,e){let r=document.createElement("bundle-terminal");return r.dataset.integrity=e??"",r.sources=t,r}export{vr as createTerminalElement};
36
+ `)+e}function un(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["n"]});Nt(n,s);let o=_(n.get("n")??"10","head: -n");return o.error?o.error:Ft("head",t,s,r,i=>i.slice(0,o.value))}function cn(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["n"]});Nt(n,s);let o=_(n.get("n")??"10","tail: -n");return o.error?o.error:Ft("tail",t,s,r,i=>o.value===0?[]:i.slice(-o.value))}function Nt(t,e){t.has("n")||e[0]&&/^-\d+$/u.test(e[0])&&t.set("n",e.shift().slice(1))}function Ft(t,e,r,n,s){let o=E(t,r,e,n),i=o.inputs.length>1,u=[];for(let c=0;c<o.inputs.length;c++){let{name:a,content:l}=o.inputs[c],f=s(v(l)).join(`
37
+ `);i?u.push(`${c>0?`
38
+ `:""}==> ${a} <==
39
+ ${f}${f?`
40
+ `:""}`):f.length>0&&u.push(f+`
41
+ `)}return T(u.join(""),o)}function an(t,e,r){let{flags:n,positional:s}=m(e,{short:["l","w","c"]}),o=fn(n),i=E("wc",s,t,r),u=[],c={l:0,w:0,c:0};for(let{name:l,content:f}of i.inputs){let h=dn(f);u.push({counts:h,name:l}),c.l+=h.l,c.w+=h.w,c.c+=h.c}i.inputs.length>1&&u.push({counts:c,name:"total"});let a=ln(u,o);return T($(u.map(l=>pn(l.counts,l.name,o,a))),i)}function ln(t,e){let r=0;for(let{counts:n}of t)e.l&&(r=Math.max(r,String(n.l).length)),e.w&&(r=Math.max(r,String(n.w).length)),e.c&&(r=Math.max(r,String(n.c).length));return r}function fn(t){return t.has("l")||t.has("w")||t.has("c")?{l:t.has("l"),w:t.has("w"),c:t.has("c")}:{l:!0,w:!0,c:!0}}var hn=new TextEncoder;function dn(t){return{l:(t.match(/\n/gu)??[]).length,w:(t.match(/\S+/gu)??[]).length,c:hn.encode(t).length}}function pn(t,e,r,n){let s=[];return r.l&&s.push(String(t.l).padStart(n)),r.w&&s.push(String(t.w).padStart(n)),r.c&&s.push(String(t.c).padStart(n)),s.join(" ")+(e?" "+e:"")}function mn(t,e,r){let{flags:n,positional:s}=m(e,{short:["n","r","u"]}),o=R("sort",s,t,r),i=v(o.content),u=n.has("n"),c=n.has("u");if(u){let a=i.map(l=>({line:l,key:At(l)}));a.sort(c?(l,f)=>l.key-f.key:(l,f)=>l.key-f.key||(l.line<f.line?-1:l.line>f.line?1:0)),i=a.map(l=>l.line)}else i.sort();if(c){let a=new Set;i=i.filter(l=>{let f=u?At(l):l;return a.has(f)?!1:(a.add(f),!0)})}return n.has("r")&&i.reverse(),T($(i),o)}function At(t){let e=/^[ \t]*(-?(?:\d+\.?\d*|\.\d+))/u.exec(t);return e?Number(e[1]):0}function gn(t,e,r){let{flags:n,positional:s}=m(e,{short:["c","d","u","i"]}),o=R("uniq",s,t,r),i=n.has("c"),u=n.has("d"),c=n.has("u"),a=n.has("i"),l=S=>a?S.toLowerCase():S,f=v(o.content),h=[],p=null,b=null,g=0,y=()=>{if(p===null)return;let S=g>=2;!(u&&c)&&(u?S:!c||!S)&&h.push(i?`${String(g).padStart(7)} ${p}`:p)};for(let S of f){let C=l(S);if(C===b){g++;continue}y(),p=S,b=C,g=1}return y(),T($(h),o)}function wn(t,e){let{flags:r,positional:n}=m(e,{short:["n","e","E"]}),s=n.join(" "),o=!r.has("n");if(r.has("e")){let i=xn(s);s=i.text,i.stop&&(o=!1)}return w(o?s+`
42
+ `:s)}function xn(t){let e={a:7,b:8,e:27,f:12,n:10,r:13,t:9,v:11,"\\":92},r="";for(let n=0;n<t.length;n++){if(t[n]!=="\\"||n+1>=t.length){r+=t[n];continue}let s=t[++n];if(s==="c")return{text:r,stop:!0};if(s in e){r+=String.fromCodePoint(e[s]);continue}if(s==="0"){let o="";for(;o.length<3&&/[0-7]/u.test(t[n+1]??"");)o+=t[++n];r+=String.fromCodePoint(o===""?0:parseInt(o,8));continue}if(s==="x"&&/[0-9a-fA-F]/u.test(t[n+1]??"")){let o="";for(;o.length<2&&/[0-9a-fA-F]/u.test(t[n+1]??"");)o+=t[++n];r+=String.fromCodePoint(parseInt(o,16));continue}r+="\\"+s}return{text:r,stop:!1}}function bn(t,e,r){let{flags:n,values:s,positional:o}=m(e,{short:["r"],valueShort:["n"],stopAtFirstPositional:!0}),[i="echo",...u]=o,c=t.split(/\s+/u).filter(Boolean);if(c.length===0)return n.has("r")?w():r.dispatch(i,u,"");let a=s.has("n")?_(s.get("n"),"xargs: -n"):{value:c.length};return a.error?a.error:s.has("n")&&a.value===0?d("xargs: -n: must be at least 1"):Sn(r,i,u,c,a.value)}function Sn(t,e,r,n,s){let o="",i="",u=0;for(let c=0;c<n.length;c+=s){let a=t.dispatch(e,[...r,...n.slice(c,c+s)],"");o+=a.stdout,i+=a.stderr,a.exitCode!==0&&(u=a.exitCode)}return{stdout:o,stderr:i,exitCode:u}}function Dt(){return w()}function yn(){return{stdout:"",stderr:"",exitCode:1}}var It={cat:sn,grep:_t,head:un,tail:cn,wc:an,sort:mn,uniq:gn,echo:wn,xargs:bn},Wt={true:Dt,false:yn,":":Dt};function Lt(t,e,r){let{index:n,pipe:s}=$n(t),o=t.slice(n),i=Tn(o),u=o.slice(i),c=o.slice(0,i).trim(),a=c==="",l=a?"":r.resolveCommand(c.split(/\s+/u)[0]),f=t.slice(0,n+i),h=s&&f.endsWith("|")?" ":"";return vn(u,a,s,l,e,r).map(p=>f+h+p)}function vn(t,e,r,n,s,o){if(!e)return r?[]:kn(t,s,n==="cd");let i=r?o.pipeNames:o.names;for(let u of o.binPrefixes)if(t.startsWith(u)){let c=t.slice(u.length);return i.filter(a=>a.startsWith(c)).map(a=>u+a)}return t.startsWith("/")||t.startsWith("./")?[]:i.filter(u=>u.startsWith(t))}function $n(t){let e=0,r=!1,n=0;for(;n<t.length;){let s=t[n];if(s==="|"){let o=t[n+1]==="|";n+=o?2:1,e=n,r=!o}else s==="&"&&t[n+1]==="&"?(n+=2,e=n,r=!1):s===";"?(n++,e=n,r=!1):n++}return{index:e,pipe:r}}function Tn(t){for(let e=t.length-1;e>=0;e--)if(/\s/u.test(t[e]))return e+1;return 0}function kn(t,e,r=!1){let n=t.lastIndexOf("/"),s=t.slice(0,n+1),o=t.slice(n+1),i=x(e.cwd,s);if(!e.fs.isDir(i))return[];let{dirs:u,files:c}=e.fs.listDir(i),a=o.startsWith("."),l=[];for(let f of u)!a&&f.startsWith(".")||f.startsWith(o)&&l.push(s+f+"/");if(r)return l;for(let f of c)!a&&f.startsWith(".")||f.startsWith(o)&&l.push(s+f);return l}var P={__proto__:null,...It,...yt,...ht},Z={__proto__:null,sed:vt,...dt,...Wt},Cn=["ls","cd","cat","grep","find","head","tail","wc","tree","sort","uniq","cut","tr","nl","tac","xargs","echo","pwd","seq","which","basename","dirname"],Rt=Mn(),En=Rt.join(", "),_n=["grep","head","tail","wc","sort","uniq","cut","xargs","tr","nl","tac","cat"];function Mn(){let t=new Set(Object.keys(P)),e=[];for(let r of Cn)t.delete(r)&&e.push(r);return e.push(...[...t].sort()),e}function Ot(t,e={}){let r=ct(t),s={cwd:e.cwd===void 0?"/":x("/",e.cwd),fs:r,user:e.user??"user"};if(s.dispatch=(o,i,u)=>Pt(o,i,u,s),s.hasCommand=o=>!!(P[o]||Z[o]),!r.isDir(s.cwd))throw new Error(`createTerminal: cwd is not a directory: ${s.cwd}`);return{run:o=>An(o,s),cwd:()=>s.cwd,complete:o=>Lt(o,s,{names:Rt,pipeNames:_n,binPrefixes:jt,resolveCommand:qt})}}var jt=["/usr/local/bin/","/usr/bin/","/bin/","/sbin/"];function qt(t){for(let e of jt)if(t.startsWith(e)){let r=t.slice(e.length);if(P[r]||Z[r])return r}return t}function Pt(t,e,r,n){let s=qt(t),o=P[s]??Z[s];if(!o)return In(t);try{return o(r,e,n)}catch(i){return d(`${t}: ${i.message}`)}}function An(t,e){try{let r=t.trim();if(r==="")return{stdout:"",stderr:"",exitCode:0,cwd:e.cwd};let n=st(r);return{...Ht(n,e,""),cwd:e.cwd}}catch(r){return{...d(`error: ${r.message}`),cwd:e.cwd}}}function Ht(t,e,r){let n="",s="",o=0,i=!1;for(let u=0;u<t.length;u++){let c=t[u];if(c.gate==="and"&&o!==0||c.gate==="or"&&o===0)continue;let a=Dn(c.stages,e,u===0?r:"");n+=a.stdout,s+=a.stderr,o=a.exitCode,i=!0}return i||(o=0),{stdout:n,stderr:s,exitCode:o}}function Dn(t,e,r){let n=r,s="";for(let o=0;o<t.length;o++){let i=t[o],u=i.group?Fn(i.group,e,n):Nn(i,e,n),c=u.stdout,a=u.stderr;if(i.mergeStderrToStdout&&(c+=a,a=""),i.mergeStdoutToStderr&&(a+=c,c=""),i.stdoutToNull&&(c=""),i.stderrToNull&&(a=""),s+=a,o===t.length-1)return{stdout:c,stderr:s,exitCode:u.exitCode};n=c}return{stdout:"",stderr:s,exitCode:0}}function Nn(t,e,r){let n=nt(t.argv,t.quoted??new Set),s=gt(n.argv,n.quoted,e);return Pt(s[0],s.slice(1),r,e)}function Fn(t,e,r){let n=e.cwd;try{return Ht(t,e,r)}finally{e.cwd=n}}function In(t){return d(`${t}: command not found. Available: ${En}`,127)}var Bt=`:host{display:flex;flex-direction:column;height:100%;min-height:0;background:var(--bg, #0c0c0c);color:var(--text, #e6edf3);font-family:ui-monospace,SF Mono,Menlo,Consolas,monospace;font-size:13px;line-height:1.45}.output{flex:1 1 auto;overflow-y:auto;overscroll-behavior:none;-webkit-app-region:no-drag;app-region:no-drag;scrollbar-gutter:stable;padding:12px 14px 6px;cursor:text}.banner{color:var(--muted, #8b949e);margin-bottom:8px}.banner .cmd{color:var(--accent, #58a6ff)}.banner .cmd:hover{text-decoration:underline}.line{white-space:pre-wrap;overflow-wrap:break-word}.cwd{color:var(--accent, #58a6ff)}.sigil{color:var(--muted, #8b949e);margin:0 6px 0 4px}.stdout,.stderr{margin:0 0 4px;font:inherit;white-space:pre-wrap;overflow-wrap:break-word}.stderr{color:var(--critical, #d8413a)}.form{flex:0 0 auto;display:flex;align-items:center;padding:8px 14px 10px;border-top:1px solid var(--border, #2c2c2c)}.form:focus-within{border-top-color:var(--accent, #58a6ff)}.form .cwd{flex:0 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;user-select:none}.form .sigil{flex:0 0 auto;user-select:none}.input-wrap{flex:1 1 auto;min-width:0;position:relative;display:flex;align-items:center}.input{width:100%;background:transparent;border:none;outline:none;color:inherit;font:inherit;padding:0;position:relative}.ghost{position:absolute;inset:0;display:flex;align-items:center;font:inherit;white-space:pre;overflow:hidden;pointer-events:none;user-select:none;color:var(--muted, #8b949e);opacity:.55}.ghost-pad{color:transparent}
43
+ `;function Ln(t){let r=["ls","find /",`grep -r ${Rn(t)} .`];if(!t||t.size===0)return r;let n=null;for(let s of t.keys()){let o=On(s);(n===null||o.length<n.length)&&(n=o)}return n&&r.push(`cat ${n} | head -n 20`),r}var J=["TODO","FIXME","XXX","HACK","NOTE","BUG","export","import","require","//"];function Rn(t){if(!t||t.size===0)return J[0];for(let e of J)for(let r of t.values())if(r.includes(e))return e;return J[0]}function On(t){return t.startsWith("./")?t.slice(2):t.startsWith("/")?t.slice(1):t}var K=class t extends tt{static properties={sources:{attribute:!1},_lines:{state:!0},_input:{state:!0},_cwd:{state:!0},_ghost:{state:!0}};static styles=et(Bt);#n=null;#c=null;#e=[];#t=-1;#s="";#r=null;#o=null;static#f=200;constructor(){super(),this.sources=null,this._lines=[],this._input="",this._cwd="/",this._ghost=""}willUpdate(e){e.has("sources")&&this.sources!==this.#c&&(this.#n=this.sources?Ot(this.sources):null,this._cwd=this.#n?this.#n.cwd():"/",this._lines=[{kind:"banner",commands:Ln(this.sources)}],this.#e=[],this.#t=-1,this.#s="",this.#r=null,this._input="",this.#c=this.sources),e.has("_input")&&(this.#u(),this.#l())}connectedCallback(){super.connectedCallback(),this.updateComplete.then(()=>this.#a())}disconnectedCallback(){super.disconnectedCallback(),this.#u()}firstUpdated(){this.#i()}updated(e){(e.has("_lines")||e.has("sources"))&&this.#a()}#i(){let e=this.renderRoot.querySelector(".input");e&&e.focus()}#a(){let e=this.renderRoot.querySelector(".output");e&&(e.scrollTop=e.scrollHeight)}#h=e=>{if(e.preventDefault(),!this.#n)return;let r=this._input,n=r.trim();if(n==="clear"){this._lines=[],this._input="",n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1;return}let s=this._cwd,o=this.#n.run(r);this._cwd=o.cwd;let i=[...this._lines,{kind:"prompt",cwd:s,text:r}];o.stdout&&i.push({kind:"stdout",text:o.stdout}),o.stderr&&i.push({kind:"stderr",text:o.stderr}),this._lines=i,n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1,this._input=""};#d=e=>{if(e.key==="Tab"&&!e.shiftKey){e.preventDefault(),this.#p();return}this.#r=null,e.key==="ArrowUp"?(e.preventDefault(),this.#g()):e.key==="ArrowDown"&&(e.preventDefault(),this.#w()),this.#u(),this.#l()};#p(){if(!this.#n)return;let e=this.#r;if(e&&e.items[e.index]===this._input){e.index=(e.index+1)%e.items.length,this._input=e.items[e.index];return}let r=this.#n.complete(this._input);if(r.length===0){this.#r=null;return}this._input=r[0],this.#r=r.length>1?{items:r,index:0}:null}#u(){this._ghost="",this.#o!==null&&(clearTimeout(this.#o),this.#o=null)}#l(){this.#o=setTimeout(()=>{this.#o=null,this.#m()},t.#f)}#m(){if(!this.#n||this.#r)return;let e=this._input,r=e.slice(-1),n=e===""||r===" "||r==="|",s=/[\w./]/u.test(r);if(!n&&!s)return;let o=this.#n.complete(e);if(o.length===0)return;let i=o[0];!i.startsWith(e)||i===e||s&&o.includes(e)||(this._ghost=i.slice(e.length))}#g(){this.#e.length!==0&&(this.#t<0?(this.#s=this._input,this.#t=this.#e.length-1):this.#t=Math.max(0,this.#t-1),this._input=this.#e[this.#t])}#w(){if(this.#t<0)return;let e=this.#t+1;e>=this.#e.length?(this.#t=-1,this._input=this.#s,this.#s=""):(this.#t=e,this._input=this.#e[e])}#x=e=>{this._input=e.target.value};#b=e=>{this._input=e,this.#t=-1,this.#s="",this.#i()};#S=()=>{let e=this.renderRoot.getSelection?.()??document.getSelection();e&&!e.isCollapsed||this.#i()};render(){return M`<div class="output" @click="${this.#S}">${this._lines.map(e=>this.#y(e))}</div><form class="form" @submit="${this.#h}"><span class="cwd">${this._cwd}</span><span class="sigil">$</span><div class="input-wrap">${this._ghost?M`<div class="ghost" aria-hidden="true"><span class="ghost-pad">${this._input}</span>${this._ghost}</div>`:H} <input class="input" type="text" aria-label="Terminal command" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" .value="${this._input}" @input="${this.#x}" @keydown="${this.#d}"></div></form>`}#y(e){return e.kind==="banner"?M`<div class="banner">Virtual shell over the bundle source tree. Try ${e.commands.map((r,n)=>M`${n>0?", ":""}\`<span class="cmd" @click="${()=>this.#b(r)}">${r}</span>\``)}. ↑/↓ for history, Tab to complete.</div>`:e.kind==="prompt"?M`<div class="line"><span class="cwd">${e.cwd}</span><span class="sigil">$</span>${e.text}</div>`:e.kind==="stdout"?M`<pre class="stdout">${e.text}</pre>`:e.kind==="stderr"?M`<pre class="stderr">${e.text}</pre>`:H}};customElements.define("bundle-terminal",K);function qr(t,e){let r=document.createElement("bundle-terminal");return r.dataset.integrity=e??"",r.sources=t,r}export{qr as createTerminalElement};