[READ-ONLY] Mirror of https://github.com/bombshell-dev/tab. shell autocompletions for javascript CLIs
4

Configure Feed

Select the types of activity you want to include in your feed.

resolve comments

Mohammad Bagher Abiyat (Oct 9, 2024, 8:14 PM +0330) ae37e119 a4ba1b42

+38 -27
+34 -14
cac.ts
··· 70 70 let isCompletingFlagValue = false; 71 71 let flagName = ""; 72 72 let option: (typeof options)[number] | null = null; 73 - const lastArg = previousArgs[previousArgs.length - 1] 73 + const lastArg = previousArgs[previousArgs.length - 1]; 74 74 75 - console.log({ lastArg, toComplete, endsWithSpace, flagName }); 76 - // TODO: fix for "--port 3" 75 + function processOption() { 76 + const matchedOption = options.find((o) => 77 + o.names.some((name) => name === flagName), 78 + ); 79 + 80 + if (matchedOption && !matchedOption.isBoolean) { 81 + isCompletingFlagValue = true; 82 + option = matchedOption; 83 + if (endsWithSpace) { 84 + toComplete = ""; 85 + } 86 + } else { 87 + isCompletingFlagValue = false; 88 + option = null; 89 + } 90 + } 91 + 77 92 if (toComplete.startsWith("--")) { 78 93 // Long option 79 94 flagName = toComplete.slice(2); ··· 82 97 // Option with '=', get the name before '=' 83 98 flagName = flagName.slice(0, equalsIndex); 84 99 toComplete = toComplete.slice(toComplete.indexOf("=") + 1); 100 + processOption(); 85 101 } else if (!endsWithSpace) { 86 102 // If not ending with space, still typing option name 87 103 flagName = ""; 88 104 } else { 89 - toComplete = "" 105 + // User pressed space after typing the option name 106 + processOption(); 107 + toComplete = ""; 90 108 } 91 109 } else if (toComplete.startsWith("-") && toComplete.length > 1) { 92 110 // Short option ··· 94 112 if (!endsWithSpace) { 95 113 // Still typing option name 96 114 flagName = ""; 97 - } 98 - } 99 - 100 - if (flagName) { 101 - option = options.find((o) => o.names.includes(flagName)) ?? null; 102 - if (option && !option.isBoolean) { 103 - isCompletingFlagValue = true; 115 + } else { 116 + processOption(); 117 + toComplete = ""; 104 118 } 119 + } else if (lastArg?.startsWith("--") && !endsWithSpace) { 120 + flagName = lastArg.slice(2); 121 + processOption(); 122 + } else if (lastArg?.startsWith("-") && lastArg.length > 1 && !endsWithSpace) { 123 + flagName = lastArg.slice(2); 124 + processOption(); 105 125 } 106 126 107 127 if (isCompletingFlagValue) { ··· 113 133 // Call custom completion function for the flag 114 134 const comps = await flagCompletionFn(previousArgs, toComplete); 115 135 completions.push( 116 - ...comps.map((comp) => `${comp.action}\t${comp.description}`), 136 + ...comps.map((comp) => `${comp.action}\t${comp.description ?? ''}`), 117 137 ); 118 138 directive = ShellCompDirective.ShellCompDirectiveNoFileComp; 119 139 } else { ··· 143 163 144 164 if (optionsToSuggest.length > 0) { 145 165 completions.push( 146 - ...optionsToSuggest.map((o) => `--${o.name}\t${o.description}`), 166 + ...optionsToSuggest.map((o) => `--${o.name}\t${o.description ?? ''}`), 147 167 ); 148 168 } 149 169 ··· 194 214 part.value.startsWith(fullCommandPart) 195 215 ) { 196 216 // User is typing this command part, provide completion 197 - completions.push(`${part.value}\t${c.description}`); 217 + completions.push(`${part.value}\t${c.description ?? ""}`); 198 218 } 199 219 // Command part does not match, break 200 220 break;
+4 -13
demo.ts
··· 167 167 if (o.rawName.includes("--port <port>")) { 168 168 // Completion for --port 169 169 flagMap.set(optionKey, async (previousArgs, toComplete) => { 170 - console.log({previousArgs, toComplete}) 171 170 return [ 172 171 { action: "3000", description: "Development server port" }, 173 172 { action: "8080", description: "Alternative port" }, ··· 210 209 value: arg.value, 211 210 completion: async (previousArgs, toComplete) => { 212 211 if (arg.value === "root") { 213 - // Completion for the [root] positional argument in the dev command 214 - const currentDir = process.cwd(); 215 - const dirs = await fs.readdir(currentDir); 216 - const dirCompletions: Completion[] = []; 217 - for (const dir of dirs) { 218 - const dirPath = path.join(currentDir, dir); 219 - const stat = await fs.lstat(dirPath); 220 - if (stat.isDirectory() && dir.startsWith(toComplete)) { 221 - dirCompletions.push({ action: dir, description: "" }); 222 - } 223 - } 224 - return dirCompletions; 212 + return [ 213 + { action: "src/", description: "💣️.sh loves vite!" }, 214 + { action: "./", description: "This one is better." }, 215 + ]; 225 216 } 226 217 return []; 227 218 },