Skip to content

Commit

Permalink
Fix WindowTabs use of parse_text (#4712)
Browse files Browse the repository at this point in the history
This fixes the issue raised in #3741 by using the parser function for each window title before any state or markup is applied, similarly to what is done for the TaskList widget.
  • Loading branch information
heitzmann committed Mar 3, 2024
1 parent 8594323 commit 9c684ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
26 changes: 10 additions & 16 deletions libqtile/widget/windowtabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ class WindowTabs(base._TextBox):
("selected", ("<b>", "</b>"), "Selected task indicator"),
(
"parse_text",
None,
"Function to parse and modify window names. "
"e.g. function in config that removes excess "
"strings from window name: "
"def my_func(text)"
' for string in [" - Chromium", " - Firefox"]:'
' text = text.replace(string, "")'
" return text"
"then set option parse_text=my_func",
lambda window_name: window_name,
"Function to modify window names. It must accept "
"a string argument (original window name) and return "
"a string with the modified name.",
),
]

Expand All @@ -68,22 +63,21 @@ def _configure(self, qtile, bar):
def update(self, *args):
names = []
for w in self.bar.screen.group.windows:
try:
name = self.parse_text(w.name if w and w.name else "")
except: # noqa: E722
logger.exception("parse_text function failed:")
name = w.name if w and w.name else "(unnamed)"
state = ""
if w.maximized:
state = "[] "
elif w.minimized:
state = "_ "
elif w.floating:
state = "V "
task = "%s%s" % (state, w.name if w and w.name else " ")
task = pangocffi.markup_escape_text(task)
task = pangocffi.markup_escape_text(state + name)
if w is self.bar.screen.group.current_window:
task = task.join(self.selected)
names.append(task)
self.text = self.separator.join(names)
if callable(self.parse_text):
try:
self.text = self.parse_text(self.text)
except: # noqa: E722
logger.exception("parse_text function failed:")
self.bar.draw()
12 changes: 12 additions & 0 deletions test/widgets/test_windowtabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
from libqtile.confreader import Config


def custom_text_parser(name):
return f"TEST-{name}-TEST"


class WindowTabsConfig(Config):
auto_fullscreen = True
groups = [libqtile.config.Group("a"), libqtile.config.Group("b")]
Expand All @@ -40,6 +44,7 @@ class WindowTabsConfig(Config):
top=bar.Bar(
[
widget.WindowTabs(),
widget.WindowTabs(name="customparse", parse_text=custom_text_parser),
],
24,
),
Expand Down Expand Up @@ -136,3 +141,10 @@ def test_escaping_text(manager):
"""
manager.test_window("Text & Text")
assert manager.c.widget["windowtabs"].info()["text"] == "<b>Text &amp; Text</b>"


@windowtabs_config
def test_custom_text_parser(manager):
"""Test the custom text parser function."""
manager.test_window("one")
assert manager.c.widget["customparse"].info()["text"] == "<b>TEST-one-TEST</b>"

0 comments on commit 9c684ec

Please sign in to comment.