Skip to content

Commit

Permalink
Implement human-readable variant of RuleList
Browse files Browse the repository at this point in the history
The old JSON formatted variant is now reachable with the `--json` flag.
  • Loading branch information
zappolowski committed Jan 21, 2024
1 parent 8a34ef3 commit 5468661
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 5 additions & 4 deletions contrib/dunstctl.fishcomp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
if command -q jq
function __fish_dunstctl_info
dunstctl $argv[1] | jq -r ".data[][] | \"\(.$argv[2].data)\t\(.$argv[3].data)\""
dunstctl (string split ' ' $argv[1]) | jq -r ".data[][] | \"\(.$argv[2].data)\t\(.$argv[3].data)\""
end
else
function __fish_dunstctl_info
dunstctl $argv[1] | awk -vpattern="\"$argv[2]\" :" '$0 ~ pattern {getline; getline; gsub("\"", "", $3); print $3}'
dunstctl (string split ' ' $argv[1]) | awk -vpattern="\"$argv[2]\" :" '$0 ~ pattern {getline; getline; gsub("\"", "", $3); print $3}'
end
end

function __fish_dunstctl_rule_complete
set -l parts (string split ' ' $argv[1])
if test (count $parts[-1]) -eq 0 || test $parts[-2] = rule
__fish_dunstctl_info rules name enabled
__fish_dunstctl_info 'rules --json' name enabled
return
end
# TODO? enable disable might not make sense when the rule is already in the correct state
Expand All @@ -30,7 +30,7 @@ complete -c dunstctl -f -n __fish_use_subcommand -a history-pop -d 'Pop the late
complete -c dunstctl -f -n __fish_use_subcommand -a history-rm -d 'Remove the notification from history with given ID'
complete -c dunstctl -f -n __fish_use_subcommand -a is-paused -d 'Check if dunst is running or paused'
complete -c dunstctl -f -n __fish_use_subcommand -a set-paused -d 'Set the pause status'
complete -c dunstctl -f -n __fish_use_subcommand -a rules -d 'Displays configured rules (in JSON)'
complete -c dunstctl -f -n __fish_use_subcommand -a rules -d 'Displays configured rules (optionally in JSON)'
complete -c dunstctl -f -n __fish_use_subcommand -a rule -d 'Enable or disable a rule by its name'
complete -c dunstctl -f -n __fish_use_subcommand -a debug -d 'Print debugging information'
complete -c dunstctl -f -n __fish_use_subcommand -a help -d 'Show this help'
Expand All @@ -41,5 +41,6 @@ complete -c dunstctl -x -n '__fish_seen_subcommand_from count' -a 'displayed his
complete -c dunstctl -x -n '__fish_seen_subcommand_from history-pop history-rm' -a '(__fish_dunstctl_info history id appname)'
complete -c dunstctl -x -n '__fish_seen_subcommand_from set-paused' -a 'true false toggle'
complete -c dunstctl -x -n '__fish_seen_subcommand_from rule' -a '(__fish_dunstctl_rule_complete (commandline -c))'
complete -c dunstctl -x -n '__fish_seen_subcommand_from rules' -a --json

# ex: filetype=fish
22 changes: 19 additions & 3 deletions dunstctl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ show_help() {
get-pause-level Get the current pause level
set-pause-level level Set the pause level
rule name enable|disable|toggle Enable or disable a rule by its name
rules Displays configured rules (in JSON)
rules [--json] Displays configured rules (optionally
in JSON)
debug Print debugging information
help Show this help
EOH
Expand Down Expand Up @@ -125,8 +126,23 @@ case "${1:-}" in
fi
;;
"rules")
busctl --user --json=pretty --no-pager call org.freedesktop.Notifications /org/freedesktop/Notifications org.dunstproject.cmd0 RuleList 2>/dev/null \
|| die "Dunst is not running."
case "${2:-}" in
"" | --json)
busctl --user --json=pretty --no-pager call "${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_DUNST}" RuleList 2>/dev/null \
| {
if [ "${2:-}" = '--json' ]
then
cat
else
jq --raw-output '.data[][] | to_entries[] | if (.key == "name") then "[\(.value.data)]" else " \(.key) = \(.value.data)" end'
fi
} \
|| die "Dunst is not running."
;;
*)
die "Unknown format \"${2}\". Please use either \"--json\" or no option at all."
;;
esac
;;
"rule")
[ "${2:-}" ] \
Expand Down

0 comments on commit 5468661

Please sign in to comment.