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

Feature/autocomplete script #227

Merged
merged 2 commits into from
Aug 25, 2023
Merged
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
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ BIN=target/$(TARGET)/$(APP)
BIN_DST=$(DESTDIR)$(prefix)/bin/$(APP)
DOC_DST=$(DESTDIR)$(prefix)/share/man/man1/bandwhich.1
LIC_DST=$(DESTDIR)$(prefix)/share/licenses/$(APP)
AUTOCOMPLETE_DST=/usr/share/bash-completion/completions
SRC = Makefile Cargo.lock Cargo.toml $(shell find src -type f -wholename 'src/*.rs')

.PHONY: all clean distclean install uninstall vendor
.PHONY: all clean distclean install uninstall vendor install_autocomplete

all: $(BIN)

Expand All @@ -35,12 +36,19 @@ ifeq ($(VENDOR),1)
endif
cargo build $(ARGS)

install:
install_autocomplete:
mkdir -p $(AUTOCOMPLETE_DST)
cp src/bandwhich $(AUTOCOMPLETE_DST)/bandwhich

uninstall_autocomplete:
rm -f $(AUTOCOMPLETE_DST)/bandwhich

install: install_autocomplete
install -Dm755 $(BIN) $(BIN_DST)
install -Dm644 docs/bandwhich.1 $(DOC_DST)
install -Dm644 LICENSE.md $(LIC_DST)/LICENSE

uninstall:
uninstall: uninstall_autocomplete
rm -rf $(BIN_DST) $(DOC_DST) $(LIC_DST)

vendor:
Expand Down
31 changes: 31 additions & 0 deletions src/bandwhich
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# bandwhich completion -*- shell-script -*-

_bandwhich()
{
local cur prev flags opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
flags="--addresses --connections --help --no-resolve
--processes --raw --show-dns --total-utilization --version"
opts="--dns-server --interface"

interfaces=$(ip link show | grep -o ": .*:" | sed 's/[: ]//g' | tr '\n' ' ')
case "${prev}" in
--interface)
COMPREPLY=( $(compgen -W "${interfaces}" -- "${cur}"))
return
;;
esac

case "${cur}" in
--interface)
COMPREPLY=( $(compgen -W "${interfaces}"))
;;
-*)
COMPREPLY=( $(compgen -W "${flags} ${opts}" -- "${cur}"))
;;
esac
} &&

complete -o nospace -F _bandwhich bandwhich