Skip to content

Commit

Permalink
Add -t option to toggle modes
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen authored and emersion committed Mar 12, 2024
1 parent 8960eb9 commit c0c5373
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 4 additions & 1 deletion doc/makoctl.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Sends IPC commands to the running mako daemon via dbus.

*mode* ++
*mode* -s <mode>... ++
*mode* [-a mode]... [-r mode]...
*mode* [-a mode]... [-r mode]... [-t mode]... +++
When run without any option, retrieves a list of current modes.

When run with the _-s_ option, replaces the current modes with the provided
Expand All @@ -84,6 +84,9 @@ Sends IPC commands to the running mako daemon via dbus.
When run with the _-a_ or _-r_ options, adds or removes the provided mode
from the current modes.

When run with the _-t_ option, toggle the mode by removing it when present
and otherwise adding it.

See the _MODES_ section in **mako**(5) for more information about modes.

*help, -h, --help*
Expand Down
17 changes: 11 additions & 6 deletions makoctl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ usage() {
echo " reload Reload the configuration file"
echo " mode List modes"
echo " mode [-a mode]... [-r mode]... Add/remove modes"
echo " mode [-t mode]... Toggle modes (add if not present, remove if present)"
echo " mode -s mode... Set modes"
echo " help Show this help"
}
Expand Down Expand Up @@ -147,34 +148,38 @@ case "$1" in
shift 1
require_jq
modes="$(call ListModes | jq '.data[0]')"
add_remove_flag=0
add_remove_toggle_flag=0
set_flag=0
while getopts a:r:s name; do
while getopts a:r:t:s name; do
case "$name" in
a)
add_remove_flag=1
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" '. += [$mode]')"
;;
r)
add_remove_flag=1
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" 'del(.[] | select(. == $mode))')"
;;
t)
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" '. |= if any(.[]; . == $mode) then map(select(. != $mode)) else . + [$mode] end')"
;;
s)
set_flag=1
;;
?)
exit 1
esac
done
if [ "$add_remove_flag" = 1 ] && [ "$set_flag" = 1 ]; then
if [ "$add_remove_toggle_flag" = 1 ] && [ "$set_flag" = 1 ]; then
echo >&2 "makoctl: -a/-r and -s cannot be used together"
exit 1
fi
if [ "$set_flag" = 1 ]; then
shift $(($OPTIND - 1))
modes="$(jq -n '$ARGS.positional' --args "$@")"
fi
if [ "$add_remove_flag" = 1 ] || [ "$set_flag" = 1 ]; then
if [ "$add_remove_toggle_flag" = 1 ] || [ "$set_flag" = 1 ]; then
modes="$(echo "$modes" | jq '. | unique')"
modes_len="$(echo "$modes" | jq '. | length')"
modes_args=$(echo "$modes" | jq -r '@sh')
Expand Down

0 comments on commit c0c5373

Please sign in to comment.