(theme: ThemeName)
| 75 | * shell's rc file. Returns a user-facing status message. |
| 76 | */ |
| 77 | export async function setupShellCompletion(theme: ThemeName): Promise<string> { |
| 78 | const shell = detectShell() |
| 79 | if (!shell) { |
| 80 | return '' |
| 81 | } |
| 82 | |
| 83 | // Ensure the cache directory exists |
| 84 | try { |
| 85 | await mkdir(dirname(shell.cacheFile), { recursive: true }) |
| 86 | } catch (e: unknown) { |
| 87 | logError(e) |
| 88 | return `${EOL}${color('warning', theme)(`Could not write ${shell.name} completion cache`)}${EOL}${chalk.dim(`Run manually: claude completion ${shell.shellFlag} > ${shell.cacheFile}`)}${EOL}` |
| 89 | } |
| 90 | |
| 91 | // Generate the completion script by writing directly to the cache file. |
| 92 | // Using --output avoids piping through stdout where process.exit() can |
| 93 | // truncate output before the pipe buffer drains. |
| 94 | const claudeBin = process.argv[1] || 'claude' |
| 95 | const result = await execFileNoThrow(claudeBin, [ |
| 96 | 'completion', |
| 97 | shell.shellFlag, |
| 98 | '--output', |
| 99 | shell.cacheFile, |
| 100 | ]) |
| 101 | if (result.code !== 0) { |
| 102 | return `${EOL}${color('warning', theme)(`Could not generate ${shell.name} shell completions`)}${EOL}${chalk.dim(`Run manually: claude completion ${shell.shellFlag} > ${shell.cacheFile}`)}${EOL}` |
| 103 | } |
| 104 | |
| 105 | // Check if rc file already sources completions |
| 106 | let existing = '' |
| 107 | try { |
| 108 | existing = await readFile(shell.rcFile, { encoding: 'utf-8' }) |
| 109 | if ( |
| 110 | existing.includes('claude completion') || |
| 111 | existing.includes(shell.cacheFile) |
| 112 | ) { |
| 113 | return `${EOL}${color('success', theme)(`Shell completions updated for ${shell.name}`)}${EOL}${chalk.dim(`See ${formatPathLink(shell.rcFile)}`)}${EOL}` |
| 114 | } |
| 115 | } catch (e: unknown) { |
| 116 | if (!isENOENT(e)) { |
| 117 | logError(e) |
| 118 | return `${EOL}${color('warning', theme)(`Could not install ${shell.name} shell completions`)}${EOL}${chalk.dim(`Add this to ${formatPathLink(shell.rcFile)}:`)}${EOL}${chalk.dim(shell.completionLine)}${EOL}` |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Append source line to rc file |
| 123 | try { |
| 124 | const configDir = dirname(shell.rcFile) |
| 125 | await mkdir(configDir, { recursive: true }) |
| 126 | |
| 127 | const separator = existing && !existing.endsWith('\n') ? '\n' : '' |
| 128 | const content = `${existing}${separator}\n# Claude Code shell completions\n${shell.completionLine}\n` |
| 129 | await writeFile(shell.rcFile, content, { encoding: 'utf-8' }) |
| 130 | |
| 131 | return `${EOL}${color('success', theme)(`Installed ${shell.name} shell completions`)}${EOL}${chalk.dim(`Added to ${formatPathLink(shell.rcFile)}`)}${EOL}${chalk.dim(`Run: source ${shell.rcFile}`)}${EOL}` |
| 132 | } catch (error) { |
| 133 | logError(error) |
| 134 | return `${EOL}${color('warning', theme)(`Could not install ${shell.name} shell completions`)}${EOL}${chalk.dim(`Add this to ${formatPathLink(shell.rcFile)}:`)}${EOL}${chalk.dim(shell.completionLine)}${EOL}` |
no test coverage detected