|
| 1 | +// Copyright 2024 Bloomberg Finance L.P. |
| 2 | +// Distributed under the terms of the Apache 2.0 license. |
| 3 | +import type { StricliAutoCompleteContext } from "../context"; |
| 4 | +import type { ShellManager } from "../impl"; |
| 5 | +import nodeOs from "node:os"; |
| 6 | +import nodeFs from "node:fs"; |
| 7 | +import nodePath from "node:path"; |
| 8 | + |
| 9 | +function getFilePath(targetCommand: string, completionsDir: string): string { |
| 10 | + return nodePath.join(completionsDir, `${targetCommand}.fish`); |
| 11 | +} |
| 12 | + |
| 13 | +const BLOCK_START_LINE = (targetCommand: string) => |
| 14 | + `# @stricli/auto-complete START [${targetCommand}]`; |
| 15 | +const BLOCK_END_LINE = "# @stricli/auto-complete END"; |
| 16 | + |
| 17 | +export async function forFish( |
| 18 | + context: StricliAutoCompleteContext |
| 19 | +): Promise<ShellManager | undefined> { |
| 20 | + const { os = nodeOs, fs = nodeFs, path = nodePath } = context; |
| 21 | + if (!context.process.env["SHELL"]?.includes("fish")) { |
| 22 | + context.process.stderr.write(`Skipping fish as shell was not detected.\n`); |
| 23 | + return; |
| 24 | + } |
| 25 | + const home = os.homedir(); |
| 26 | + const completionsDir = path.join(home, ".config", "fish", "completions"); |
| 27 | + try { |
| 28 | + await fs.promises.mkdir(completionsDir, { recursive: true }); |
| 29 | + } catch { |
| 30 | + context.process.stderr.write( |
| 31 | + `Could not create fish completions directory at ${completionsDir}.\n` |
| 32 | + ); |
| 33 | + return; |
| 34 | + } |
| 35 | + return { |
| 36 | + install: async (targetCommand: string, autocompleteCommand: string) => { |
| 37 | + const filePath = getFilePath(targetCommand, completionsDir); |
| 38 | + const functionName = `__fish_${targetCommand}_complete`; |
| 39 | + // Create a fish completions file that defines a helper function and registers it. |
| 40 | + const fileContent = [ |
| 41 | + BLOCK_START_LINE(targetCommand), |
| 42 | + `function ${functionName}`, |
| 43 | + ` # Get the current command line (as a single string)`, |
| 44 | + ` set -l cmd (commandline -cp)`, |
| 45 | + ` # Invoke the provided autocomplete command with the command line`, |
| 46 | + ` set -l completions ( ${autocompleteCommand} $cmd )`, |
| 47 | + ` for comp in $completions`, |
| 48 | + ` echo $comp`, |
| 49 | + ` end`, |
| 50 | + `end`, |
| 51 | + `complete -c ${targetCommand} -f -a "(${functionName})"`, |
| 52 | + BLOCK_END_LINE, |
| 53 | + "", |
| 54 | + ].join("\n"); |
| 55 | + try { |
| 56 | + await fs.promises.writeFile(filePath, fileContent); |
| 57 | + context.process.stdout.write( |
| 58 | + `Fish completions installed to ${filePath}. Restart fish shell or run 'source ${filePath}' to load changes.\n` |
| 59 | + ); |
| 60 | + } catch { |
| 61 | + context.process.stderr.write( |
| 62 | + `Failed to write fish completions file at ${filePath}.\n` |
| 63 | + ); |
| 64 | + } |
| 65 | + }, |
| 66 | + uninstall: async (targetCommand: string) => { |
| 67 | + const filePath = getFilePath(targetCommand, completionsDir); |
| 68 | + try { |
| 69 | + await fs.promises.unlink(filePath); |
| 70 | + context.process.stdout.write( |
| 71 | + `Fish completions removed from ${filePath}. Restart fish shell or run 'source ${filePath}' to update changes.\n` |
| 72 | + ); |
| 73 | + } catch { |
| 74 | + context.process.stderr.write( |
| 75 | + `Could not remove fish completions file at ${filePath}.\n` |
| 76 | + ); |
| 77 | + } |
| 78 | + }, |
| 79 | + }; |
| 80 | +} |
0 commit comments