Skip to content

Instantly share code, notes, and snippets.

@rjl6789
Last active July 8, 2019 10:49
Show Gist options
  • Save rjl6789/f4d3b57fc4b690912a9fca562327ce56 to your computer and use it in GitHub Desktop.
Save rjl6789/f4d3b57fc4b690912a9fca562327ce56 to your computer and use it in GitHub Desktop.
dunst - basic indicator bar based on arch wiki and associated links

From Arch wiki

Source

Tips and tricks

Using dunstify as volume/brightness level indicator You can use the replace id feature to implement a simple volume or brightness indicator notification like in this picture [2].

To realize that volume indicator place the following script somewhere on your PATH.

 #!/bin/bash
 # changeVolume
 
 # Arbitrary but unique message id
 msgId="991049"
  
 # Change the volume using alsa(might differ if you use pulseaudio)
 amixer -c 0 set Master "$@" > /dev/null
 
 # Query amixer for the current volume and whether or not the speaker is muted
 volume="$(amixer -c 0 get Master | tail -1 | awk '{print $4}' | sed 's/[^0-9]*//g')"
 mute="$(amixer -c 0 get Master | tail -1 | awk '{print $6}' | sed 's/[^a-z]*//g')"
 if [[ $volume == 0 || "$mute" == "off" ]]; then
     # Show the sound muted notification
     dunstify -a "changeVolume" -u low -i audio-volume-muted -r "$msgId" "Volume muted" 
 else
     # Show the volume notification
     dunstify -a "changeVolume" -u low -i audio-volume-high -r "$msgId" \
     "Volume: ${volume}%" "$(getProgressString 10 "<b> </b>" " " $volume)"
 fi
 
 # Play the volume changed sound
 canberra-gtk-play -i audio-volume-change -d "changeVolume"

getProgressString needs to be some function assembling the progressbar like string. This script uses [3].

Now simply bind changeVolume 2dB+ unmute etc. to some hotkey and you are done. You might also want to make dunst ignore these type of notifications in its history. See #Modifying.

#!/bin/bash
# getProgressString <TOTAL ITEMS> <FILLED LOOK> <NOT FILLED LOOK> <STATUS>
# For instance:
# $ getProgressString 10 "#" "-" 50
# #####-----
# Note: if you want to use | in your progress bar string you need to change the delimiter in the sed commands
ITEMS="$1" # The total number of items(the width of the bar)
FILLED_ITEM="$2" # The look of a filled item
NOT_FILLED_ITEM="$3" # The look of a not filled item
STATUS="$4" # The current progress status in percent
# calculate how many items need to be filled and not filled
FILLED_ITEMS=$(echo "((${ITEMS} * ${STATUS})/100 + 0.5) / 1" | bc)
NOT_FILLED_ITEMS=$(echo "$ITEMS - $FILLED_ITEMS" | bc)
# Assemble the bar string
msg=$(printf "%${FILLED_ITEMS}s" | sed "s| |${FILLED_ITEM}|g")
msg=${msg}$(printf "%${NOT_FILLED_ITEMS}s" | sed "s| |${NOT_FILLED_ITEM}|g")
echo "$msg"

File: dunstrc - add this "rule"

[signed_on]
   appname = changeVolume
   history_ignore=yes

From Arch wiki

Source

Rules You can create rules in your dunstrc file which match certain notifications and then perform an action on it such as executing a script.

Filtering To create a new rule create a new section with a custom name in your config file. In that section you can now use the attributes appname, summary, body, icon, category, match_transient and msg_urgency to match the notification. Globbing is supported. See Scripting for an example. Start dunst with the -print option to find out useful information about a notification to write proper rules.

Modifying When a notification is matched you can perform certain actions on it like modifying the format string, which is especially useful if you want tocompletely ignore certain notifications. In that case simply add the line format="" to your rule.

Another useful feature is if you want to keep certain notifications out of your history for example if you use dunst as a Volume indicator. To achieve this simply add history_ignore=yes to your rule.

Scripting Dunst can be configured to run scripts based on certain notification content. Here is an example using Dunst to run a script when someone from pidgin signs on:

[signed_on]
   appname = Pidgin
   summary = "*signed on*"
   urgency = low
   script = do_something.sh

The specified script will be passed the following parameters in that order: appname, summary, body, icon, urgency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment