diff --git a/CHANGELOG.md b/CHANGELOG.md index 404d9e3b..6d485418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,11 @@ and this project (partially) adheres to [Semantic Versioning](https://semver.org ## [Unreleased] ### Added - Official EndeavourOS distribution support +- Display server protocol to `DesktopEnvironment` ### Changed - Allow `df` output to contain Unicode characters +- `DesktopEnvironment` API value format: now an object with `name` and `display_server_protocol` attributes ## [v4.14.2.0] - 2023-08-26 ### Added diff --git a/archey/entries/desktop_environment.py b/archey/entries/desktop_environment.py index d4787a50..6444a3dc 100644 --- a/archey/entries/desktop_environment.py +++ b/archey/entries/desktop_environment.py @@ -2,6 +2,7 @@ import os import platform +import typing from archey.entry import Entry from archey.processes import Processes @@ -19,6 +20,11 @@ "xfce4-session": "Xfce", } +DSP_DICT = { + "x11": "X11", + "wayland": "Wayland", +} + class DesktopEnvironment(Entry): """ @@ -31,17 +37,34 @@ class DesktopEnvironment(Entry): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.value = { + "name": self._fetch_desktop_env_name(), + "display_server_protocol": DSP_DICT.get(os.getenv("XDG_SESSION_TYPE", "")), + + } + + def _fetch_desktop_env_name(self) -> typing.Optional[str]: # macOS' desktop environment is called "Aqua", # and could not be detected from processes list. if platform.system() == "Darwin": - self.value = "Aqua" - return + return "Aqua" processes = Processes().list for de_id, de_name in DE_DICT.items(): if de_id in processes: - self.value = de_name - break - else: - # Let's rely on an environment variable if the loop above didn't `break`. - self.value = os.getenv("XDG_CURRENT_DESKTOP") + return de_name + + # Let's rely on an environment variable if the loop above didn't `break`. + return os.getenv("XDG_CURRENT_DESKTOP") + + def output(self, output) -> None: + # No DE could be detected. + if self.value["name"] is None: + output.append(self.name, self._default_strings.get("not_detected")) + return + + text_output = self.value["name"] + if self.value["display_server_protocol"] is not None: + text_output += f" ({self.value['display_server_protocol']})" + + output.append(self.name, text_output) diff --git a/archey/test/entries/test_archey_desktop_environment.py b/archey/test/entries/test_archey_desktop_environment.py index e143f75c..af925ae1 100644 --- a/archey/test/entries/test_archey_desktop_environment.py +++ b/archey/test/entries/test_archey_desktop_environment.py @@ -24,7 +24,7 @@ class TestDesktopEnvironmentEntry(unittest.TestCase): ) def test_match(self, _): """Simple list matching""" - self.assertEqual(DesktopEnvironment().value, "Cinnamon") + self.assertEqual(DesktopEnvironment().value["name"], "Cinnamon") @patch( "archey.entries.desktop_environment.Processes.list", @@ -36,17 +36,22 @@ def test_match(self, _): "coffee", ), ) - @patch("archey.entries.desktop_environment.os.getenv", return_value="DESKTOP ENVIRONMENT") + @patch( + "archey.entries.desktop_environment.os.getenv", + side_effect=["DESKTOP ENVIRONMENT", "wayland"], + ) def test_mismatch(self, _, __): """Simple list (mis-)-matching""" - self.assertEqual(DesktopEnvironment().value, "DESKTOP ENVIRONMENT") + desktop_environment = DesktopEnvironment() + self.assertEqual(desktop_environment.value["name"], "DESKTOP ENVIRONMENT") + self.assertEqual(desktop_environment.value["display_server_protocol"], "Wayland") @patch("archey.entries.desktop_environment.platform.system") def test_darwin_aqua_deduction(self, _, platform_system_mock): """Test "Aqua" deduction on Darwin systems""" platform_system_mock.return_value = "Darwin" # Override module-wide mocked value. - self.assertEqual(DesktopEnvironment().value, "Aqua") + self.assertEqual(DesktopEnvironment().value["name"], "Aqua") @patch( "archey.entries.desktop_environment.Processes.list", @@ -64,7 +69,7 @@ def test_darwin_aqua_deduction(self, _, platform_system_mock): ) def test_non_detection(self, _, __): """Simple global non-detection""" - self.assertIsNone(DesktopEnvironment().value) + self.assertIsNone(DesktopEnvironment().value["name"]) if __name__ == "__main__": diff --git a/archey/test/entries/test_archey_window_manager.py b/archey/test/entries/test_archey_window_manager.py index 89384454..abb112d0 100644 --- a/archey/test/entries/test_archey_window_manager.py +++ b/archey/test/entries/test_archey_window_manager.py @@ -46,6 +46,7 @@ def test_wmctrl(self, _, __): "here", ), ) + def test_no_wmctrl_match(self, _, __): """Test basic detection based on a (fake) processes list""" self.assertEqual(WindowManager().value, "Awesome")