Skip to content

Instantly share code, notes, and snippets.

@Wielding
Created April 20, 2024 14:14
Show Gist options
  • Save Wielding/fff13df6814aed624f62b0f825bad94a to your computer and use it in GitHub Desktop.
Save Wielding/fff13df6814aed624f62b0f825bad94a to your computer and use it in GitHub Desktop.
A fuzzel frontend to access mako notifications with a keyboard under sway
#!/usr/bin/env bash
#####################################################################################
# A keyboard-centric mako notification control script
#
# Requirements:
# jq - https://jqlang.github.io/jq/
# fuzzel - https://codeberg.org/dnkl/fuzzel
#
# Use:
# * Change fuzzel monitor output to your preference (everywhere you see HDMI-A-3)
# * Sway - bind a key to this script e.g.
# bindsym $mod+Alt+n exec --no-startup-id "~/.config/mako/mako_center.sh"
# * Thunderbird - there is Thunderbird specific logic here to get around
# it's desire to close notifications too quickly and to use different text for the
# notification title. In your mako config add the
# following to prevent Tunderbird notifications from timing out.
#
# [app-name=Thunderbird]
# default-timeout=0
#
#####################################################################################
declare -A entries
declare -A reverse_entries
declare -A apps
declare -A actions
notifications=$(makoctl list | jq -r '(.data)[][] | "\(.id.data)|\(.summary.data)|\(."app-name".data)|\(.body.data)|\(.actions.data)" | gsub("[\\n\\t]"; "")')
# if no notifications, exit
if [ -z "$notifications" ]; then
exit
fi
# populate the notification data
while IFS=$'\n' read -r line; do
items=()
IFS='|' read -ra item <<<"$line"
for i in "${item[@]}"; do
items+=("$i")
done
display="${items[1]}"
# if Thunderbird use body as display
if [[ ${items[2]} == "Thunderbird" ]]; then
display="${items[3]}"
fi
echo "items: ${items[@]}\n"
entries["${items[0]}"]="$display"
reverse_entries["$display"]="${items[0]}"
apps["${items[0]}"]="${items[2]}"
actions["${items[0]}"]="${items[4]}"
done <<<"$notifications"
if [ ${#entries[@]} -eq 0 ]; then
exit
fi
fuzzel_input=""
for key in "${!entries[@]}"; do
fuzzel_input+="${entries[$key]}\n"
done
# select the notification to act on
fuzzel_output=$(echo -e -n "$fuzzel_input" | fuzzel --output HDMI-A-3 --no-fuzzy -p "select notification:" -d)
if [ -z "$fuzzel_output" ]; then
exit
fi
notification_id=${reverse_entries[$fuzzel_output]}
# if no actions, dismiss
if [[ ${actions[$notification_id]} == "{}" ]]; then
echo "no actions"
makoctl dismiss -n $notification_id
exit
fi
# select the notification action to perform
makoctl menu -n $notification_id fuzzel -o HDMI-A-3 -d -p "$fuzzel_output: "
app="${apps[$notification_id]}"
# if it is Thunderbird notification, dismiss since my mako is configured to make it persistent
# to avoid Thunderbird from closing it quickly using the following in the mako config file
#
#[app-name=Thunderbird]
#default-timeout=0
if [[ $app == "Thunderbird" ]]; then
makoctl dismiss -n $notification_id
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment