-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on command line completions
- Loading branch information
Andrey Popp
committed
Jun 8, 2024
1 parent
90f399a
commit a6c9982
Showing
16 changed files
with
331 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
_NAME() { | ||
local prefix="${COMP_WORDS[COMP_CWORD]}" | ||
COMP_WORDS[COMP_CWORD]="+cmdliner_complete:${COMP_WORDS[COMP_CWORD]}" | ||
local line="env COMP_RUN=1 ${COMP_WORDS[@]}" | ||
while read type; do | ||
if [[ $type == 'dir' ]] && (type compopt &> /dev/null); then | ||
if [[ $prefix != -* ]]; then | ||
COMPREPLY+=( $(compgen -d "$prefix") ) | ||
fi | ||
elif [[ $type == 'file' ]] && (type compopt &> /dev/null); then | ||
if [[ $prefix != -* ]]; then | ||
COMPREPLY+=( $(compgen -f "$prefix") ) | ||
fi | ||
elif [[ $type == 'item' ]]; then | ||
read value; | ||
read _doc; | ||
COMPREPLY+=($value) | ||
fi | ||
done < <(eval $line) | ||
return 0 | ||
} | ||
_NAME_setup() { | ||
complete -F _NAME NAME | ||
} | ||
_NAME_setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# This is a template zsh script which defines a completion function for a | ||
# cmdliner based program. | ||
# | ||
# The NAME is the program name, should be replaced when eval'ing this script, | ||
# for example: | ||
# | ||
# eval "$(cat set-zsh-completion.sh | sed 's/NAME/myprog/g')" | ||
# | ||
# The script: | ||
# | ||
# 1. Invokes the program with no arguments but COMP_CWORD, COMP_NWORD and | ||
# COMP_WORD# envrironment variables set, where # is the number from 1 to | ||
# COMP_NWORD, the COMP_CWORD designates the position where completion is | ||
# requested. | ||
# | ||
# 2. It expects the output on stdout from the program where each three lines | ||
# are: | ||
# | ||
# item | ||
# COMPLETION | ||
# DESCRIPTION | ||
# | ||
# or a single line (to activate filename completion): | ||
# | ||
# file | ||
# | ||
# or a single line (to activate directory completion): | ||
# | ||
# dir | ||
|
||
function _NAME { | ||
words[CURRENT]="+cmdliner_complete:${words[CURRENT]}" | ||
local line="env COMP_RUN=1 ${(@)words}" | ||
local -a completions | ||
response=("${(@f)$(eval $line)}") | ||
for t key desc in ${response}; do | ||
if [[ "$t" == "item" ]]; then | ||
completions+=("$key":"$desc") | ||
elif [[ "$t" == "dir" ]]; then | ||
_path_files -/ | ||
elif [[ "$t" == "file" ]]; then | ||
_path_files -f | ||
fi | ||
done | ||
if [ -n "$completions" ]; then | ||
_describe -V unsorted completions -U | ||
fi | ||
} | ||
|
||
compdef _NAME NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,4 @@ val t4 : | |
('a * 'b * 'c * 'd) conv | ||
|
||
val env_bool_parse : bool parser | ||
val is_space : char -> bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
let zsh_completion name = Printf.sprintf {|# This is a template zsh script which defines a completion function for a | ||
# cmdliner based program. | ||
# | ||
# The %s is the program name, should be replaced when eval'ing this script, | ||
# for example: | ||
# | ||
# eval "$(cat set-zsh-completion.sh | sed 's/%s/myprog/g')" | ||
# | ||
# The script: | ||
# | ||
# 1. Invokes the program with no arguments but COMP_CWORD, COMP_NWORD and | ||
# COMP_WORD# envrironment variables set, where # is the number from 1 to | ||
# COMP_NWORD, the COMP_CWORD designates the position where completion is | ||
# requested. | ||
# | ||
# 2. It expects the output on stdout from the program where each three lines | ||
# are: | ||
# | ||
# item | ||
# COMPLETION | ||
# DESCRIPTION | ||
# | ||
# or a single line (to activate filename completion): | ||
# | ||
# file | ||
# | ||
# or a single line (to activate directory completion): | ||
# | ||
# dir | ||
|
||
function _%s { | ||
words[CURRENT]="+cmdliner_complete:${words[CURRENT]}" | ||
local line="env COMP_RUN=1 ${(@)words}" | ||
local -a completions | ||
response=("${(@f)$(eval $line)}") | ||
for t key desc in ${response}; do | ||
if [[ "$t" == "item" ]]; then | ||
completions+=("$key":"$desc") | ||
elif [[ "$t" == "dir" ]]; then | ||
_path_files -/ | ||
elif [[ "$t" == "file" ]]; then | ||
_path_files -f | ||
fi | ||
done | ||
if [ -n "$completions" ]; then | ||
_describe -V unsorted completions -U | ||
fi | ||
} | ||
|
||
compdef _%s %s | ||
|} name name name name name;;let bash_completion name = Printf.sprintf {|_%s() { | ||
local prefix="${COMP_WORDS[COMP_CWORD]}" | ||
COMP_WORDS[COMP_CWORD]="+cmdliner_complete:${COMP_WORDS[COMP_CWORD]}" | ||
local line="env COMP_RUN=1 ${COMP_WORDS[@]}" | ||
while read type; do | ||
if [[ $type == 'dir' ]] && (type compopt &> /dev/null); then | ||
if [[ $prefix != -* ]]; then | ||
COMPREPLY+=( $(compgen -d "$prefix") ) | ||
fi | ||
elif [[ $type == 'file' ]] && (type compopt &> /dev/null); then | ||
if [[ $prefix != -* ]]; then | ||
COMPREPLY+=( $(compgen -f "$prefix") ) | ||
fi | ||
elif [[ $type == 'item' ]]; then | ||
read value; | ||
read _doc; | ||
COMPREPLY+=($value) | ||
fi | ||
done < <(eval $line) | ||
return 0 | ||
} | ||
_%s_setup() { | ||
complete -F _%s %s | ||
} | ||
_%s_setup; | ||
|} name name name name name;; |
Oops, something went wrong.