Skip to content

Commit

Permalink
add a completion function for zsh (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
okapia authored Apr 5, 2021
1 parent 1913ebe commit 04dd36f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ you can do it with the `change-password` command.

### Changing the location of the credentials file

Simply put this into your `.zshrc` (or `.{YourSell}rc` or `.profile`):
Simply put this into your `.zshrc` (or `.{YourShell}rc` or `.profile`):

```
export TOTP_CLI_CREDENTIAL_FILE="/mnt/mydrive/totp-credentials"
Expand All @@ -113,3 +113,19 @@ Note: It's a filename not just a directory.

Note: It does not traverse through the given path,
parent directory has to be there already.

### Zsh Completion

A function to provide tab-completion for zsh is in the file `_totp-cli`.
When installing or packaging totp-cli this should preferably be
installed in `$prefix/share/zsh/site-functions`. Otherwise, it can be
installed by copying to a directory where zsh searches for completion
functions (the `$fpath` array). If you, for example, put all completion
functions into the folder `~/.zsh/completions` you must add the
following to your zsh main config file (`.zshrc`):

```
fpath=( ~/.zsh/completions $fpath )
autoload -U compinit
compinit
```
33 changes: 33 additions & 0 deletions _totp-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#compdef totp-cli

# Completion function for zsh
# Install this file in $prefix/share/zsh/site-functions or
# update the fpath array in zsh to include its location

local -a commands=(
'change-password:change password'
'delete:delete an account or a whole namespace'
'generate:generate a specific OTP'
'instant:generate an OTP from TOTP_TOKEN without the Storage backend'
'list:list all available namespaces or accounts under a namespace'
'update:check and update totp-cli itself'
'version:display version information'
'add-token:add new token'
'help:display general or command-specific usage information'
)

case $CURRENT:${words[2]} in
2:*|3:help)
_describe -t commands command commands
;;
3:(add-token|delete|generate|list))
_message -e namespaces namespace
;;
4:(add-token|delete|generate|list))
_message -e accounts account
;;
*)
_message 'no more arguments'
;;
esac

0 comments on commit 04dd36f

Please sign in to comment.