From 3c0499ba56759e40732ded9cf607f83b595c6aab Mon Sep 17 00:00:00 2001 From: Joachim Mathes Date: Sun, 18 Oct 2020 15:34:52 +0200 Subject: [PATCH] Provide alternative dunstctl implementation --- bumblebee_status/modules/contrib/dunstctl.py | 41 ++++++++------- tests/modules/contrib/test_dunstctl.py | 54 ++++++-------------- themes/icons/ascii.json | 11 ++++ themes/icons/awesome-fonts.json | 3 +- themes/icons/ionicons.json | 5 ++ 5 files changed, 55 insertions(+), 59 deletions(-) diff --git a/bumblebee_status/modules/contrib/dunstctl.py b/bumblebee_status/modules/contrib/dunstctl.py index 24b93f0..3c803a4 100644 --- a/bumblebee_status/modules/contrib/dunstctl.py +++ b/bumblebee_status/modules/contrib/dunstctl.py @@ -1,43 +1,42 @@ # pylint: disable=C0111,R0903 -""" -Toggle dunst notifications using dunstctl. +"""Toggle dunst notifications using dunstctl. -When notifications are paused using this module dunst doesn't get killed and you'll keep getting notifications on the background that will be displayed when unpausing. -This is specially useful if you're using dunst's scripting (https://wiki.archlinux.org/index.php/Dunst#Scripting), which requires dunst to be running. Scripts will be executed when dunst gets unpaused. +When notifications are paused using this module dunst doesn't get killed and +you'll keep getting notifications on the background that will be displayed when +unpausing. This is specially useful if you're using dunst's scripting +(https://wiki.archlinux.org/index.php/Dunst#Scripting), which requires dunst to +be running. Scripts will be executed when dunst gets unpaused. Requires: * dunst v1.5.0+ contributed by `cristianmiranda `_ - many thanks! +contributed by `joachimmathes `_ - many thanks! """ import core.module import core.widget import core.input - import util.cli class Module(core.module.Module): def __init__(self, config, theme): super().__init__(config, theme, core.widget.Widget("")) - self._paused = self.__isPaused() - core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.toggle_status) + core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__toggle_state) + self.__states = {"unknown": ["unknown", "critical"], + "true": ["muted", "warning"], + "false": ["unmuted"]} - def toggle_status(self, event): - self._paused = self.__isPaused() - - if self._paused: - util.cli.execute("dunstctl set-paused false") - else: - util.cli.execute("dunstctl set-paused true") - self._paused = not self._paused - - def __isPaused(self): - return util.cli.execute("dunstctl is-paused").strip() == "true" + def __toggle_state(self, event): + util.cli.execute("dunstctl set-paused toggle", ignore_errors=True) def state(self, widget): - if self._paused: - return ["muted", "warning"] - return ["unmuted"] + return self.__states[self.__is_dunst_paused()] + + def __is_dunst_paused(self): + result = util.cli.execute("dunstctl is-paused", + return_exitcode=True, + ignore_errors=True) + return result[1].rstrip() if result[0] == 0 else "unknown" diff --git a/tests/modules/contrib/test_dunstctl.py b/tests/modules/contrib/test_dunstctl.py index e6a7ce3..db77fe3 100644 --- a/tests/modules/contrib/test_dunstctl.py +++ b/tests/modules/contrib/test_dunstctl.py @@ -14,54 +14,34 @@ def test_load_module(): __import__("modules.contrib.dunstctl") def test_dunst_running(mocker): - command = mocker.patch('util.cli.execute', return_value='false') + command = mocker.patch('util.cli.execute', return_value=(0, "false")) module = build_module() module.update() - - command.assert_called_with('dunstctl is-paused') - widget = module.widget() - assert module.state(widget) == ['unmuted'] + + actual = module.state(widget) + command.assert_called_with('dunstctl is-paused', return_exitcode=True, ignore_errors=True) + assert actual == ['unmuted'] def test_dunst_paused(mocker): - command = mocker.patch('util.cli.execute', return_value='true') + command = mocker.patch('util.cli.execute', return_value=(0, "true")) module = build_module() module.update() - - command.assert_called_with('dunstctl is-paused') - widget = module.widget() - assert module.state(widget) == ['muted', 'warning'] -def test_toggle_status_pause(mocker): - command = mocker.patch('util.cli.execute') - command.side_effect = ['true', 'true', None] + actual = module.state(widget) + command.assert_called_with('dunstctl is-paused', return_exitcode=True, ignore_errors=True) + assert actual == ['muted', 'warning'] + +def test_dunst_off(mocker): + command = mocker.patch('util.cli.execute', return_value=(1, "dontcare")) module = build_module() - module.toggle_status(False) - - command.assert_any_call('dunstctl set-paused false') - -def test_toggle_status_unpause(mocker): - command = mocker.patch('util.cli.execute') - command.side_effect = ['false', 'false', None] - - module = build_module() - module.toggle_status(False) - - command.assert_called_with('dunstctl set-paused true') - -def test_input_register(mocker): - command = mocker.patch('util.cli.execute') - input_register = mocker.patch('core.input.register') - - module = build_module() - - input_register.assert_called_with( - module, - button=core.input.LEFT_MOUSE, - cmd=module.toggle_status - ) + module.update() + widget = module.widget() + actual = module.state(widget) + command.assert_called_with('dunstctl is-paused', return_exitcode=True, ignore_errors=True) + assert actual == ['unknown', 'critical'] diff --git a/themes/icons/ascii.json b/themes/icons/ascii.json index 2580d32..77ef931 100644 --- a/themes/icons/ascii.json +++ b/themes/icons/ascii.json @@ -355,6 +355,17 @@ "prefix": "dunst" } }, + "dunstctl": { + "muted": { + "prefix": "dunst(muted)" + }, + "unmuted": { + "prefix": "dunst" + }, + "unknown": { + "prefix": "dunst(unknown)" + } + }, "twmn": { "muted": { "prefix": "twmn" diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index 2ac9061..2d05dc6 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -259,7 +259,8 @@ }, "dunstctl": { "muted": { "prefix": "" }, - "unmuted": { "prefix": "" } + "unmuted": { "prefix": "" }, + "unknown": { "prefix": "" } }, "twmn": { "muted": { "prefix": "" }, diff --git a/themes/icons/ionicons.json b/themes/icons/ionicons.json index b510cc1..c9931f1 100644 --- a/themes/icons/ionicons.json +++ b/themes/icons/ionicons.json @@ -187,6 +187,11 @@ "muted": { "prefix": "\uf39a" }, "unmuted": { "prefix": "\uf39b" } }, + "dunstctl": { + "muted": { "prefix": "\uf39a" }, + "unmuted": { "prefix": "\uf39b" }, + "unknown": { "prefix": "\uf142" } + }, "twmn": { "muted": { "prefix": "\uf1f6" }, "unmuted": { "prefix": "\uf0f3" }