Skip to content

Commit

Permalink
Implemented bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
terminationshock committed Apr 10, 2024
1 parent 79d771a commit f30280b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

lisst_completion() {
COMPREPLY=($(lisst --completion "$COMP_LINE" "$2"))
if [ "${COMPREPLY[0]}" == "-" ]; then
COMPREPLY=($(compgen -A command "$2"))
fi
}

complete -F "lisst_completion" lisst
40 changes: 40 additions & 0 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func NewConfig() *Config {
stat, err := os.Stat(p)
return err == nil && stat.IsDir()
}
case "--completion":
if len(os.Args) > 3 {
printCompletion(os.Args[2], os.Args[3])
}
os.Exit(0)
default:
if strings.HasPrefix(arg, "--") {
fmt.Fprintln(os.Stderr, "Invalid command-line option " + arg)
Expand Down Expand Up @@ -124,3 +129,38 @@ func NewConfig() *Config {
return config
}

func printCompletion(line string, current string) {
if strings.HasPrefix(current, "--") {
printCompletionOption(line, current, []string{"--help", "--filter", "--show-output", "--ignore-error"})
printExclusiveCompletionOption(line, current, []string{"--sort", "--sort-rev"})
printExclusiveCompletionOption(line, current, []string{"--line", "--git-commit-hash", "--filename", "--filename-lineno", "--dirname"})
} else {
hasPattern := false
for _, pattern := range []string{"--line", "--git-commit-hash", "--filename", "--filename-lineno", "--dirname"} {
if strings.Contains(line, pattern + " ") {
hasPattern = true
break
}
}
if hasPattern {
fmt.Println("-")
}
}
}

func printCompletionOption(line string, current string, options []string) {
for _, option := range options {
if strings.HasPrefix(option, current) && !strings.Contains(line, option + " ") {
fmt.Println(option)
}
}
}

func printExclusiveCompletionOption(line string, current string, options []string) {
for _, option := range options {
if strings.Contains(line, option + " ") {
return
}
}
printCompletionOption(line, current, options)
}

0 comments on commit f30280b

Please sign in to comment.