diff --git a/i3/.config/i3blocks/config b/i3/.config/i3blocks/config index e0eb712..96c3cab 100644 --- a/i3/.config/i3blocks/config +++ b/i3/.config/i3blocks/config @@ -18,6 +18,12 @@ command=./activity interval=10 LABEL= +[dunst] +command=./dunst +markup=pango +interval=once +format=json + [volume] command=./volume LABEL= diff --git a/i3/.config/i3blocks/dunst b/i3/.config/i3blocks/dunst new file mode 100755 index 0000000..4a5c4e0 --- /dev/null +++ b/i3/.config/i3blocks/dunst @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +""" +A do-not-disturb button for muting Dunst notifications in i3 using i3blocks +Mute is handled by passing 'DUNST_COMMAND_PAUSE' and 'DUNST_COMMAND_RESUME' to +the notify-send script and the 'DUNST_MUTE' environment variable is set to keep +track of the toggle. +""" + +__author__ = "Jessey White-Cinis " +__copyright__ = "Copyright (c) 2019 Jessey White-Cinis" +__license__ = "MIT" +__version__ = "1.1.0" + +import os +import subprocess +import json + +def mute_on(): + '''Turns off dunst notifications''' + subprocess.run(["dunstctl", "set-paused", "true"], check=True) + return { + "full_text":"\uf1f6", + "DUNST_MUTE":"on" + } + +def mute_off(): + '''Turns back on dunst notifications''' + subprocess.run(["dunstctl", "set-paused", "false"], check=True) + return { + "full_text":"\uf0f3", + "DUNST_MUTE":"off" + } + +def clicked(): + '''Returns True if the button was clicked''' + button = "BLOCK_BUTTON" in os.environ and os.environ["BLOCK_BUTTON"] + return bool(button) + +def muted(): + '''Returns True if Dunst is muted''' + cmd = ['dunstctl', 'is-paused'] + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) + output = proc.communicate()[0] + return u'true' == output.strip().decode("UTF-8") + +if clicked(): + # toggle button click to turn mute on and off + if muted(): + RTN = mute_off() + else: + RTN = mute_on() + +else: + # Set default state using 'DUNST_MUTE' environment variable + if muted(): + RTN = mute_on() + else: + RTN = mute_off() + +print(json.dumps(RTN))