Skip to content

Commit 84ebc4b

Browse files
committed
Add bash completion
1 parent a34b132 commit 84ebc4b

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,30 @@ SRC=main.c args.c byte_array.c image.c util.c file.c generate_ascii.c generate_b
77
OBJ=$(addprefix obj/, $(addsuffix .o, $(SRC)))
88

99
BIN=bin-graph
10+
COMPLETION=bin-graph-completion.bash
1011

1112
PREFIX=/usr/local
1213
BINDIR=$(PREFIX)/bin
14+
COMPLETIONDIR=$(PREFIX)/share/bash-completion/completions
1315

1416
#-------------------------------------------------------------------------------
1517

16-
.PHONY: all clean install
18+
.PHONY: all clean install install-bin install-completion
1719

1820
all: $(BIN)
1921

2022
clean:
2123
rm -f $(OBJ)
2224
rm -f $(BIN)
2325

24-
install: $(BIN)
26+
install: install-bin install-completion
27+
28+
install-bin: $(BIN)
2529
install -D -m 755 $^ -t $(DESTDIR)$(BINDIR)
2630

31+
install-completion: $(COMPLETION)
32+
install -D -m 644 $^ $(DESTDIR)$(COMPLETIONDIR)/$(BIN)
33+
2734
#-------------------------------------------------------------------------------
2835

2936
$(BIN): $(OBJ)

bin-graph-completion.bash

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
_bin_graph_completion() {
4+
local arg_opts nonarg_opts
5+
arg_opts=(
6+
-m --mode
7+
-w --width
8+
-z --zoom
9+
--block-size
10+
--offset-start --offset-end
11+
--output-format
12+
--transform-squares
13+
)
14+
nonarg_opts=(
15+
-h --help
16+
--list-modes
17+
--list-output-formats
18+
)
19+
20+
# If the previous option ('$3') is a redirector, show the default file
21+
# completion.
22+
if [[ "$3" == '2>' || "$3" == '>' || "$3" == '<' ]]; then
23+
compopt -o bashdefault -o default
24+
return
25+
fi
26+
27+
# If the previous option expected an extra parameter, don't show completion.
28+
for arg in "${arg_opts[@]}"; do
29+
if [ "$3" = "$arg" ]; then
30+
return
31+
fi
32+
done
33+
34+
if [[ "$2" = -* ]]; then
35+
# If the current option ('$2') starts with a dash, return (in
36+
# '$COMPREPLY') the possible completions for the current option using
37+
# 'compgen'.
38+
mapfile -t COMPREPLY < <(compgen -W "${arg_opts[*]} ${nonarg_opts[*]}" -- "$2")
39+
else
40+
# Otherwise, show the default file completion.
41+
compopt -o bashdefault -o default
42+
fi
43+
}
44+
45+
complete -F _bin_graph_completion bin-graph

0 commit comments

Comments
 (0)