Skip to content

Commit

Permalink
🐟 mommy added missing completions~
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Feb 26, 2024
1 parent e64e387 commit a68740f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## [unreleased]
### added
* 🦓 mommy now supports templates with literal slashes using `%%S%%`~ ([#107](https://github.com/FWDekker/mommy/issues/107))
* 🐟 mommy added completions for fish and zsh for the new options introduced in v1.4.0~ ([#105](https://github.com/FWDekker/mommy/issues/105))

### changed
* 🙅‍♀️ mommy's `MOMMY_FORBIDDEN_WORDS` setting now interprets each word as a regex~ ([#103](https://github.com/FWDekker/mommy/issues/103))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ for reference, here's the three main ways to invoke mommy~
| format | example | when to use |
|-----------------------|-----------------------------|----------------------------------------------------------------------|
| `mommy [command] ...` | `mommy npm test` | if you want mommy to respond to a single command~ |
| `mommy -e [eval]` | `mommy -e "ls -l \| wc -l"` | if you want mommy when using `\|` or `>`, or need mommy in a script~ |
| `mommy -e [command]` | `mommy -e "ls -l \| wc -l"` | if you want mommy when using `\|` or `>`, or need mommy in a script~ |
| `mommy -s [status]` | `mommy -s $?` | if you already ran a command and want mommy's help afterwards~ |

### 🛸 extra options<a name="extra-options"></a>
Expand Down
106 changes: 59 additions & 47 deletions src/main/completions/fish/mommy.fish
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
## Helper functions
# Extracts the non-option commands from `$argv` and writes to stdout.
# For example, given `mommy -c ./config.sh apt update -f`, writes `apt update -f`.
function extract_command
set -e argv[1]
set --erase argv[1]

set -l is_option_argument 0
set --local is_option_argument 0
for arg in $argv
if test $is_option_argument -eq 1
set -e argv[1]
set --erase argv[1]
set is_option_argument 0
continue
end

switch $arg
case '-c' '-e' '-s'
set -e argv[1]
case '-c' '-e' '-s' # Do not include long options here!
set --erase argv[1]
set is_option_argument 1
case '-*'
set -e argv[1]
set --erase argv[1]
case '*'
echo $argv
return 0
Expand All @@ -26,60 +27,71 @@ function extract_command
return 0
end

# Extract the args, excluding the arg that the user is currently writing
# Extract the args, excluding the arg that the user is currently writing.
function get_args
set -l tokens (commandline -opc)
set --local tokens (commandline -opc)

extract_command $tokens
end

# Extract the args, including the arg that the user is currently writing
# Extract the args, including the arg that the user is currently writing.
function get_args_with_token
set -l tokens (commandline -opc) (commandline -ct)
set --local tokens (commandline -opc) (commandline -ct)

extract_command $tokens
end


# Set common elements
set -l opt_help "-o h -l help"
set -l opt_version "-o v -l version"
## Completions
set --local opt_help "--short-option h --long-option help"
set --local opt_version "--short-option v --long-option version"
set --local opt_eval "--short-option e --long-option eval"
set --local opt_status "--short-option s --long-option status"

complete --command mommy --no-files # Disabled by default, but re-enabled for specific cases below

# Add completions
complete -c mommy -f
# Help/version
complete --command mommy --short-option h --long-option help \
--description "Show manual" \
--condition "__fish_is_first_arg"
complete --command mommy --short-option v --long-option version \
--description "Show version" \
--condition "__fish_is_first_arg"

complete -c mommy -o h -l help \
-d "Show manual" \
-n "__fish_is_first_arg"
complete -c mommy -o v -l version \
-d "Show version" \
-n "__fish_is_first_arg"
# Config
complete --command mommy --short-option c --long-option config \
--require-parameter --force-files \
--description "Configuration file" \
--condition "not __fish_seen_argument $opt_help $opt_version"\
--condition "test -z (get_args)"
complete --command mommy --long-option global-config-dirs \
--require-parameter \
--arguments "(__fish_complete_directories)" \
--description "Colon-separated global config file dirs" \
--condition "not __fish_seen_argument $opt_help $opt_version"\
--condition "test -z (get_args)"

complete -c mommy -o 1 \
-d "Write to stdout" \
-n "not __fish_seen_argument $opt_help $opt_version" \
-n "test -z (get_args)"
complete -c mommy -o c \
-rF \
-d "Configuration file" \
-n "not __fish_seen_argument $opt_help $opt_version"\
-n "test -z (get_args)"
# Misc
complete --command mommy --short-option 1 \
--description "Write to stdout" \
--condition "not __fish_seen_argument $opt_help $opt_version" \
--condition "test -z (get_args)"

complete -c mommy -o e \
-r \
-d "Evaluate string" \
-n "not __fish_seen_argument $opt_help $opt_version -o s" \
-n "test -z (get_args)"
complete -c mommy -o s \
-rf \
-d "Exit code" \
-a "(echo 0\tSuccess\n1\tError)" \
-n "not __fish_seen_argument $opt_help $opt_version -o e" \
-n "test -z (get_args)"

complete -c mommy \
# `complete -C` requires one argument, so must be wrapped in quotes. Fish <3.4.0 cannot do `$(...)`, so workaround
# is to assign to temporary variable.
-k -a "(set -l command (get_args_with_token); complete -C \"\$command\")" \
-n "test -n (get_args_with_token); or not __fish_seen_argument $opt_help $opt_version -o e -o s"
# Usage
complete --command mommy \
# `complete --do-complete` requires one argument, so must be wrapped in quotes.
# Fish <3.4.0 cannot do `$(...)`, so workaround is to assign to temporary variable.
--keep-order \
--arguments "(set --local command (get_args_with_token); complete --do-complete \"\$command\")" \
--condition "test -n (get_args_with_token); or not __fish_seen_argument $opt_help $opt_version $opt_eval $opt_status"
complete --command mommy --short-option e --long-option eval \
--require-parameter \
--description "Evaluate string" \
--condition "not __fish_seen_argument $opt_help $opt_version $opt_status" \
--condition "test -z (get_args)"
complete --command mommy --short-option s --long-option status \
--require-parameter --no-files \
--description "Exit code" \
--arguments "(echo 0\tSuccess\n1\tError)" \
--condition "not __fish_seen_argument $opt_help $opt_version $opt_eval" \
--condition "test -z (get_args)"
7 changes: 4 additions & 3 deletions src/main/completions/zsh/_mommy
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ _arguments \
"(- *)"{-h,--help}'[Show manual]' \
"(- *)"{-v,--version}'[Show version]' \
-1'[Write to stdout]' \
-c'[Configuration file]:config:_files' \
-e'[Evaluate string]:string' \
-s'[Exit code]:code:->status' \
{-c,--config}'[Configuration file]:config:_files' \
--global-config-dirs'[Colon-separated global config file dirs]:global config:_dir_list' \
{-e,--eval}'[Evaluate string]:string' \
{-s,--status}'[Exit code]:code:->status' \
'*::command:'

# suggest exit codes for --status
Expand Down
21 changes: 14 additions & 7 deletions src/main/man/man1/mommy.1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mommy - here to support you~
\fBmommy\fP [\fIoptions\fP] \fIcommand\fP ...
e.g. \fBmommy\fP npm test
.TP
\fBmommy\fP [\fIoptions\fP] \fB-e\fP \fIeval\fP
\fBmommy\fP [\fIoptions\fP] \fB-e\fP \fIcommand\fP
e.g. \fBmommy\fP -e "ls -l | wc -l"
.TP
\fBmommy\fP [\fIoptions\fP] \fB-s\fP \fIstatus\fP
Expand All @@ -27,6 +27,19 @@ to change mommy's output, you should change the config file.
for more info about config files, check
\fIhttps://github.com/FWDekker/mommy/tree/v%%VERSION_NUMBER%%#configuration\fP.

.TP
\fB-h\fP, \fB--help\fP
opens \fBmommy\fP's manual page~
.TP
\fB-v\fP, \fB--version\fP
displays \fBmommy\fP's version information~
.TP
\fB-e\fP \fIcommand\fP, \fB--eval=\fP\fIcommand\fP
runs \fIcommand\fP and responds to the output.
note that \fIcommand\fP should be given as a single argument~
.TP
\fB-s\fP \fIstatus\fP, \fB--status=\fP\fIstatus\fP
gives output corresponding to exit code \fIstatus\fP~
.TP
\fB-c\fP \fIfile\fP, \fB--config=\fP\fIfile\fP
tells mommy that she should read your config from \fIfile\fP~
Expand All @@ -36,12 +49,6 @@ sets global configuration dirs to the colon-separated list in \fIdirs\fP~
.TP
\fB-1\fP
writes output to \fBstdout\fP instead of \fBstderr\fP~
.TP
\fB-h\fP, \fB--help\fP
opens \fBmommy\fP's manual page~
.TP
\fB-v\fP, \fB--version\fP
displays \fBmommy\fP's version information~


.SH bugs
Expand Down

0 comments on commit a68740f

Please sign in to comment.