From 14cb73ee681a999980764740da92bc487bfedf86 Mon Sep 17 00:00:00 2001 From: Yankee Maharjan Date: Sat, 22 Jan 2022 13:25:26 +0545 Subject: [PATCH] Add bash autocompletion --- README.md | 17 ++++++++++++++++- wt_completion | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 wt_completion diff --git a/README.md b/README.md index b3562ce..7316ba7 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,21 @@ Copy the executable to any directory in your `$PATH` $ sudo cp wt /usr/local/bin ``` +### Tab Completion +:warning: Only **bash** completion is available for now. + +```bash +sudo cp wt_completion.bash /etc/bash_completion.d +``` + +```bash +wt + +# OR + +wt +``` + ## Usage Switch between worktrees. You can do a text search to change to the worktree directory. @@ -48,4 +63,4 @@ Update to the latest release ```bash $ wt update -``` \ No newline at end of file +``` diff --git a/wt_completion b/wt_completion new file mode 100644 index 0000000..0e36a9a --- /dev/null +++ b/wt_completion @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +_wt() { + local cur opts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + list="$(wt list | awk '{ print $1; }' | tr "\n" " ")" + opts="" + + for item in $list + do + opts+="$(basename "$item") " + done + + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + local cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(compgen -W "${opts}" -- "$cur") ) + fi +} + +complete -F _wt wt