Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add fish completions (Fixing #118) #151

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LIBDIR ?= $(PREFIX)/lib
SYSTEM_EXTENSION_DIR ?= $(LIBDIR)/password-store/extensions
MANDIR ?= $(PREFIX)/man
BASHCOMPDIR ?= /etc/bash_completion.d
FISHCOMPDIR ?= /etc/fish/completions

all:
@echo "pass-$(PROG) is a shell script and does not need compilation, it can be simply executed."
Expand All @@ -20,6 +21,8 @@ install:
install -m0755 $(PROG).bash "$(DESTDIR)$(SYSTEM_EXTENSION_DIR)/$(PROG).bash"
install -d "$(DESTDIR)$(BASHCOMPDIR)/"
install -m 644 pass-otp.bash.completion "$(DESTDIR)$(BASHCOMPDIR)/pass-otp"
install -d "$(DESTDIR)$(FISHCOMPDIR)/"
install -m 644 pass.fish "$(DESTDIR)$(FISHCOMPDIR)/pass.fish"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be called pass-otp since it might interfere with pass?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried that first, but it seemed like fish uses the filename to apply its completions to a specific command (in this case pass). If you know of a way to name this file pass-otp.fish I'm all for it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But won't that overwrite the default pass.fish?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's why by default this pass.fish installs into /etc/fish/completions and reads the default pass.fish from /usr/share/fish/vendor_completions.d/pass.fish. That is where the default ones are on my system, but I can't guarantee that on other systems. That's why checkboxes 3 and 4 are still open

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the setup for completions is kind of messy and I haven't looked at this for a while. Lemme check upstream and get back to you.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the ideal solution would be for pass to delegate to extension completion scripts itself, similar to what it does for pass git and pass grep here: https://git.zx2c4.com/password-store/commit/?id=06f499994071bb6131244218b25d637103afe1d5. This should be a reasonable change to submit upstream.

@echo
@echo "pass-$(PROG) is installed succesfully"
@echo
Expand All @@ -28,7 +31,8 @@ uninstall:
rm -vrf \
"$(DESTDIR)$(SYSTEM_EXTENSION_DIR)/$(PROG).bash" \
"$(DESTDIR)$(MANDIR)/man1/pass-$(PROG).1" \
"$(DESTDIR)$(BASHCOMPDIR)/pass-otp"
"$(DESTDIR)$(BASHCOMPDIR)/pass-otp" \
"$(DESTDIR)$(FISHCOMPDIR)/pass.fish"

lint:
shellcheck -s bash $(PROG).bash
Expand Down
56 changes: 56 additions & 0 deletions pass.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env fish

#TODO Do not suggest a second subcommand after a first one is used
#TODO Do not hard-code the path to the main pass completions
#TODO Make sure the Makefile does not overwrite the main pass completions
#TODO Make sure the default FISHCOMPDIR is in $fish_complete_path, and before the main pass completions' location

source "/usr/share/fish/vendor_completions.d/pass.fish"

# Allow for the checking of two commands
function __fish_pass_uses_command
set -l cmd (commandline -opc)
if test (count $argv) -gt 1
if test (count $cmd) -gt 2
if test \( $argv[1] = $cmd[2] \) -a \( $argv[2] = $cmd[3] \)
return 0
end
end
else if test (count $cmd) -gt 1
if test $argv[1] = $cmd[2]
return 0
end
end
return 1
end

set -l PROG 'pass'

# Add `otp` after `pass`. Defaults to `pass otp code`
complete -c $PROG -f -n '__fish_pass_needs_command' -a otp -d 'Command: generate OTP code'
complete -c $PROG -f -n '__fish_pass_uses_command otp; and not __fish_pass_uses_command otp insert; and not __fish_pass_uses_command otp append; and not __fish_pass_uses_command otp validate' -s c -l clip -d 'Put OTP code in clipboard'
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a "(__fish_pass_print_entries_and_dirs)"

# Add `code` after `pass otp`
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a code -d 'Command: generate an OTP code'
complete -c $PROG -f -n '__fish_pass_uses_command otp code' -s c -l clip -d 'Put OTP code in clipboard'

# Add `insert` after `pass otp`
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a insert -d 'Command: insert a new OTP key URI in a new password file'
complete -c $PROG -f -n '__fish_pass_uses_command otp insert' -s e -l echo -d 'Echo the input'
complete -c $PROG -f -n '__fish_pass_uses_command otp insert' -s f -l force -d 'Do not prompt before overwriting an existing URI'

# Add `append` after `pass otp`
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a append -d 'Command: append an OTP key URI to an existing password file'
complete -c $PROG -f -n '__fish_pass_uses_command otp append' -s e -l echo -d 'Echo the input'
complete -c $PROG -f -n '__fish_pass_uses_command otp append' -s f -l force -d 'Do not prompt before overwriting an existing URI'
complete -c $PROG -f -n '__fish_pass_uses_command otp append' -a "(__fish_pass_print_entries_and_dirs)"

# Add `uri` after `pass otp`
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a uri -d 'Command: display the stored key URI'
complete -c $PROG -f -n '__fish_pass_uses_command otp uri' -s c -l clip -d 'Put key URI in clipboard'
complete -c $PROG -f -n '__fish_pass_uses_command otp uri' -s q -l qrcode -d 'Display a QR code'
complete -c $PROG -f -n '__fish_pass_uses_command otp uri' -a "(__fish_pass_print_entries_and_dirs)"

# Add `validate` after `pass otp`
complete -c $PROG -f -n '__fish_pass_uses_command otp' -a validate -d 'Command: test if the given URI is a valid OTP key URI'