Skip to content

Commit

Permalink
fix shell completion generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattaa committed Nov 18, 2024
1 parent f92207a commit de181e3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OBJS := $(SRCS:$(PROJECT_DIR)/%.c=$(OBJ_DIR)/%.o)

TARGET := $(BIN_DIR)/lsr

.PHONY: all debug release clean format test install uninstall
.PHONY: all debug release clean format test install uninstall completions

all: debug

Expand All @@ -43,11 +43,22 @@ $(BIN_DIR) $(OBJ_DIR):

clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
rm -rf completions

format:
clang-format -i $(SRCS) $(PROJECT_DIR)/include/**.h

install: release
completions:
@mkdir -p completions
@mkdir -p completions/bash
@mkdir -p completions/zsh
@mkdir -p completions/fish

./$(TARGET) --completions bash > completions/bash/lsr
./$(TARGET) --completions zsh > completions/zsh/_lsr
./$(TARGET) --completions fish > completions/fish/lsr.fish

install: release completions
install -m 755 $(TARGET) /usr/local/bin
make clean

Expand Down
11 changes: 11 additions & 0 deletions completions/bash/lsr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_lsr() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts="-a --all -F --Files -D --Directories -S --Symlinks -G --Git -l --long -r --recursive --completions "
if [[ ${cur} == -* ]]; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
fi
return 0
}
complete -F _lsr lsr
9 changes: 9 additions & 0 deletions completions/fish/lsr.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Fish completions for lsr
complete -c lsr -s a -l all -d 'Show all entries, including hidden'
complete -c lsr -s F -l Files -d 'Show files only'
complete -c lsr -s D -l Directories -d 'Show directories only'
complete -c lsr -s S -l Symlinks -d 'Show symlinks only'
complete -c lsr -s G -l Git -d 'Show entries that are not defined in .gitignore'
complete -c lsr -s l -l long -d 'Use long format'
complete -c lsr -s r -l recursive -d 'Show in tree format'
complete -c lsr -l completions -d 'Generate shell completions'
13 changes: 13 additions & 0 deletions completions/zsh/_lsr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
compdef _lsr lsr
_lsr() {
_arguments -s \
'(-a --all)'{-a,--all}'[Show all entries, including hidden]' \
'(-F --Files)'{-F,--Files}'[Show files only]' \
'(-D --Directories)'{-D,--Directories}'[Show directories only]' \
'(-S --Symlinks)'{-S,--Symlinks}'[Show symlinks only]' \
'(-G --Git)'{-G,--Git}'[Show entries that are not defined in .gitignore]' \
'(-l --long)'{-l,--long}'[Use long format]' \
'(-r --recursive)'{-r,--recursive}'[Show in tree format]' \
'--completions[Generate shell completions]' \
'*:file:_files'
}
48 changes: 31 additions & 17 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,49 +101,63 @@ void laser_generate_completions(const char *shell, struct option long_args[])

if (strcmp(shell, "bash") == 0)
{
printf("# Bash completions for laser\n");
printf("_laser() {\n");
printf("_lsr() {\n");
printf(" local cur prev opts\n");
printf(" COMPREPLY=()\n");
printf(" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n");

printf(" opts=\"");
for (int i = 0; long_args[i].name != NULL; i++)
{
char short_flag = long_args[i].val ? long_args[i].val : '\0';
if (short_flag)
{
printf("-%c ", short_flag);
}
printf("--%s ", long_args[i].name);
}
printf("\"\n");

printf(" COMPREPLY=( $(compgen -W \"${opts}\" -- ${cur}) )\n");
printf(" if [[ ${cur} == -* ]]; then\n");
printf(" COMPREPLY=( $(compgen -W \"${opts}\" -- ${cur}) )\n");
printf(" fi\n");

printf(" return 0\n");
printf("}\n");
printf("complete -F _laser laser\n");
printf("complete -F _lsr lsr\n");
}
else if (strcmp(shell, "zsh") == 0)
{
printf("# Zsh completions for laser\n");
printf("compdef _laser laser\n");
printf("_laser() {\n");
printf(" local -a opts\n");
printf(" opts=(\n");
printf("compdef _lsr lsr\n");
printf("_lsr() {\n");
printf(" _arguments -s \\\n");
for (int i = 0; long_args[i].name != NULL; i++)
{
printf(" '--%s[%s]'\n", long_args[i].name, descriptions[i]);
char short_flag = long_args[i].val ? long_args[i].val : '\0';
if (short_flag)
{
printf(" '(-%c --%s)'{-%c,--%s}'[%s]' \\\n", short_flag,
long_args[i].name, short_flag, long_args[i].name,
descriptions[i]);
}
else
{
printf(" '--%s[%s]' \\\n", long_args[i].name,
descriptions[i]);
}
}
printf(" )\n");
printf(" _describe 'laser options' opts\n");
printf(" '*:file:_files'\n");
printf("}\n");
}
else if (strcmp(shell, "fish") == 0)
{
printf("# Fish completions for laser\n");
printf("# Fish completions for lsr\n");
for (int i = 0; long_args[i].name != NULL; i++)
{
char short_flag = long_args[i].val ? long_args[i].val : '\0';
printf("complete -c laser");
if (short_flag)
printf("complete -c lsr");
if (long_args[i].val)
{
printf(" -s %c", short_flag);
printf(" -s %c", long_args[i].val);
}
printf(" -l %s -d '%s'\n", long_args[i].name, descriptions[i]);
}
Expand Down

0 comments on commit de181e3

Please sign in to comment.