diff --git a/aw_watcher_window/config.py b/aw_watcher_window/config.py index cfa2c7b..1e949e1 100644 --- a/aw_watcher_window/config.py +++ b/aw_watcher_window/config.py @@ -5,6 +5,7 @@ default_config = """ [aw-watcher-window] exclude_title = false +exclude_titles = [] poll_time = 1.0 strategy_macos = "swift" """.strip() @@ -19,6 +20,7 @@ def parse_args(): default_poll_time = config["poll_time"] default_exclude_title = config["exclude_title"] + default_exclude_titles = config["exclude_titles"] default_strategy_macos = config["strategy_macos"] parser = argparse.ArgumentParser( @@ -33,6 +35,13 @@ def parse_args(): action="store_true", default=default_exclude_title, ) + parser.add_argument( + "--exclude-titles", + dest="exclude_titles", + nargs='+', + default=default_exclude_titles, + help="List of regular expression patterns to exclude from tracking. Each pattern can match any part of a window title. Use standard regex syntax. For example, to exclude windows containing 'Game', simply use 'Game'. Can specify multiple patterns." + ) parser.add_argument("--verbose", dest="verbose", action="store_true") parser.add_argument( "--poll-time", dest="poll_time", type=float, default=default_poll_time diff --git a/aw_watcher_window/main.py b/aw_watcher_window/main.py index 79b23bd..c3c133a 100644 --- a/aw_watcher_window/main.py +++ b/aw_watcher_window/main.py @@ -1,5 +1,6 @@ import logging import os +import re import signal import subprocess import sys @@ -22,7 +23,6 @@ if log_level: logger.setLevel(logging.__getattribute__(log_level.upper())) - def kill_process(pid): logger.info("Killing process {}".format(pid)) try: @@ -30,7 +30,6 @@ def kill_process(pid): except ProcessLookupError: logger.info("Process {} already dead".format(pid)) - def main(): args = parse_args() @@ -92,10 +91,10 @@ def main(): poll_time=args.poll_time, strategy=args.strategy, exclude_title=args.exclude_title, + exclude_titles=[try: re.compile(title, re.IGNORECASE) except re.error: re.compile(re.escape(title), re.IGNORECASE) for title in args.exclude_titles] ) - -def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False): +def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False, exclude_titles=[]): while True: if os.getppid() == 1: logger.info("window-watcher stopped because parent process died") @@ -128,6 +127,10 @@ def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False): if current_window is None: logger.debug("Unable to fetch window, trying again on next poll") else: + for pattern in exclude_titles: + if pattern.search(current_window["title"]): + current_window["title"] = "excluded" + if exclude_title: current_window["title"] = "excluded" @@ -141,4 +144,4 @@ def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False): bucket_id, current_window_event, pulsetime=poll_time + 1.0, queued=True ) - sleep(poll_time) + sleep(poll_time) \ No newline at end of file