import"./chunk-NUFRM6SI.js";import{a as W}from"./chunk-HWKDQ44K.js";import{a as F,b as T}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";import{minimatch as H}from"minimatch";function A(i){if(i<1024)return String(i);if(i<1024*1024){let n=i/1024;return n<10?`${n.toFixed(1)}K`:`${Math.round(n)}K`}if(i<1024*1024*1024){let n=i/1048576;return n<10?`${n.toFixed(1)}M`:`${Math.round(n)}M`}let r=i/(1024*1024*1024);return r<10?`${r.toFixed(1)}G`:`${Math.round(r)}G`}function M(i){let n=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][i.getMonth()],g=String(i.getDate()).padStart(2," "),$=new Date,y=new Date($.getTime()-4320*60*60*1e3);if(i>y){let w=String(i.getHours()).padStart(2,"0"),S=String(i.getMinutes()).padStart(2,"0");return`${n} ${g} ${w}:${S}`}let z=i.getFullYear();return`${n} ${g}  ${z}`}var G={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","-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"]},L={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"},directoryOnly:{short:"d",long:"directory",type:"boolean"},sortByTime:{short:"t",type:"boolean"},onePerLine:{short:"1",type:"boolean"}},U={name:"ls",async execute(i,r){if(T(i))return F(G);let n=W("ls",i,L);if(!n.ok)return n.error;let g=n.result.flags.showAll,$=n.result.flags.showAlmostAll,y=n.result.flags.longFormat,z=n.result.flags.humanReadable,w=n.result.flags.recursive,S=n.result.flags.reverse,D=n.result.flags.sortBySize,C=n.result.flags.directoryOnly,m=n.result.flags.sortByTime;n.result.flags.onePerLine;let o=n.result.positional;o.length===0&&o.push(".");let a="",c="",f=0;for(let u=0;u<o.length;u++){let e=o[u];if(u>0&&a&&!a.endsWith(`

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