import{a as f,b as m,c}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";var h={name:"nl",summary:"number lines of files",usage:"nl [OPTION]... [FILE]...",description:"Write each FILE to standard output, with line numbers added. If no FILE is specified, standard input is read.",options:["-b STYLE     Body numbering style: a (all), t (non-empty), n (none)","-n FORMAT    Number format: ln (left), rn (right), rz (right zeros)","-w WIDTH     Number width (default: 6)","-s SEP       Separator after number (default: TAB)","-v START     Starting line number (default: 1)","-i INCR      Line number increment (default: 1)"],examples:["nl file.txt              # Number non-empty lines","nl -ba file.txt          # Number all lines","nl -n rz -w 3 file.txt   # Right-justified with zeros","nl -s ': ' file.txt      # Use ': ' as separator"]};function p(r,s,i){let l=String(r);switch(s){case"ln":return l.padEnd(i);case"rn":return l.padStart(i);case"rz":return l.padStart(i,"0");default:return s}}function N(r,s){switch(s){case"a":return!0;case"t":return r.trim().length>0;case"n":return!1;default:return s}}function b(r,s,i){if(r==="")return{output:"",nextNumber:i};let l=r.split(`
`),t=[],u=i,o=r.endsWith(`
`)&&l[l.length-1]==="";o&&l.pop();for(let n of l)if(N(n,s.bodyStyle)){let e=p(u,s.numberFormat,s.width);t.push(`${e}${s.separator}${n}`),u+=s.increment}else{let e=" ".repeat(s.width);t.push(`${e}${s.separator}${n}`)}return{output:t.join(`
`)+(o?`
`:""),nextNumber:u}}var w={name:"nl",execute:async(r,s)=>{if(m(r))return f(h);let i={bodyStyle:"t",numberFormat:"rn",width:6,separator:"	",startNumber:1,increment:1},l=[],t=0;for(;t<r.length;){let n=r[t];if(n==="-b"&&t+1<r.length){let e=r[t+1];if(e!=="a"&&e!=="t"&&e!=="n")return{exitCode:1,stdout:"",stderr:`nl: invalid body numbering style: '${e}'
`};i.bodyStyle=e,t+=2}else if(n.startsWith("-b")){let e=n.slice(2);if(e!=="a"&&e!=="t"&&e!=="n")return{exitCode:1,stdout:"",stderr:`nl: invalid body numbering style: '${e}'
`};i.bodyStyle=e,t++}else if(n==="-n"&&t+1<r.length){let e=r[t+1];if(e!=="ln"&&e!=="rn"&&e!=="rz")return{exitCode:1,stdout:"",stderr:`nl: invalid line numbering format: '${e}'
`};i.numberFormat=e,t+=2}else if(n.startsWith("-n")){let e=n.slice(2);if(e!=="ln"&&e!=="rn"&&e!=="rz")return{exitCode:1,stdout:"",stderr:`nl: invalid line numbering format: '${e}'
`};i.numberFormat=e,t++}else if(n==="-w"&&t+1<r.length){let e=parseInt(r[t+1],10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:"",stderr:`nl: invalid line number field width: '${r[t+1]}'
`};i.width=e,t+=2}else if(n.startsWith("-w")){let e=parseInt(n.slice(2),10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:"",stderr:`nl: invalid line number field width: '${n.slice(2)}'
`};i.width=e,t++}else if(n==="-s"&&t+1<r.length)i.separator=r[t+1],t+=2;else if(n.startsWith("-s"))i.separator=n.slice(2),t++;else if(n==="-v"&&t+1<r.length){let e=parseInt(r[t+1],10);if(Number.isNaN(e))return{exitCode:1,stdout:"",stderr:`nl: invalid starting line number: '${r[t+1]}'
`};i.startNumber=e,t+=2}else if(n.startsWith("-v")){let e=parseInt(n.slice(2),10);if(Number.isNaN(e))return{exitCode:1,stdout:"",stderr:`nl: invalid starting line number: '${n.slice(2)}'
`};i.startNumber=e,t++}else if(n==="-i"&&t+1<r.length){let e=parseInt(r[t+1],10);if(Number.isNaN(e))return{exitCode:1,stdout:"",stderr:`nl: invalid line number increment: '${r[t+1]}'
`};i.increment=e,t+=2}else if(n.startsWith("-i")){let e=parseInt(n.slice(2),10);if(Number.isNaN(e))return{exitCode:1,stdout:"",stderr:`nl: invalid line number increment: '${n.slice(2)}'
`};i.increment=e,t++}else if(n==="--"){l.push(...r.slice(t+1));break}else{if(n.startsWith("-")&&n!=="-")return c("nl",n);l.push(n),t++}}let u="",o=i.startNumber;if(l.length===0){let n=s.stdin??"";u=b(n,i,o).output}else for(let n of l){let e=s.fs.resolvePath(s.cwd,n),a=await s.fs.readFile(e);if(a===null)return{exitCode:1,stdout:u,stderr:`nl: ${n}: No such file or directory
`};let d=b(a,i,o);u+=d.output,o=d.nextNumber}return{exitCode:0,stdout:u,stderr:""}}};export{w as nl};
