#!/usr/bin/env node
import{a as W}from"./chunk-XT766N2U.js";import{a as R}from"./chunk-JBABAK44.js";import{a as E,b as H}from"./chunk-GTNBSMZR.js";function M(n){if(n<1024)return String(n);if(n<1024*1024){let c=n/1024;return c<10?`${c.toFixed(1)}K`:`${Math.round(c)}K`}if(n<1024*1024*1024){let c=n/1048576;return c<10?`${c.toFixed(1)}M`:`${Math.round(c)}M`}let s=n/(1024*1024*1024);return s<10?`${s.toFixed(1)}G`:`${Math.round(s)}G`}function T(n){let c=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][n.getMonth()],w=String(n.getDate()).padStart(2," "),S=new Date,y=new Date(S.getTime()-4320*60*60*1e3);if(n>y){let b=String(n.getHours()).padStart(2,"0"),g=String(n.getMinutes()).padStart(2,"0");return`${c} ${w} ${b}:${g}`}let P=n.getFullYear();return`${c} ${w}  ${P}`}function C(n){return n.isDirectory?"/":n.isSymbolicLink?"@":(n.mode&73)!==0?"*":""}var k={name:"ls",summary:"list directory contents",usage:"ls [OPTION]... [FILE]...",options:["-a, --all            do not ignore entries starting with .","-A, --almost-all     do not list . and ..","-d, --directory      list directories themselves, not their contents","-F, --classify       append indicator (one of */=>@) to entries","-h, --human-readable with -l, print sizes like 1K 234M 2G etc.","-l                   use a long listing format","-r, --reverse        reverse order while sorting","-R, --recursive      list subdirectories recursively","-S                   sort by file size, largest first","-t                   sort by time, newest first","-1                   list one file per line","    --help           display this help and exit"]},G={showAll:{short:"a",long:"all",type:"boolean"},showAlmostAll:{short:"A",long:"almost-all",type:"boolean"},longFormat:{short:"l",type:"boolean"},humanReadable:{short:"h",long:"human-readable",type:"boolean"},recursive:{short:"R",long:"recursive",type:"boolean"},reverse:{short:"r",long:"reverse",type:"boolean"},sortBySize:{short:"S",type:"boolean"},classifyFiles:{short:"F",long:"classify",type:"boolean"},directoryOnly:{short:"d",long:"directory",type:"boolean"},sortByTime:{short:"t",type:"boolean"},onePerLine:{short:"1",type:"boolean"}},Z={name:"ls",async execute(n,s){if(H(n))return E(k);let c=R("ls",n,G);if(!c.ok)return c.error;let w=c.result.flags.showAll,S=c.result.flags.showAlmostAll,y=c.result.flags.longFormat,P=c.result.flags.humanReadable,b=c.result.flags.recursive,g=c.result.flags.reverse,v=c.result.flags.sortBySize,$=c.result.flags.classifyFiles,A=c.result.flags.directoryOnly,p=c.result.flags.sortByTime;c.result.flags.onePerLine;let r=c.result.positional;r.length===0&&r.push(".");let a="",l="",u=0;for(let f=0;f<r.length;f++){let o=r[f];if(f>0&&a&&!a.endsWith(`

`)&&(a+=`
`),A){let i=s.fs.resolvePath(s.cwd,o);try{let t=await s.fs.stat(i);if(y){let e=t.isDirectory?"drwxr-xr-x":"-rw-r--r--",d=$?C(await s.fs.lstat(i)):t.isDirectory?"/":"",h=t.size??0,m=P?M(h).padStart(5):String(h).padStart(5),z=t.mtime??new Date(0),F=T(z);a+=`${e} 1 user user ${m} ${F} ${o}${d}
`}else{let e=$?C(await s.fs.lstat(i)):"";a+=`${o}${e}
`}}catch{l+=`ls: cannot access '${o}': No such file or directory
`,u=2}continue}if(o.includes("*")||o.includes("?")||o.includes("[")){let i=await _(o,s,w,S,y,g,P,v,$);a+=i.stdout,l+=i.stderr,i.exitCode!==0&&(u=i.exitCode)}else{let i=await O(o,s,w,S,y,b,r.length>1,g,P,v,$);a+=i.stdout,l+=i.stderr,i.exitCode!==0&&(u=i.exitCode)}}return{stdout:a,stderr:l,exitCode:u}}};async function _(n,s,c,w,S,y=!1,P=!1,b=!1,g=!1){let v=c||w,$=s.fs.getAllPaths(),A=s.fs.resolvePath(s.cwd,"."),p=[];for(let r of $){let a=r.startsWith(A)&&r.slice(A.length+1)||r;if(W(a,n)||W(r,n)){let l=a.split("/").pop()||a;if(!v&&l.startsWith("."))continue;p.push(a||r)}}if(p.length===0)return{stdout:"",stderr:`ls: ${n}: No such file or directory
`,exitCode:2};if(b){let r=[];for(let a of p){let l=s.fs.resolvePath(s.cwd,a);try{let u=await s.fs.stat(l);r.push({path:a,size:u.size??0})}catch{r.push({path:a,size:0})}}r.sort((a,l)=>l.size-a.size),p.length=0,p.push(...r.map(a=>a.path))}else p.sort();if(y&&p.reverse(),S){let r=[];for(let a of p){let l=s.fs.resolvePath(s.cwd,a);try{let u=await s.fs.stat(l),f=u.isDirectory?"drwxr-xr-x":"-rw-r--r--",o=g?C(await s.fs.lstat(l)):u.isDirectory?"/":"",i=u.size??0,t=P?M(i).padStart(5):String(i).padStart(5),e=u.mtime??new Date(0),d=T(e);r.push(`${f} 1 user user ${t} ${d} ${a}${o}`)}catch{r.push(`-rw-r--r-- 1 user user     0 Jan  1 00:00 ${a}`)}}return{stdout:`${r.join(`
`)}
`,stderr:"",exitCode:0}}if(g){let r=[];for(let a of p){let l=s.fs.resolvePath(s.cwd,a);try{let u=await s.fs.lstat(l);r.push(`${a}${C(u)}`)}catch{r.push(a)}}return{stdout:`${r.join(`
`)}
`,stderr:"",exitCode:0}}return{stdout:`${p.join(`
`)}
`,stderr:"",exitCode:0}}async function O(n,s,c,w,S,y,P,b=!1,g=!1,v=!1,$=!1,A=!1){let p=c||w,r=s.fs.resolvePath(s.cwd,n);try{let a=await s.fs.stat(r);if(!a.isDirectory){let f=$?C(await s.fs.lstat(r)):"";if(S){let o=a.size??0,i=g?M(o).padStart(5):String(o).padStart(5),t=a.mtime??new Date(0),e=T(t);return{stdout:`-rw-r--r-- 1 user user ${i} ${e} ${n}${f}
`,stderr:"",exitCode:0}}return{stdout:`${n}${f}
`,stderr:"",exitCode:0}}let l=await s.fs.readdir(r);if(p||(l=l.filter(f=>!f.startsWith("."))),v){let f=[];for(let o of l){let i=r==="/"?`/${o}`:`${r}/${o}`;try{let t=await s.fs.stat(i);f.push({name:o,size:t.size??0})}catch{f.push({name:o,size:0})}}f.sort((o,i)=>i.size-o.size),l=f.map(o=>o.name)}else l.sort();c&&(l=[".","..",...l]),b&&l.reverse();let u="";if((y||P)&&(u+=`${n}:
`),S){u+=`total ${l.length}
`;let f=l.filter(e=>e==="."||e===".."),o=l.filter(e=>e!=="."&&e!=="..");for(let e of f)u+=`drwxr-xr-x 1 user user     0 Jan  1 00:00 ${e}
`;let i=[];for(let e=0;e<o.length;e+=100){let d=o.slice(e,e+100),h=await Promise.all(d.map(async m=>{let z=r==="/"?`/${m}`:`${r}/${m}`;try{let F=await s.fs.stat(z),B=F.isDirectory?"drwxr-xr-x":"-rw-r--r--",J=$?C(await s.fs.lstat(z)):F.isDirectory?"/":"",x=F.size??0,j=g?M(x).padStart(5):String(x).padStart(5),L=F.mtime??new Date(0),N=T(L);return{name:m,line:`${B} 1 user user ${j} ${N} ${m}${J}
`}}catch{return{name:m,line:`-rw-r--r-- 1 user user     0 Jan  1 00:00 ${m}
`}}}));i.push(...h)}let t=new Map(o.map((e,d)=>[e,d]));i.sort((e,d)=>(t.get(e.name)??0)-(t.get(d.name)??0));for(let{line:e}of i)u+=e}else if($){let f=[],o=l.filter(t=>t!=="."&&t!==".."),i=l.filter(t=>t==="."||t==="..");for(let t of i)f.push(`${t}/`);for(let t=0;t<o.length;t+=100){let e=o.slice(t,t+100),d=await Promise.all(e.map(async h=>{let m=r==="/"?`/${h}`:`${r}/${h}`;try{let z=await s.fs.lstat(m);return`${h}${C(z)}`}catch{return h}}));f.push(...d)}u+=f.join(`
`)+(f.length?`
`:"")}else u+=l.join(`
`)+(l.length?`
`:"");if(y){let f=l.filter(t=>t!=="."&&t!==".."),o=[];if(s.fs.readdirWithFileTypes)o=(await s.fs.readdirWithFileTypes(r)).filter(e=>e.isDirectory&&f.includes(e.name)).map(e=>({name:e.name,isDirectory:!0}));else for(let t=0;t<f.length;t+=100){let e=f.slice(t,t+100),d=await Promise.all(e.map(async h=>{let m=r==="/"?`/${h}`:`${r}/${h}`;try{let z=await s.fs.stat(m);return{name:h,isDirectory:z.isDirectory}}catch{return{name:h,isDirectory:!1}}}));o.push(...d.filter(h=>h.isDirectory))}o.sort((t,e)=>t.name.localeCompare(e.name)),b&&o.reverse();let i=[];for(let t=0;t<o.length;t+=100){let e=o.slice(t,t+100),d=await Promise.all(e.map(async h=>{let m=n==="."?`./${h.name}`:`${n}/${h.name}`,z=await O(m,s,c,w,S,y,!1,b,g,v,$,!0);return{name:h.name,result:z}}));i.push(...d)}i.sort((t,e)=>t.name.localeCompare(e.name)),b&&i.reverse();for(let{result:t}of i)u+=`
`,u+=t.stdout}return{stdout:u,stderr:"",exitCode:0}}catch{return{stdout:"",stderr:`ls: ${n}: No such file or directory
`,exitCode:2}}}var q={name:"ls",flags:[{flag:"-a",type:"boolean"},{flag:"-A",type:"boolean"},{flag:"-l",type:"boolean"},{flag:"-h",type:"boolean"},{flag:"-R",type:"boolean"},{flag:"-r",type:"boolean"},{flag:"-S",type:"boolean"},{flag:"-F",type:"boolean"},{flag:"-d",type:"boolean"},{flag:"-t",type:"boolean"},{flag:"-1",type:"boolean"}],needsFiles:!0};export{Z as a,q as b};
