import{n as e,r as t,t as n}from"./chunk-74CEPOFO-V0JXh91S.mjs";var r={name:`fold`,summary:`wrap each input line to fit in specified width`,usage:`fold [OPTION]... [FILE]...`,description:`Wrap input lines in each FILE, writing to standard output. If no FILE is specified, standard input is read.`,options:[`-w WIDTH    Use WIDTH columns instead of 80`,`-s          Break at spaces`,`-b          Count bytes rather than columns`],examples:[`fold -w 40 file.txt           # Wrap at 40 columns`,`fold -sw 40 file.txt          # Word wrap at 40 columns`,`echo 'long line' | fold -w 5  # Force wrap at 5`]};function i(e,t,n){return n?new TextEncoder().encode(e).length:e===`	`?8-t%8:e===`\b`?-1:1}function a(e,t){if(e.length===0)return e;let{width:n,breakAtSpaces:r,countBytes:a}=t,o=[],s=``,c=0,l=-1,u=0;for(let t=0;t<e.length;t++){let d=e[t],f=i(d,c,a);c+f>n&&s.length>0?r&&l>=0?(o.push(s.slice(0,l+1)),s=s.slice(l+1)+d,c=c-u-1+f,l=-1,u=0):(o.push(s),s=d,c=f,l=-1,u=0):(s+=d,c+=f,(d===` `||d===`	`)&&(l=s.length-1,u=c-f))}return s.length>0&&o.push(s),o.join(`
`)}function o(e,t){if(e===``)return``;let n=e.split(`
`),r=e.endsWith(`
`)&&n[n.length-1]===``;return r&&n.pop(),n.map(e=>a(e,t)).join(`
`)+(r?`
`:``)}var s={name:`fold`,execute:async(i,a)=>{if(n(i))return t(r);let s={width:80,breakAtSpaces:!1,countBytes:!1},c=[],l=0;for(;l<i.length;){let t=i[l];if(t===`-w`&&l+1<i.length){let e=parseInt(i[l+1],10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:``,stderr:`fold: invalid number of columns: '${i[l+1]}'
`};s.width=e,l+=2}else if(t.startsWith(`-w`)&&t.length>2){let e=parseInt(t.slice(2),10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:``,stderr:`fold: invalid number of columns: '${t.slice(2)}'
`};s.width=e,l++}else if(t===`-s`)s.breakAtSpaces=!0,l++;else if(t===`-b`)s.countBytes=!0,l++;else if(t===`-bs`||t===`-sb`)s.breakAtSpaces=!0,s.countBytes=!0,l++;else if(t.match(/^-[sb]+w\d+$/)){t.includes(`s`)&&(s.breakAtSpaces=!0),t.includes(`b`)&&(s.countBytes=!0);let e=t.replace(/^-[sb]+w/,``),n=parseInt(e,10);if(Number.isNaN(n)||n<1)return{exitCode:1,stdout:``,stderr:`fold: invalid number of columns: '${e}'
`};s.width=n,l++}else if(t.match(/^-[sb]+w$/)&&l+1<i.length){t.includes(`s`)&&(s.breakAtSpaces=!0),t.includes(`b`)&&(s.countBytes=!0);let e=parseInt(i[l+1],10);if(Number.isNaN(e)||e<1)return{exitCode:1,stdout:``,stderr:`fold: invalid number of columns: '${i[l+1]}'
`};s.width=e,l+=2}else if(t===`--`){c.push(...i.slice(l+1));break}else if(t.startsWith(`-`)&&t!==`-`){let n=t.slice(1),r=!1;for(let e of n)if(e===`s`)s.breakAtSpaces=!0;else if(e===`b`)s.countBytes=!0;else{r=!0;break}if(r)return e(`fold`,t);l++}else c.push(t),l++}let u=``;if(c.length===0)u=o(a.stdin??``,s);else for(let e of c){let t=a.fs.resolvePath(a.cwd,e),n=await a.fs.readFile(t);if(n===null)return{exitCode:1,stdout:u,stderr:`fold: ${e}: No such file or directory
`};u+=o(n,s)}return{exitCode:0,stdout:u,stderr:``}}},c={name:`fold`,flags:[{flag:`-w`,type:`value`,valueHint:`number`},{flag:`-s`,type:`boolean`},{flag:`-b`,type:`boolean`}],stdinType:`text`,needsFiles:!0};export{c as n,s as t};
//# sourceMappingURL=chunk-D4J545R4-KlK3EOFl.mjs.map