@bomb.sh/tab 0.0.11 → 0.0.12

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.
package/README.md CHANGED
@@ -14,7 +14,9 @@ tab solves this complexity by providing autocompletions that work consistently a
14
14
 
15
15
  ## Installation
16
16
 
17
- ### For Package Manager Completions (Global install is recommended)
17
+ ### For Package Manager Completions
18
+
19
+ > **Note:** Global install is recommended
18
20
 
19
21
  ```bash
20
22
  npm install -g @bomb.sh/tab
@@ -97,10 +99,10 @@ As mentioned earlier, tab provides completions for package managers as well:
97
99
 
98
100
  ```bash
99
101
  # Generate and install completion scripts
100
- npx @bomb.sh/tab pnpm zsh > ~/.pnpm-completion.zsh && echo 'source ~/.pnpm-completion.zsh' >> ~/.zshrc
101
- npx @bomb.sh/tab npm bash > ~/.npm-completion.bash && echo 'source ~/.npm-completion.bash' >> ~/.bashrc
102
- npx @bomb.sh/tab yarn fish > ~/.config/fish/completions/yarn.fish
103
- npx @bomb.sh/tab bun powershell > ~/.bun-completion.ps1 && echo '. ~/.bun-completion.ps1' >> $PROFILE
102
+ tab pnpm zsh > ~/.pnpm-completion.zsh && echo 'source ~/.pnpm-completion.zsh' >> ~/.zshrc
103
+ tab npm bash > ~/.npm-completion.bash && echo 'source ~/.npm-completion.bash' >> ~/.bashrc
104
+ tab yarn fish > ~/.config/fish/completions/yarn.fish
105
+ tab bun powershell > ~/.bun-completion.ps1 && echo '. ~/.bun-completion.ps1' >> $PROFILE
104
106
  ```
105
107
 
106
108
  Example in action:
package/dist/bin/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{i as e,o as t}from"../t-BWhsyajM.js";import{readFileSync as n}from"node:fs";import{promisify as r}from"node:util";import i from"node:child_process";import{readFileSync as a}from"fs";import{spawnSync as o}from"child_process";function s(){try{let e;try{e=n(`pnpm-workspace.yaml`,`utf8`)}catch{e=n(`pnpm-workspace.yml`,`utf8`)}let t=e.match(/packages:\s*\n((?:\s*-\s*.+\n?)*)/);return t?t[1].split(`
2
+ import{i as e,o as t}from"../t-DYl0qC9V.js";import{readFileSync as n}from"node:fs";import{promisify as r}from"node:util";import i from"node:child_process";import{readFileSync as a}from"fs";import{spawnSync as o}from"child_process";function s(){try{let e;try{e=n(`pnpm-workspace.yaml`,`utf8`)}catch{e=n(`pnpm-workspace.yml`,`utf8`)}let t=e.match(/packages:\s*\n((?:\s*-\s*.+\n?)*)/);return t?t[1].split(`
3
3
  `).map(e=>e.trim()).filter(e=>e.startsWith(`-`)).map(e=>e.substring(1).trim()).filter(e=>e&&!e.startsWith(`#`)):[]}catch{return[]}}function c(){try{let e=JSON.parse(a(`package.json`,`utf8`));return Object.keys(e.scripts||{})}catch{return[]}}function l(){try{let e=JSON.parse(a(`package.json`,`utf8`)),t={...e.dependencies,...e.devDependencies,...e.peerDependencies,...e.optionalDependencies};return Object.keys(t)}catch{return[]}}const ee=async e=>{c().forEach(t=>{e(t.replace(/:/g,`\\:`),` `)})},te=async e=>{l().forEach(t=>e(t,``))},ne=r(i.exec),{execSync:re}=i,u={workspace(e){s().forEach(t=>e(t,`Workspace pattern: ${t}`))}};function d(e,t,n,r){e._lazyCommand=t,e._optionsLoaded=!1;let i=e.options;e.optionsRaw=i,Object.defineProperty(e,`options`,{get(){return this._optionsLoaded||(this._optionsLoaded=!0,r(this,this._lazyCommand)),i},configurable:!0})}function f(e,t,n){[`remove`,`rm`,`uninstall`,`un`,`update`,`up`].includes(t)&&e.argument(`package`,te),[`run`,`run-script`].includes(t)&&e.argument(`script`,ee,!0)}async function p(e,t={}){try{let{stdout:n}=await ne(e,{encoding:`utf8`,timeout:500,maxBuffer:4*1024*1024,...t});return n}catch(e){return e instanceof Error&&`stdout`in e?e.stdout:``}}function m(e,t={}){try{return re(e,{encoding:`utf8`,timeout:500,...t})}catch(e){return e?.stdout?e.stdout:``}}function h(e){return t=>e.forEach(e=>t(e,` `))}const ie=/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g,g=/^\s+([a-z][a-z\s,-]*?)\s{2,}(\S.*)$/i,_=/^\s*(?:-(?<short>[A-Za-z]),\s*)?--(?<long>[a-z0-9-]+)(?<val>\s+(?:<[^>]+>|\[[^\]]+\]))?\s{2,}(?<desc>\S.*)$/i,ae=/^\s*(?:-[A-Za-z],\s*)?--[a-z0-9-]+/i,v=e=>e.replace(ie,``),y=e=>(e.match(/^\s*/)||[``])[0].length,oe=e=>e.split(`,`).map(e=>e.trim()).filter(Boolean),b=/^\s*Options:/i,x=/(?:levels?|options?|values?)[^:]*:\s*([^.]+)/i,S=/\r?\n/,se=/[,\s]+/,C=/^\s*(?:-\w,?\s*)?--(\w+(?:-\w+)*)\s+(\w+(?:-\w+)*)\s+(.+)$/,w=/^\s*-\w,?\s*--\w+(?:,\s*--(\w+(?:-\w+)*)\s+(\w+(?:-\w+)*))?\s+(.+)$/,T=/^\s{20,}/,E=/^\s*[A-Z][^:]*:\s*$/;function D(e){return v(e).split(S)}function ce(e){let t=1/0;for(let n of e){let e=n.match(g);if(!e)continue;let r=n.indexOf(e[2]);r>=0&&r<t&&(t=r)}return t}function le(e,t){let n=1/0;for(let r of e){let e=r.match(_);if(!e||t&&e.groups?.val)continue;let i=r.indexOf(e.groups.desc);i>=0&&i<n&&(n=i)}return n}function O(e,t){let n=D(e),r=[];for(let e=0;e<n.length;e++){let i=n[e],a=i.match(C);if(a){let[,i,o,s]=a;if(i===t){let t=s.trim(),i=e+1;for(;i<n.length;){let e=n[i],r=T.test(e),a=C.test(e)||w.test(e),o=!e.trim()||E.test(e);if(r&&!a&&!o)t+=` `+e.trim(),i++;else break}r.push({value:o,desc:t})}}let o=i.match(w);if(o){let[,i,a,s]=o;if(i===t&&a){let t=s.trim(),i=e+1;for(;i<n.length;){let e=n[i],r=T.test(e),a=C.test(e)||w.test(e),o=!e.trim()||E.test(e);if(r&&!a&&!o)t+=` `+e.trim(),i++;else break}r.push({value:a,desc:t})}}}if(r.length)return r;for(let e=0;e<n.length;e++){let r=n[e];if(r.includes(`--${t}`)||r.includes(`${t}:`))for(let t=e;t<Math.min(e+3,n.length);t++){let e=n[t].match(x);if(e)return e[1].split(se).map(e=>e.trim()).filter(e=>e&&!e.includes(`(`)&&!e.includes(`)`)).map(e=>({value:e,desc:`Log level: ${e}`}))}}return[]}const k={...u,loglevel(e){let t=O(m(`pnpm install --help`),`loglevel`);t.length?t.forEach(({value:t,desc:n})=>e(t,n)):h([`debug`,`info`,`warn`,`error`,`silent`])(e)},reporter(e){let t=O(m(`pnpm install --help`),`reporter`);t.length?t.forEach(({value:t,desc:n})=>e(t,n)):h([`default`,`append-only`,`ndjson`,`silent`])(e)},filter(e){e(`.`,`Current working directory`),e(`!<selector>`,`Exclude packages matching selector`),s().forEach(t=>{e(t,`Workspace pattern: ${t}`),e(`${t}...`,`Include dependencies of ${t}`)}),e(`@*/*`,`All scoped packages`),e(`...<pattern>`,`Include dependencies of pattern`),e(`<pattern>...`,`Include dependents of pattern`)}};function ue(e){let t=D(e),n=ce(t);if(!Number.isFinite(n))return{};let r=null,i=new Map,a=()=>{if(!r)return;let e=r.desc.trim();for(let t of r.names)i.set(t,e);r=null};for(let e of t){if(b.test(e))break;let t=e.match(g);if(t){a(),r={names:oe(t[1]),desc:t[2].trim()};continue}r&&y(e)>=n&&e.trim()&&(r.desc+=` `+e.trim())}return a(),Object.fromEntries(i)}async function de(){let e=await p(`pnpm --help`);return e?ue(e):{}}function fe(e,{flagsOnly:t=!0}={}){let n=D(e),r=le(n,t);if(!Number.isFinite(r))return[];let i=[],a=null,o=()=>{a&&=(a.desc=a.desc.trim(),i.push(a),null)};for(let e of n){let n=e.match(_);if(n){if(t&&n.groups?.val)continue;o(),a={short:n.groups?.short||void 0,long:n.groups.long,desc:n.groups.desc.trim()};continue}if(a){let t=y(e),n=ae.test(e);t>=r&&e.trim()&&!n&&(a.desc+=` `+e.trim())}}return o(),i}function A(e,t){let n=m(`pnpm ${t} --help`);if(!n)return;let r=fe(n,{flagsOnly:!1});for(let{long:t,short:n,desc:i}of r){if(e.optionsRaw?.get?.(t))continue;let r=k[t];r?e.option(t,i,r,n):e.option(t,i,n)}for(let[t,r]of Object.entries(k))e.optionsRaw?.get?.(t)||O(n,t).length>0&&e.option(t,` `,r)}async function j(e){try{let t=await de();for(let[n,r]of Object.entries(t)){let t=e.command(n,r);f(t,n,`pnpm`),d(t,n,`pnpm`,A)}}catch{}}const M=/^All commands:\s*$/i,N=/^Options:\s*$/i,P=/^(aliases|run|more)/i,F=/^[a-z][a-z0-9-]*$/,I=/(?:\[)?(?:-([a-z])\|)?--([a-z][a-z0-9-]+)(?:\s+<[^>]+>)?(?:\])?/gi,L=/<[^>]+>/,R=/^\s/;function z(e){return v(e).split(/\r?\n/)}function B(e,t){let n=e.findIndex(e=>t.test(e.trim()));if(n===-1)return``;let r=``;for(let t=n+1;t<e.length;t++){let n=e[t];if(!R.test(n)&&n.trim()&&!n.includes(`,`))break;R.test(n)&&(r+=` `+n.trim())}return r}const V=(e,t=()=>` `)=>n=>e.forEach(e=>n(e,t(e))),pe={...u,loglevel:h([`silent`,`error`,`warn`,`notice`,`http`,`info`,`verbose`,`silly`]),"install-strategy":V([`hoisted`,`nested`,`shallow`,`linked`],()=>` `),omit:V([`dev`,`optional`,`peer`],()=>` `),include:V([`prod`,`dev`,`optional`,`peer`],()=>` `)};function me(e){let t=B(z(e),M);if(!t)return{};let n={};return t.split(`,`).map(e=>e.trim()).filter(e=>e&&F.test(e)).forEach(e=>{n[e]=` `}),n.run=` `,n}async function he(){let e=await p(`npm --help`);return e?me(e):{}}function ge(e,{flagsOnly:t=!0}={}){let n=z(e),r=n.findIndex(e=>N.test(e.trim()));if(r===-1)return[];let i=[];for(let e of n.slice(r+1)){let n=e.trim();if(P.test(n))break;let r=e.matchAll(I);for(let e of r){let n=e[1]||void 0,r=e[2],a=L.test(e[0]);t&&a||i.push({short:n,long:r,desc:` `})}}return i}function _e(e,t){let n=m(`npm ${t} --help`);if(!n)return;let r=ge(n,{flagsOnly:!1});for(let{long:t,short:n,desc:i}of r){if(e.optionsRaw?.get?.(t))continue;let r=pe[t];r?e.option(t,i,r,n):e.option(t,i,n)}}async function ve(e){try{let t=await he();for(let[n,r]of Object.entries(t)){let t=e.command(n,r);f(t,n,`npm`),d(t,n,`npm`,_e)}}catch{}}const ye=/^\s*Options:\s*$/i,be=/^\s*Commands:\s*$/i,H=/^(Run `yarn help|Visit https:\/\/)/i,xe=/\r?\n/,Se=/^\s*(?:-([a-zA-Z]),?\s*)?--([a-z][a-z0-9-]*)(?:\s+<[^>]+>|\s+\[[^\]]+\])?\s+(.+)$/,Ce=/^\s*-\s+([a-z][a-z0-9-]*(?:\s*\/\s*[a-z][a-zA-Z]*)*)/;function U(e){return v(e).split(xe)}function W(e,t){for(let n=0;n<e.length;n++)if(t.test(e[n].trim()))return n+1;return-1}const we={...u,emoji(e){e(`true`,` `),e(`false`,` `)},production(e){e(`true`,` `),e(`false`,` `)},"scripts-prepend-node-path"(e){e(`true`,` `),e(`false`,` `)}};function Te(e){let t=U(e),n={},r=W(t,be);if(r===-1)return n;for(let e=r;e<t.length;e++){let r=t[e];if(H.test(r))break;if(!r.trim())continue;let i=r.match(Ce);if(i){let[,e]=i,t=e.split(/\s*\/\s*/),r=t[0].trim();if(r){n[r]=``;for(let e=1;e<t.length;e++){let r=t[e].trim();r&&(n[r]=``)}}}}return n}async function Ee(){let e=await p(`yarn --help`);return e?Te(e):{}}function De(e,{flagsOnly:t=!0}={}){let n=U(e),r=[],i=W(n,ye);if(i===-1)return r;for(let e=i;e<n.length;e++){let i=n[e];if(H.test(i.trim()))break;let a=i.match(Se);if(!a)continue;let[,o,s,c]=a,l=i.includes(`<`)||i.includes(`[`);t&&l||r.push({short:o||void 0,long:s,desc:c.trim()})}return r}function Oe(e,t){let n=m(`yarn ${t} --help`);if(!n)return;let r=De(n,{flagsOnly:!1});for(let{long:t,short:n,desc:i}of r){if(e.optionsRaw?.get?.(t))continue;let r=we[t];r?e.option(t,i,r,n):e.option(t,i,n)}}async function ke(e){try{let t=await Ee();for(let[n,r]of Object.entries(t)){let t=e.command(n,r);f(t,n,`yarn`),d(t,n,`yarn`,Oe)}}catch{}}const Ae=/^Commands:\s*$/i,G=/^Flags:\s*$/i,je=/^(Examples|Full documentation|Learn more)/i,K=/^[a-z][a-z0-9-]*$/,Me=/^\s*(?:-([a-zA-Z]),?\s*)?--([a-z][a-z0-9-]*)(?:=<[^>]+>)?\s+(.+)$/,Ne=/^ ([a-z][a-z0-9-]*)\s+(.+)$/,Pe=/^\s{12,}([a-z][a-z0-9-]*)\s+(.+)$/,Fe=/^\s+[a-z]/,Ie=/\s{2,}/,q=/^[A-Z]/,Le=/\r?\n/;function J(e){return v(e).split(Le)}function Y(e,t){for(let n=0;n<e.length;n++)if(t.test(e[n].trim()))return n+1;return-1}const Re={...u,backend(e){e(`clonefile`,` `),e(`hardlink`,` `),e(`symlink`,` `),e(`copyfile`,` `)},linker(e){e(`isolated`,` `),e(`hoisted`,` `)},omit(e){e(`dev`,` `),e(`optional`,` `),e(`peer`,` `)},shell(e){e(`bun`,` `),e(`system`,` `)},"unhandled-rejections"(e){e(`strict`,` `),e(`throw`,` `),e(`warn`,` `),e(`none`,` `),e(`warn-with-error-code`,` `)}};function ze(e){let t=J(e),n=Y(t,Ae);if(n===-1)return{};let r={};for(let e=n;e<t.length;e++){let n=t[e];if(G.test(n.trim())||n.trim()===``&&e+1<t.length&&!t[e+1].match(Fe))break;if(!n.trim())continue;let i=n.match(Ne);if(i){let[,e,t]=i;if(K.test(e)){let n=t.split(Ie),i=n[n.length-1];if(i&&q.test(i))r[e]=i.trim();else if(n.length>1){for(let t of n)if(q.test(t)){r[e]=t.trim();break}}}}let a=n.match(Pe);if(a){let[,e,t]=a;K.test(e)&&(r[e]=t.trim())}}return r}async function Be(){let e=await p(`bun --help`);return e?ze(e):{}}function Ve(e,{flagsOnly:t=!0}={}){let n=J(e),r=[],i=Y(n,G);if(i===-1)return r;for(let e=i;e<n.length;e++){let i=n[e];if(je.test(i.trim()))break;let a=i.match(Me);if(!a)continue;let[,o,s,c]=a,l=i.includes(`=<`);t&&l||r.push({short:o||void 0,long:s,desc:c.trim()})}return r}function He(e,t){let n=m(`bun ${t} --help`);if(!n)return;let r=Ve(n,{flagsOnly:!1});for(let{long:t,short:n,desc:i}of r){if(e.optionsRaw?.get?.(t))continue;let r=Re[t];r?e.option(t,i,r,n):e.option(t,i,n)}}async function Ue(e){try{let t=await Be();for(let[n,r]of Object.entries(t)){let t=e.command(n,r);f(t,n,`bun`),d(t,n,`bun`,He)}}catch{}}async function We(e,t){switch(e){case`pnpm`:await j(t);break;case`npm`:await ve(t);break;case`yarn`:await ke(t);break;case`bun`:await Ue(t);break;default:break}}function X(...e){process.env.DEBUG&&console.error(`[DEBUG]`,...e)}const Ge={encoding:`utf8`,stdio:[`pipe`,`pipe`,`ignore`],timeout:1e3};function Z(e,t,n){let r=o(e,[...t,`complete`,`--`,...n],Ge);if(r.error)throw r.error;if(typeof r.status==`number`&&r.status!==0)throw Error(`Completion command "${e}" exited with code ${r.status}`);return(r.stdout??``).trim()}async function Ke(e,t){try{if(Z(e,[],[]))return!0}catch{}try{return!!Z(t,[e],[])}catch{return!1}}async function qe(e,t,n){try{let t=Z(e,[],n);if(t)return t.split(`
4
4
  `).filter(Boolean)}catch{}try{return Z(t,[e],n).split(`
5
5
  `).filter(Boolean)}catch{return[]}}var Je=class extends e{packageManager;constructor(e){super(),this.packageManager=e}stripPackageManagerCommands(e){return e.length===0?e:[`exec`,`x`,`run`,`dlx`].includes(e[0])?e.slice(1):e}async parse(e){let t=this.stripPackageManagerCommands(e);if(t.length>=1&&t[0].trim()!==``){let e=t[0];if(![...this.commands.keys()].includes(e)&&await Ke(e,this.packageManager)){let n=t.slice(1),r=await qe(e,this.packageManager,n);if(r.length>0){X(`Returning ${r.length} completions for ${e}`);for(let e of r)if(!e.startsWith(`:`))if(e.includes(` `)){let[t,n]=e.split(` `);console.log(`${t}\t${n}`)}else console.log(e);console.log(`:4`);return}}}return super.parse(e)}};const Q=[`npm`,`pnpm`,`yarn`,`bun`],$=[`zsh`,`bash`,`fish`,`powershell`];async function Ye(){let e=process.argv.slice(2),t=process.platform===`win32`&&process.env.PSModulePath;if(e.length>=2&&e[1]===`complete`){let n=e[0];Q.includes(n)||(console.error(`Error: Unsupported package manager "${n}"`),console.error(`Supported package managers: ${Q.join(`, `)}`),process.exit(1));let r=process.argv.indexOf(`--`),i=r!==-1&&(!t||r<process.argv.length-1)?process.argv.slice(r+1):t?e.slice(2):null;i||(console.error(`Error: Expected '--' followed by command to complete`),process.exit(1));let a=new Je(n);await We(n,a),await a.parse(i),process.exit(0)}if(e.length===2){let[t,n]=e;Q.includes(t)||(console.error(`Error: Unsupported package manager "${t}"`),console.error(`Supported package managers: ${Q.join(`, `)}`),process.exit(1)),$.includes(n)||(console.error(`Error: Unsupported shell "${n}"`),console.error(`Supported shells: ${$.join(`, `)}`),process.exit(1)),Xe(t,n),process.exit(0)}console.error(`Usage: tab <packageManager> <shell>`),console.error(` tab <packageManager> complete -- <args>`),process.exit(1)}function Xe(e,n){t(n,e,process.argv[1].endsWith(`dist/bin/cli.js`)?`node ${process.argv[1]} ${e}`:`tab ${e}`)}Ye().catch(console.error);export{};
package/dist/cac.js CHANGED
@@ -1 +1 @@
1
- import{c as e,d as t,l as n,s as r,u as i}from"./t-BWhsyajM.js";import{t as a}from"./shared-BE1U9MBi.js";const o=process.execPath,s=process.argv.slice(1),c=f(o),l=s.map(f),u=`${c} ${process.execArgv.map(f).join(` `)} ${l[0]}`,d=/<[^>]+>|\[[^\]]+\]/;function f(e){return e.includes(` `)?`'${e}'`:e}async function p(c,l){for(let e of[c.globalCommand,...c.commands]){if(e.name===`complete`)continue;let t=(e.rawName.match(/\[.*?\]|<.*?>/g)||[]).map(e=>e.startsWith(`[`)),n=e.name===`@@global@@`,i=n?l:l?.subCommands?.[e.name],a=n?``:e.name,o=n?r:r.command(a,e.description||``);if(o){let n=(e.rawName.match(/<([^>]+)>|\[\.\.\.([^\]]+)\]/g)||[]).map(e=>e.startsWith(`<`)&&e.endsWith(`>`)?e.slice(1,-1):e.startsWith(`[...`)&&e.endsWith(`]`)?e.slice(4,-1):e);t.forEach((e,t)=>{let r=n[t]||`arg${t}`,a=i?.args?.[r];a?o.argument(r,a,e):o.argument(r,void 0,e)})}for(let t of[...c.globalCommand.options,...e.options]){let e=t.rawName.match(/^-([a-zA-Z]), --/)?.[1],a=t.name,s=n?r:o;if(s){let n=i?.options?.[a],r=t.required||d.test(t.rawName);n?e?s.option(a,t.description||``,n,e):s.option(a,t.description||``,n):r?e?s.option(a,t.description||``,async()=>[],e):s.option(a,t.description||``,async()=>[]):e?s.option(a,t.description||``,e):s.option(a,t.description||``)}}}return c.command(`complete [shell]`).action(async(l,d)=>{switch(l){case`zsh`:{let e=t(c.name,u);console.log(e);break}case`bash`:{let e=i(c.name,u);console.log(e);break}case`fish`:{let e=n(c.name,u);console.log(e);break}case`powershell`:{let t=e(c.name,u);console.log(t);break}default:{a(c.name);let e=d[`--`]||[];return c.showHelpOnExit=!1,c.unsetMatchedCommand(),c.parse([o,s[0],...e],{run:!1}),r.parse(e)}}}),r}export{p as default};
1
+ import{c as e,d as t,l as n,s as r,u as i}from"./t-DYl0qC9V.js";import{t as a}from"./shared-BE1U9MBi.js";const o=process.execPath,s=process.argv.slice(1),c=f(o),l=s.map(f),u=`${c} ${process.execArgv.map(f).join(` `)} ${l[0]}`,d=/<[^>]+>|\[[^\]]+\]/;function f(e){return e.includes(` `)?`'${e}'`:e}async function p(c,l){for(let e of[c.globalCommand,...c.commands]){if(e.name===`complete`)continue;let t=(e.rawName.match(/\[.*?\]|<.*?>/g)||[]).map(e=>e.startsWith(`[`)),n=e.name===`@@global@@`,i=n?l:l?.subCommands?.[e.name],a=n?``:e.name,o=n?r:r.command(a,e.description||``);if(o){let n=(e.rawName.match(/<([^>]+)>|\[\.\.\.([^\]]+)\]/g)||[]).map(e=>e.startsWith(`<`)&&e.endsWith(`>`)?e.slice(1,-1):e.startsWith(`[...`)&&e.endsWith(`]`)?e.slice(4,-1):e);t.forEach((e,t)=>{let r=n[t]||`arg${t}`,a=i?.args?.[r];a?o.argument(r,a,e):o.argument(r,void 0,e)})}for(let t of[...c.globalCommand.options,...e.options]){let e=t.rawName.match(/^-([a-zA-Z]), --/)?.[1],a=t.name,s=n?r:o;if(s){let n=i?.options?.[a],r=t.required||d.test(t.rawName);n?e?s.option(a,t.description||``,n,e):s.option(a,t.description||``,n):r?e?s.option(a,t.description||``,async()=>[],e):s.option(a,t.description||``,async()=>[]):e?s.option(a,t.description||``,e):s.option(a,t.description||``)}}}return c.command(`complete [shell]`).action(async(l,d)=>{switch(l){case`zsh`:{let e=t(c.name,u);console.log(e);break}case`bash`:{let e=i(c.name,u);console.log(e);break}case`fish`:{let e=n(c.name,u);console.log(e);break}case`powershell`:{let t=e(c.name,u);console.log(t);break}default:{a(c.name);let e=d[`--`]||[];return c.showHelpOnExit=!1,c.unsetMatchedCommand(),c.parse([o,s[0],...e],{run:!1}),r.parse(e)}}}),r}export{p as default};
package/dist/citty.js CHANGED
@@ -1 +1 @@
1
- import{c as e,d as t,l as n,s as r,u as i}from"./t-BWhsyajM.js";import{t as a}from"./shared-BE1U9MBi.js";import{defineCommand as o}from"citty";async function s(e){let t=[],n=[];for(let[r,i]of Object.entries(e))if(i.type===`positional`){let e=i;n.push({name:r,description:e.description,isOptional:!e.required,isVariadic:r.startsWith(`[...`)||r.startsWith(`<...`)})}else{let e={name:`--${r}`,description:i.description||``,isRequired:i.required};`alias`in i&&i.alias&&(Array.isArray(i.alias)?i.alias:[i.alias]).forEach(n=>{t.push({...e,name:`-${n}`})}),t.push(e)}return{options:t,args:n}}async function c(e,t=``){let n=await Promise.resolve(e.meta),r=await Promise.resolve(e.subCommands);if(!n||!n.name)throw Error(`Command meta or name is missing`);let i={name:t?`${t} ${n.name}`:n.name,description:n.description||``};if(e.args){let{options:t,args:n}=await s(await Promise.resolve(e.args));t.length>0&&(i.options=t),n.length>0&&(i.args=n)}return r&&(i.subcommands=await Promise.all(Object.entries(r).map(async([e,t])=>c(await Promise.resolve(t),i.name)))),i}async function l(e){let t=await c(e);return JSON.stringify(t,null,2)}function u(e){return e.includes(` `)?`'${e}'`:e}const d=process.execPath,f=process.argv.slice(1),p=u(d),m=f.map(u),h=`${p} ${process.execArgv.map(u).join(` `)} ${m[0]}`;function g(e){return e.args&&Object.values(e.args).some(e=>e.type===`positional`)}async function _(e,t,n){for(let[i,a]of Object.entries(e)){let e=await y(a),o=await y(e.meta),s=await y(e.subCommands),c=n?.[i];if(!o||typeof o?.description!=`string`)throw Error(`Invalid meta or missing description.`);let l=g(e),u=t?`${t} ${i}`:i,d=r.command(u,o.description);if(l&&e.args)for(let[t,n]of Object.entries(e.args)){let e=n;if(e.type===`positional`){let n=e.required===!1,r=c?.args?.[t];r?d.argument(t,r,n):d.argument(t,void 0,n)}}if(s&&await _(s,u,c?.subCommands),e.args)for(let[t,n]of Object.entries(e.args)){let e=n,r=typeof e==`object`&&`alias`in e?Array.isArray(e.alias)?e.alias[0]:e.alias:void 0,i=c?.options?.[t];i?r?d.option(t,e.description??``,i,r):d.option(t,e.description??``,i):r?d.option(t,e.description??``,r):d.option(t,e.description??``)}}}async function v(s,c){let u=await y(s.meta);if(!u)throw Error(`Invalid meta.`);let d=u.name;if(!d)throw Error(`Invalid meta or missing name.`);let f=await y(s.subCommands);if(g(s)&&s.args)for(let[e,t]of Object.entries(s.args)){let n=t;if(n.type===`positional`){let t=n.required===!1,i=c?.args?.[e];i?r.argument(e,i,t):r.argument(e,void 0,t)}}if(f&&await _(f,void 0,c?.subCommands),s.args)for(let[e,t]of Object.entries(s.args)){let n=t,i=typeof n==`object`&&`alias`in n?Array.isArray(n.alias)?n.alias[0]:n.alias:void 0,a=c?.options?.[e];a?i?r.option(e,n.description??``,a,i):r.option(e,n.description??``,a):i?r.option(e,n.description??``,i):r.option(e,n.description??``)}let p=o({meta:{name:`complete`,description:`Generate shell completion scripts`},args:{shell:{type:`positional`,description:`Shell type (zsh, bash, fish, powershell, fig)`,required:!1}},async run(o){let c=o.rawArgs[0];switch(c===`--`&&(c=void 0),c){case`zsh`:{let e=t(d,h);console.log(e);break}case`bash`:{let e=i(d,h);console.log(e);break}case`fish`:{let e=n(d,h);console.log(e);break}case`powershell`:{let t=e(d,h);console.log(t);break}case`fig`:{let e=await l(s);console.log(e);break}default:{a(d);let e=o.rawArgs.slice(o.rawArgs.indexOf(`--`)+1);return r.parse(e)}}}});return f?f.complete=p:s.subCommands={complete:p},r}async function y(e){return e instanceof Function?await e():await e}export{v as default};
1
+ import{c as e,d as t,l as n,s as r,u as i}from"./t-DYl0qC9V.js";import{t as a}from"./shared-BE1U9MBi.js";import{defineCommand as o}from"citty";async function s(e){let t=[],n=[];for(let[r,i]of Object.entries(e))if(i.type===`positional`){let e=i;n.push({name:r,description:e.description,isOptional:!e.required,isVariadic:r.startsWith(`[...`)||r.startsWith(`<...`)})}else{let e={name:`--${r}`,description:i.description||``,isRequired:i.required};`alias`in i&&i.alias&&(Array.isArray(i.alias)?i.alias:[i.alias]).forEach(n=>{t.push({...e,name:`-${n}`})}),t.push(e)}return{options:t,args:n}}async function c(e,t=``){let n=await Promise.resolve(e.meta),r=await Promise.resolve(e.subCommands);if(!n||!n.name)throw Error(`Command meta or name is missing`);let i={name:t?`${t} ${n.name}`:n.name,description:n.description||``};if(e.args){let{options:t,args:n}=await s(await Promise.resolve(e.args));t.length>0&&(i.options=t),n.length>0&&(i.args=n)}return r&&(i.subcommands=await Promise.all(Object.entries(r).map(async([e,t])=>c(await Promise.resolve(t),i.name)))),i}async function l(e){let t=await c(e);return JSON.stringify(t,null,2)}function u(e){return e.includes(` `)?`'${e}'`:e}const d=process.execPath,f=process.argv.slice(1),p=u(d),m=f.map(u),h=`${p} ${process.execArgv.map(u).join(` `)} ${m[0]}`;function g(e){return e.args&&Object.values(e.args).some(e=>e.type===`positional`)}async function _(e,t,n){for(let[i,a]of Object.entries(e)){let e=await y(a),o=await y(e.meta),s=await y(e.subCommands),c=n?.[i];if(!o||typeof o?.description!=`string`)throw Error(`Invalid meta or missing description.`);let l=g(e),u=t?`${t} ${i}`:i,d=r.command(u,o.description);if(l&&e.args)for(let[t,n]of Object.entries(e.args)){let e=n;if(e.type===`positional`){let n=e.required===!1,r=c?.args?.[t];r?d.argument(t,r,n):d.argument(t,void 0,n)}}if(s&&await _(s,u,c?.subCommands),e.args)for(let[t,n]of Object.entries(e.args)){let e=n,r=typeof e==`object`&&`alias`in e?Array.isArray(e.alias)?e.alias[0]:e.alias:void 0,i=c?.options?.[t];i?r?d.option(t,e.description??``,i,r):d.option(t,e.description??``,i):r?d.option(t,e.description??``,r):d.option(t,e.description??``)}}}async function v(s,c){let u=await y(s.meta);if(!u)throw Error(`Invalid meta.`);let d=u.name;if(!d)throw Error(`Invalid meta or missing name.`);let f=await y(s.subCommands);if(g(s)&&s.args)for(let[e,t]of Object.entries(s.args)){let n=t;if(n.type===`positional`){let t=n.required===!1,i=c?.args?.[e];i?r.argument(e,i,t):r.argument(e,void 0,t)}}if(f&&await _(f,void 0,c?.subCommands),s.args)for(let[e,t]of Object.entries(s.args)){let n=t,i=typeof n==`object`&&`alias`in n?Array.isArray(n.alias)?n.alias[0]:n.alias:void 0,a=c?.options?.[e];a?i?r.option(e,n.description??``,a,i):r.option(e,n.description??``,a):i?r.option(e,n.description??``,i):r.option(e,n.description??``)}let p=o({meta:{name:`complete`,description:`Generate shell completion scripts`},args:{shell:{type:`positional`,description:`Shell type (zsh, bash, fish, powershell, fig)`,required:!1}},async run(o){let c=o.rawArgs[0];switch(c===`--`&&(c=void 0),c){case`zsh`:{let e=t(d,h);console.log(e);break}case`bash`:{let e=i(d,h);console.log(e);break}case`fish`:{let e=n(d,h);console.log(e);break}case`powershell`:{let t=e(d,h);console.log(t);break}case`fig`:{let e=await l(s);console.log(e);break}default:{a(d);let e=o.rawArgs.slice(o.rawArgs.indexOf(`--`)+1);return r.parse(e)}}}});return f?f.complete=p:s.subCommands={complete:p},r}async function y(e){return e instanceof Function?await e():await e}export{v as default};
package/dist/commander.js CHANGED
@@ -1 +1 @@
1
- import{c as e,d as t,l as n,s as r,u as i}from"./t-BWhsyajM.js";import{t as a}from"./shared-BE1U9MBi.js";const o=process.execPath,s=process.argv.slice(1),c=d(o),l=s.map(d),u=`${c} ${process.execArgv.map(d).join(` `)} ${l[0]}`;function d(e){return e.includes(` `)?`'${e}'`:e}function f(o){let s=o.name();p(o),m(o),o.command(`complete [shell]`).description(`Generate shell completion scripts`).action(async r=>{switch(r){case`zsh`:{let e=t(s,u);console.log(e);break}case`bash`:{let e=i(s,u);console.log(e);break}case`fish`:{let e=n(s,u);console.log(e);break}case`powershell`:{let t=e(s,u);console.log(t);break}case`debug`:{let e=new Map;h(o,``,e),console.log(`Collected commands:`);for(let[t,n]of e.entries())console.log(`- ${t||`<root>`}: ${n.description()||`No description`}`);break}default:console.error(`Unknown shell: ${r}`),console.error(`Supported shells: zsh, bash, fish, powershell`),process.exit(1)}});let c=o.parse.bind(o);return o.parse=function(e,t){let n=e||process.argv,i=n.findIndex(e=>e===`complete`),l=n.findIndex(e=>e===`--`);if(i!==-1&&l!==-1&&l>i){let e=n.slice(l+1);return a(s),r.parse(e),o}return c(e,t)},r}function p(e){for(let t of e.options){let e=t.flags,n=e.match(/^-([a-zA-Z]), --/)?.[1],i=e.match(/--([a-zA-Z0-9-]+)/)?.[1];i&&(n?r.option(i,t.description||``,n):r.option(i,t.description||``))}}function m(e){let t=new Map;h(e,``,t);for(let[e,n]of t.entries()){if(e===``)continue;let t=r.command(e,n.description()||``);for(let e of n.options){let n=e.flags,r=n.match(/^-([a-zA-Z]), --/)?.[1],i=n.match(/--([a-zA-Z0-9-]+)/)?.[1];i&&(r?t.option(i,e.description||``,r):t.option(i,e.description||``))}}}function h(e,t,n){n.set(t,e);for(let r of e.commands)r.name()!==`complete`&&h(r,t?`${t} ${r.name()}`:r.name(),n)}export{f as default};
1
+ import{c as e,d as t,l as n,s as r,u as i}from"./t-DYl0qC9V.js";import{t as a}from"./shared-BE1U9MBi.js";const o=process.execPath,s=process.argv.slice(1),c=d(o),l=s.map(d),u=`${c} ${process.execArgv.map(d).join(` `)} ${l[0]}`;function d(e){return e.includes(` `)?`'${e}'`:e}function f(o){let s=o.name();p(o),m(o),o.command(`complete [shell]`).description(`Generate shell completion scripts`).action(async r=>{switch(r){case`zsh`:{let e=t(s,u);console.log(e);break}case`bash`:{let e=i(s,u);console.log(e);break}case`fish`:{let e=n(s,u);console.log(e);break}case`powershell`:{let t=e(s,u);console.log(t);break}case`debug`:{let e=new Map;h(o,``,e),console.log(`Collected commands:`);for(let[t,n]of e.entries())console.log(`- ${t||`<root>`}: ${n.description()||`No description`}`);break}default:console.error(`Unknown shell: ${r}`),console.error(`Supported shells: zsh, bash, fish, powershell`),process.exit(1)}});let c=o.parse.bind(o);return o.parse=function(e,t){let n=e||process.argv,i=n.findIndex(e=>e===`complete`),l=n.findIndex(e=>e===`--`);if(i!==-1&&l!==-1&&l>i){let e=n.slice(l+1);return a(s),r.parse(e),o}return c(e,t)},r}function p(e){for(let t of e.options){let e=t.flags,n=e.match(/^-([a-zA-Z]), --/)?.[1],i=e.match(/--([a-zA-Z0-9-]+)/)?.[1];i&&(n?r.option(i,t.description||``,n):r.option(i,t.description||``))}}function m(e){let t=new Map;h(e,``,t);for(let[e,n]of t.entries()){if(e===``)continue;let t=r.command(e,n.description()||``);for(let e of n.options){let n=e.flags,r=n.match(/^-([a-zA-Z]), --/)?.[1],i=n.match(/--([a-zA-Z0-9-]+)/)?.[1];i&&(r?t.option(i,e.description||``,r):t.option(i,e.description||``))}}}function h(e,t,n){n.set(t,e);for(let r of e.commands)r.name()!==`complete`&&h(r,t?`${t} ${r.name()}`:r.name(),n)}export{f as default};
@@ -313,15 +313,7 @@ __${n}_complete() {
313
313
 
314
314
  # Register completion function
315
315
  complete -F __${n}_complete ${e}
316
- `}function r(e,t){let n=e.replace(/[-:]/g,`_`);return`# fish completion for ${e} -*- shell-script -*-
317
-
318
- # Define shell completion directives
319
- set -l ShellCompDirectiveError ${a.ShellCompDirectiveError}
320
- set -l ShellCompDirectiveNoSpace ${a.ShellCompDirectiveNoSpace}
321
- set -l ShellCompDirectiveNoFileComp ${a.ShellCompDirectiveNoFileComp}
322
- set -l ShellCompDirectiveFilterFileExt ${a.ShellCompDirectiveFilterFileExt}
323
- set -l ShellCompDirectiveFilterDirs ${a.ShellCompDirectiveFilterDirs}
324
- set -l ShellCompDirectiveKeepOrder ${a.ShellCompDirectiveKeepOrder}
316
+ `}function r(e,t){let n=e.replace(/[-:]/g,`_`),r=a.ShellCompDirectiveError,i=a.ShellCompDirectiveNoSpace,o=a.ShellCompDirectiveNoFileComp,s=a.ShellCompDirectiveFilterFileExt,c=a.ShellCompDirectiveFilterDirs;return`# fish completion for ${e} -*- shell-script -*-
325
317
 
326
318
  function __${n}_debug
327
319
  set -l file "$BASH_COMP_DEBUG_FILE"
@@ -333,135 +325,228 @@ end
333
325
  function __${n}_perform_completion
334
326
  __${n}_debug "Starting __${n}_perform_completion"
335
327
 
336
- # Extract all args except the completion flag
337
- set -l args (string match -v -- "--completion=" (commandline -opc))
338
-
339
- # Extract the current token being completed
340
- set -l current_token (commandline -ct)
341
-
342
- # Check if current token starts with a dash
343
- set -l flag_prefix ""
344
- if string match -q -- "-*" $current_token
345
- set flag_prefix "--flag="
346
- end
347
-
348
- __${n}_debug "Current token: $current_token"
349
- __${n}_debug "All args: $args"
328
+ # Extract all args except the last one
329
+ set -l args (commandline -opc)
330
+ # Extract the last arg and escape it in case it is a space or wildcard
331
+ set -l lastArg (string escape -- (commandline -ct))
332
+
333
+ __${n}_debug "args: $args"
334
+ __${n}_debug "last arg: $lastArg"
335
+
336
+ # Build the completion request command
337
+ set -l requestComp "${t} complete -- (string join ' ' -- (string escape -- $args[2..-1])) $lastArg"
350
338
 
351
- # Call the completion program and get the results
352
- set -l requestComp "${t} complete -- $args"
353
339
  __${n}_debug "Calling $requestComp"
354
340
  set -l results (eval $requestComp 2> /dev/null)
355
-
341
+
356
342
  # Some programs may output extra empty lines after the directive.
357
343
  # Let's ignore them or else it will break completion.
358
344
  # Ref: https://github.com/spf13/cobra/issues/1279
359
345
  for line in $results[-1..1]
360
- if test (string sub -s 1 -l 1 -- $line) = ":"
361
- # The directive
362
- set -l directive (string sub -s 2 -- $line)
363
- set -l directive_num (math $directive)
346
+ if test (string trim -- $line) = ""
347
+ # Found an empty line, remove it
348
+ set results $results[1..-2]
349
+ else
350
+ # Found non-empty line, we have our proper output
364
351
  break
365
352
  end
366
353
  end
367
-
368
- # No directive specified, use default
369
- if not set -q directive_num
370
- set directive_num 0
354
+
355
+ set -l comps $results[1..-2]
356
+ set -l directiveLine $results[-1]
357
+
358
+ # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
359
+ # completions must be prefixed with the flag
360
+ set -l flagPrefix (string match -r -- '-.*=' "$lastArg")
361
+
362
+ __${n}_debug "Comps: $comps"
363
+ __${n}_debug "DirectiveLine: $directiveLine"
364
+ __${n}_debug "flagPrefix: $flagPrefix"
365
+
366
+ for comp in $comps
367
+ printf "%s%s\\n" "$flagPrefix" "$comp"
371
368
  end
372
-
373
- __${n}_debug "Directive: $directive_num"
374
369
 
375
- # Process completions based on directive
376
- if test $directive_num -eq $ShellCompDirectiveError
377
- # Error code. No completion.
378
- __${n}_debug "Received error directive: aborting."
370
+ printf "%s\\n" "$directiveLine"
371
+ end
372
+
373
+ # This function limits calls to __${n}_perform_completion, by caching the result
374
+ function __${n}_perform_completion_once
375
+ __${n}_debug "Starting __${n}_perform_completion_once"
376
+
377
+ if test -n "$__${n}_perform_completion_once_result"
378
+ __${n}_debug "Seems like a valid result already exists, skipping __${n}_perform_completion"
379
+ return 0
380
+ end
381
+
382
+ set --global __${n}_perform_completion_once_result (__${n}_perform_completion)
383
+ if test -z "$__${n}_perform_completion_once_result"
384
+ __${n}_debug "No completions, probably due to a failure"
379
385
  return 1
380
386
  end
381
387
 
382
- # Filter out the directive (last line)
383
- if test (count $results) -gt 0 -a (string sub -s 1 -l 1 -- $results[-1]) = ":"
384
- set results $results[1..-2]
388
+ __${n}_debug "Performed completions and set __${n}_perform_completion_once_result"
389
+ return 0
390
+ end
391
+
392
+ # This function is used to clear the cached result after completions are run
393
+ function __${n}_clear_perform_completion_once_result
394
+ __${n}_debug ""
395
+ __${n}_debug "========= clearing previously set __${n}_perform_completion_once_result variable =========="
396
+ set --erase __${n}_perform_completion_once_result
397
+ __${n}_debug "Successfully erased the variable __${n}_perform_completion_once_result"
398
+ end
399
+
400
+ function __${n}_requires_order_preservation
401
+ __${n}_debug ""
402
+ __${n}_debug "========= checking if order preservation is required =========="
403
+
404
+ __${n}_perform_completion_once
405
+ if test -z "$__${n}_perform_completion_once_result"
406
+ __${n}_debug "Error determining if order preservation is required"
407
+ return 1
385
408
  end
386
409
 
387
- # No completions, let fish handle file completions unless forbidden
388
- if test (count $results) -eq 0
389
- if test $directive_num -ne $ShellCompDirectiveNoFileComp
390
- __${n}_debug "No completions, performing file completion"
391
- return 1
392
- end
393
- __${n}_debug "No completions, but file completion forbidden"
410
+ set -l directive (string sub --start 2 $__${n}_perform_completion_once_result[-1])
411
+ __${n}_debug "Directive is: $directive"
412
+
413
+ set -l shellCompDirectiveKeepOrder ${a.ShellCompDirectiveKeepOrder}
414
+ set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
415
+ __${n}_debug "Keeporder is: $keeporder"
416
+
417
+ if test $keeporder -ne 0
418
+ __${n}_debug "This does require order preservation"
394
419
  return 0
395
420
  end
396
421
 
397
- # Filter file extensions
398
- if test $directive_num -eq $ShellCompDirectiveFilterFileExt
399
- __${n}_debug "File extension filtering"
400
- set -l file_extensions
401
- for item in $results
402
- if test -n "$item" -a (string sub -s 1 -l 1 -- $item) != "-"
403
- set -a file_extensions "*$item"
404
- end
405
- end
406
- __${n}_debug "File extensions: $file_extensions"
407
-
408
- # Use the file extensions as completions
409
- set -l completions
410
- for ext in $file_extensions
411
- # Get all files matching the extension
412
- set -a completions (string replace -r '^.*/' '' -- $ext)
413
- end
414
-
415
- for item in $completions
416
- echo -e "$item\t"
417
- end
418
- return 0
422
+ __${n}_debug "This doesn't require order preservation"
423
+ return 1
424
+ end
425
+
426
+ # This function does two things:
427
+ # - Obtain the completions and store them in the global __${n}_comp_results
428
+ # - Return false if file completion should be performed
429
+ function __${n}_prepare_completions
430
+ __${n}_debug ""
431
+ __${n}_debug "========= starting completion logic =========="
432
+
433
+ # Start fresh
434
+ set --erase __${n}_comp_results
435
+
436
+ __${n}_perform_completion_once
437
+ __${n}_debug "Completion results: $__${n}_perform_completion_once_result"
438
+
439
+ if test -z "$__${n}_perform_completion_once_result"
440
+ __${n}_debug "No completion, probably due to a failure"
441
+ # Might as well do file completion, in case it helps
442
+ return 1
419
443
  end
420
444
 
421
- # Filter directories
422
- if test $directive_num -eq $ShellCompDirectiveFilterDirs
423
- __${n}_debug "Directory filtering"
424
- set -l dirs
425
- for item in $results
426
- if test -d "$item"
427
- set -a dirs "$item/"
428
- end
429
- end
430
-
431
- for item in $dirs
432
- echo -e "$item\t"
433
- end
434
- return 0
445
+ set -l directive (string sub --start 2 $__${n}_perform_completion_once_result[-1])
446
+ set --global __${n}_comp_results $__${n}_perform_completion_once_result[1..-2]
447
+
448
+ __${n}_debug "Completions are: $__${n}_comp_results"
449
+ __${n}_debug "Directive is: $directive"
450
+
451
+ set -l shellCompDirectiveError ${r}
452
+ set -l shellCompDirectiveNoSpace ${i}
453
+ set -l shellCompDirectiveNoFileComp ${o}
454
+ set -l shellCompDirectiveFilterFileExt ${s}
455
+ set -l shellCompDirectiveFilterDirs ${c}
456
+
457
+ if test -z "$directive"
458
+ set directive 0
435
459
  end
436
460
 
437
- # Process remaining completions
438
- for item in $results
439
- if test -n "$item"
440
- # Check if the item has a description
441
- if string match -q "*\t*" -- "$item"
442
- set -l completion_parts (string split \t -- "$item")
443
- set -l comp $completion_parts[1]
444
- set -l desc $completion_parts[2]
445
-
446
- # Add the completion and description
447
- echo -e "$comp\t$desc"
448
- else
449
- # Add just the completion
450
- echo -e "$item\t"
461
+ set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) % 2)
462
+ if test $compErr -eq 1
463
+ __${n}_debug "Received error directive: aborting."
464
+ # Might as well do file completion, in case it helps
465
+ return 1
466
+ end
467
+
468
+ set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) % 2)
469
+ set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) % 2)
470
+ if test $filefilter -eq 1; or test $dirfilter -eq 1
471
+ __${n}_debug "File extension filtering or directory filtering not supported"
472
+ # Do full file completion instead
473
+ return 1
474
+ end
475
+
476
+ set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) % 2)
477
+ set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) % 2)
478
+
479
+ __${n}_debug "nospace: $nospace, nofiles: $nofiles"
480
+
481
+ # If we want to prevent a space, or if file completion is NOT disabled,
482
+ # we need to count the number of valid completions.
483
+ # To do so, we will filter on prefix as the completions we have received
484
+ # may not already be filtered so as to allow fish to match on different
485
+ # criteria than the prefix.
486
+ if test $nospace -ne 0; or test $nofiles -eq 0
487
+ set -l prefix (commandline -t | string escape --style=regex)
488
+ __${n}_debug "prefix: $prefix"
489
+
490
+ set -l completions (string match -r -- "^$prefix.*" $__${n}_comp_results)
491
+ set --global __${n}_comp_results $completions
492
+ __${n}_debug "Filtered completions are: $__${n}_comp_results"
493
+
494
+ # Important not to quote the variable for count to work
495
+ set -l numComps (count $__${n}_comp_results)
496
+ __${n}_debug "numComps: $numComps"
497
+
498
+ if test $numComps -eq 1; and test $nospace -ne 0
499
+ # We must first split on \\t to get rid of the descriptions to be
500
+ # able to check what the actual completion will be.
501
+ # We don't need descriptions anyway since there is only a single
502
+ # real completion which the shell will expand immediately.
503
+ set -l split (string split --max 1 "\\t" $__${n}_comp_results[1])
504
+
505
+ # Fish won't add a space if the completion ends with any
506
+ # of the following characters: @=/:.,
507
+ set -l lastChar (string sub -s -1 -- $split)
508
+ if not string match -r -q "[@=/:.,]" -- "$lastChar"
509
+ # In other cases, to support the "nospace" directive we trick the shell
510
+ # by outputting an extra, longer completion.
511
+ __${n}_debug "Adding second completion to perform nospace directive"
512
+ set --global __${n}_comp_results $split[1] $split[1].
513
+ __${n}_debug "Completions are now: $__${n}_comp_results"
451
514
  end
452
515
  end
516
+
517
+ if test $numComps -eq 0; and test $nofiles -eq 0
518
+ # To be consistent with bash and zsh, we only trigger file
519
+ # completion when there are no other completions
520
+ __${n}_debug "Requesting file completion"
521
+ return 1
522
+ end
453
523
  end
454
-
455
- # If directive contains NoSpace, tell fish not to add a space after completion
456
- if test (math "$directive_num & $ShellCompDirectiveNoSpace") -ne 0
457
- return 2
458
- end
459
-
524
+
460
525
  return 0
461
526
  end
462
527
 
463
- # Set up the completion for the ${e} command
464
- complete -c ${e} -f -a "(eval __${n}_perform_completion)"
528
+ # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves
529
+ # so we can properly delete any completions provided by another script.
530
+ # Only do this if the program can be found, or else fish may print some errors; besides,
531
+ # the existing completions will only be loaded if the program can be found.
532
+ if type -q "${e}"
533
+ # The space after the program name is essential to trigger completion for the program
534
+ # and not completion of the program name itself.
535
+ # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.
536
+ complete --do-complete "${e} " > /dev/null 2>&1
537
+ end
538
+
539
+ # Remove any pre-existing completions for the program since we will be handling all of them.
540
+ complete -c ${e} -e
541
+
542
+ # This will get called after the two calls below and clear the cached result
543
+ complete -c ${e} -n '__${n}_clear_perform_completion_once_result'
544
+ # The call to __${n}_prepare_completions will setup __${n}_comp_results
545
+ # which provides the program's completion choices.
546
+ # If this doesn't require order preservation, we don't use the -k flag
547
+ complete -c ${e} -n 'not __${n}_requires_order_preservation && __${n}_prepare_completions' -f -a '$__${n}_comp_results'
548
+ # Otherwise we use the -k flag
549
+ complete -k -c ${e} -n '__${n}_requires_order_preservation && __${n}_prepare_completions' -f -a '$__${n}_comp_results'
465
550
  `}function i(e,t){let n=e.replace(/[-:]/g,`_`);return`# powershell completion for ${e} -*- shell-script -*-
466
551
 
467
552
  [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
package/dist/t.js CHANGED
@@ -1 +1 @@
1
- import{a as e,i as t,n,o as r,r as i,s as a,t as o}from"./t-BWhsyajM.js";export{o as Argument,n as Command,i as Option,t as RootCommand,e as ShellCompDirective,a as default,r as script};
1
+ import{a as e,i as t,n,o as r,r as i,s as a,t as o}from"./t-DYl0qC9V.js";export{o as Argument,n as Command,i as Option,t as RootCommand,e as ShellCompDirective,a as default,r as script};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bomb.sh/tab",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "main": "./dist/t.js",
5
5
  "types": "./dist/t.d.ts",
6
6
  "type": "module",