From b5768a23e1992f93de765846a88f06f487c7f2ab Mon Sep 17 00:00:00 2001 From: int0h Date: Mon, 16 Sep 2019 20:08:00 +0200 Subject: [PATCH] added completeCmd option --- lib/index.js | 5 +++++ lib/installer.js | 7 ++++++- lib/scripts/bash.sh | 2 +- lib/scripts/fish.sh | 2 +- lib/scripts/zsh.sh | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index bfc9d9a..d388b3a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,13 +17,18 @@ const debug = tabtabDebug('tabtab'); */ const install = async (options = { name: '', completer: '' }) => { const { name, completer } = options; + let { completeCmd } = options; if (!name) throw new TypeError('options.name is required'); if (!completer) throw new TypeError('options.completer is required'); + if (!completeCmd) { + completeCmd = 'completion' + } return prompt().then(({ location }) => installer.install({ name, completer, + completeCmd, location }) ); diff --git a/lib/installer.js b/lib/installer.js index 1e1e207..8401aba 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -235,7 +235,7 @@ const writeToTabtabScript = async ({ name }) => { * - name: The package configured for completion * - completer: The binary that will act as the completer for `name` program */ -const writeToCompletionScript = ({ name, completer }) => { +const writeToCompletionScript = ({ name, completer, completeCmd }) => { const filename = untildify( path.join(COMPLETION_DIR, `${name}.${shellExtension()}`) ); @@ -249,6 +249,7 @@ const writeToCompletionScript = ({ name, completer }) => { filecontent .replace(/\{pkgname\}/g, name) .replace(/{completer}/g, completer) + .replace(/{completeCmd}/g, completeCmd) // on Bash on windows, we need to make sure to remove any \r .replace(/\r?\n/g, '\n') ) @@ -289,6 +290,10 @@ const install = async (options = { name: '', completer: '', location: '' }) => { throw new Error('options.location is required'); } + if (!options.completeCmd) { + options = {...options, completeCmd: 'completion'}; + } + await Promise.all([ writeToShellConfig(options), writeToTabtabScript(options), diff --git a/lib/scripts/bash.sh b/lib/scripts/bash.sh index 41feeb5..fd3799c 100644 --- a/lib/scripts/bash.sh +++ b/lib/scripts/bash.sh @@ -13,7 +13,7 @@ if type complete &>/dev/null; then IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \ COMP_LINE="$COMP_LINE" \ COMP_POINT="$COMP_POINT" \ - {completer} completion -- "${words[@]}" \ + {completer} {completeCmd} -- "${words[@]}" \ 2>/dev/null)) || return $? IFS="$si" if type __ltrim_colon_completions &>/dev/null; then diff --git a/lib/scripts/fish.sh b/lib/scripts/fish.sh index 1272721..f4668c0 100644 --- a/lib/scripts/fish.sh +++ b/lib/scripts/fish.sh @@ -4,7 +4,7 @@ function _{pkgname}_completion set cursor (commandline -C) set words (node -pe "'$cmd'.split(' ').length") - set completions (eval env DEBUG=\"" \"" COMP_CWORD=\""$words\"" COMP_LINE=\""$cmd \"" COMP_POINT=\""$cursor\"" {completer} completion -- $cmd) + set completions (eval env DEBUG=\"" \"" COMP_CWORD=\""$words\"" COMP_LINE=\""$cmd \"" COMP_POINT=\""$cursor\"" {completer} {completeCmd} -- $cmd) for completion in $completions echo -e $completion diff --git a/lib/scripts/zsh.sh b/lib/scripts/zsh.sh index 6cb519b..b3cf2d2 100644 --- a/lib/scripts/zsh.sh +++ b/lib/scripts/zsh.sh @@ -4,7 +4,7 @@ if type compdef &>/dev/null; then local reply local si=$IFS - IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {completer} completion -- "${words[@]}")) + IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {completer} {completeCmd} -- "${words[@]}")) IFS=$si _describe 'values' reply