Skip to content

Commit 76c85c6

Browse files
0xErwin1ErikBjare
andauthored
feat: add exclude_titles config & CLI option for excluding window titles by regex (#99)
* Add `exclude_titles` feature for enhanced window title exclusion This commit introduces the `--exclude-titles` argument to the aw-watcher-window module, allowing users to specify a list of window titles or regular expression patterns to exclude from tracking. This new feature is designed to complement the existing `--exclude-title` flag, providing enhanced flexibility for users who need to exclude multiple window titles without breaking compatibility with existing configurations. Key Changes: - Added the `--exclude-titles` argument to the argparse configuration in `config.py`, enabling the specification of multiple exclusion patterns. - Updated the `heartbeat_loop` function in `main.py` to support both `exclude_title` and `exclude_titles`, with `exclude_titles` allowing for an array of titles to be excluded. - Utilized the `re` module for regex pattern matching against window titles, ensuring case-insensitive comparisons. This enhancement ensures that users can now more precisely control which window titles are excluded from tracking, making the aw-watcher-window module more versatile and user-friendly. * Added exclude-titles in default config * Update aw_watcher_window/main.py Co-authored-by: Erik Bjäreholt <[email protected]> * Changed the helper message and changed how regex is compiled * Changed patterns to pass by parameter to heartbeat_loop * Change compile regex * Apply suggestions from code review --------- Co-authored-by: Erik Bjäreholt <[email protected]>
1 parent 12c7bea commit 76c85c6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

aw_watcher_window/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
default_config = """
66
[aw-watcher-window]
77
exclude_title = false
8+
exclude_titles = []
89
poll_time = 1.0
910
strategy_macos = "swift"
1011
""".strip()
@@ -19,6 +20,7 @@ def parse_args():
1920

2021
default_poll_time = config["poll_time"]
2122
default_exclude_title = config["exclude_title"]
23+
default_exclude_titles = config["exclude_titles"]
2224
default_strategy_macos = config["strategy_macos"]
2325

2426
parser = argparse.ArgumentParser(
@@ -33,6 +35,13 @@ def parse_args():
3335
action="store_true",
3436
default=default_exclude_title,
3537
)
38+
parser.add_argument(
39+
"--exclude-titles",
40+
dest="exclude_titles",
41+
nargs='+',
42+
default=default_exclude_titles,
43+
help="Exclude window titles by regular expression. Can specify multiple times."
44+
)
3645
parser.add_argument("--verbose", dest="verbose", action="store_true")
3746
parser.add_argument(
3847
"--poll-time", dest="poll_time", type=float, default=default_poll_time

aw_watcher_window/main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
import re
34
import signal
45
import subprocess
56
import sys
@@ -30,6 +31,13 @@ def kill_process(pid):
3031
except ProcessLookupError:
3132
logger.info("Process {} already dead".format(pid))
3233

34+
def try_compile_title_regex(title):
35+
try:
36+
return re.compile(title, re.IGNORECASE)
37+
except re.error:
38+
logger.error(f"Invalid regex pattern: {title}")
39+
exit(1)
40+
3341

3442
def main():
3543
args = parse_args()
@@ -92,10 +100,11 @@ def main():
92100
poll_time=args.poll_time,
93101
strategy=args.strategy,
94102
exclude_title=args.exclude_title,
103+
exclude_titles=[try_compile_title_regex(title) for title in args.exclude_titles if title is not None]
95104
)
96105

97106

98-
def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False):
107+
def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False, exclude_titles=[]):
99108
while True:
100109
if os.getppid() == 1:
101110
logger.info("window-watcher stopped because parent process died")
@@ -128,6 +137,10 @@ def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False):
128137
if current_window is None:
129138
logger.debug("Unable to fetch window, trying again on next poll")
130139
else:
140+
for pattern in exclude_titles:
141+
if pattern.search(current_window["title"]):
142+
current_window["title"] = "excluded"
143+
131144
if exclude_title:
132145
current_window["title"] = "excluded"
133146

0 commit comments

Comments
 (0)