From c0c53734ae36f6fcce99340302b5c0ef6e5a13a3 Mon Sep 17 00:00:00 2001 From: Paul Meyer <49727155+katexochen@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:41:13 +0100 Subject: [PATCH] Add -t option to toggle modes Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com> --- doc/makoctl.1.scd | 5 ++++- makoctl | 17 +++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/doc/makoctl.1.scd b/doc/makoctl.1.scd index 52c5ebd..eb10366 100644 --- a/doc/makoctl.1.scd +++ b/doc/makoctl.1.scd @@ -75,7 +75,7 @@ Sends IPC commands to the running mako daemon via dbus. *mode* ++ *mode* -s ... ++ -*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 @@ -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* diff --git a/makoctl b/makoctl index b5298fe..efb891a 100755 --- a/makoctl +++ b/makoctl @@ -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" } @@ -147,18 +148,22 @@ 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 ;; @@ -166,7 +171,7 @@ case "$1" in 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 @@ -174,7 +179,7 @@ case "$1" in 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')