diff options
Diffstat (limited to 'docs/dbus/ferdium_bar.py')
-rw-r--r-- | docs/dbus/ferdium_bar.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/docs/dbus/ferdium_bar.py b/docs/dbus/ferdium_bar.py new file mode 100644 index 000000000..6fd5d8c30 --- /dev/null +++ b/docs/dbus/ferdium_bar.py | |||
@@ -0,0 +1,102 @@ | |||
1 | import asyncio | ||
2 | import argparse | ||
3 | import html | ||
4 | |||
5 | from ferdium_dbus import Client | ||
6 | |||
7 | |||
8 | async def toggle_window(client, args): | ||
9 | """Toggle window visibility""" | ||
10 | |||
11 | await client.toggle_window() | ||
12 | |||
13 | |||
14 | async def toggle_mute(client, args): | ||
15 | """Toggle mute status""" | ||
16 | |||
17 | await client.toggle_mute() | ||
18 | |||
19 | |||
20 | async def unread(client, args): | ||
21 | """Get unread messages count""" | ||
22 | |||
23 | def callback(): | ||
24 | """Print unread count(s)""" | ||
25 | |||
26 | # For each service | ||
27 | counts = {} | ||
28 | for service in client.unread_services: | ||
29 | name, direct, indirect = service | ||
30 | safe_name = html.escape(name) | ||
31 | |||
32 | # If it's exactly the service we're looking for, just return the count | ||
33 | if safe_name == args.services: | ||
34 | count = direct | ||
35 | if not args.direct: | ||
36 | count += indirect | ||
37 | print(count) | ||
38 | return | ||
39 | |||
40 | # If the service in included in the services we're looking for | ||
41 | if args.services in ("total", "all") or safe_name in args.services: | ||
42 | counts[safe_name] = direct | ||
43 | if not args.direct: | ||
44 | counts[safe_name] += indirect | ||
45 | |||
46 | # Get total notifications | ||
47 | if args.services == "total": | ||
48 | print(sum(counts.values())) | ||
49 | return | ||
50 | |||
51 | # Finally, print each service notifications on a different line | ||
52 | print( | ||
53 | "\n".join( | ||
54 | f"{name}: {count}" | ||
55 | for name, count in counts.items() | ||
56 | ) | ||
57 | ) | ||
58 | |||
59 | # Do print counts and keep running if tail mode enabled | ||
60 | callback() | ||
61 | if args.tail: | ||
62 | client.on_change(callback) | ||
63 | await asyncio.get_running_loop().create_future() | ||
64 | |||
65 | |||
66 | async def main(): | ||
67 | """Main cli interface""" | ||
68 | |||
69 | # Define commands | ||
70 | commands = { | ||
71 | "unread": unread, | ||
72 | "toggle-mute": toggle_mute, | ||
73 | "toggle-window": toggle_window, | ||
74 | } | ||
75 | |||
76 | # Arguments parser | ||
77 | argparser = argparse.ArgumentParser(description="Script to interact with Ferdium on your bar") | ||
78 | subparsers = argparser.add_subparsers(dest="command", required=True) | ||
79 | # Unread command | ||
80 | argparser_unread = subparsers.add_parser("unread", help=unread.__doc__) | ||
81 | argparser_unread.add_argument("-s", "--services", default="total", help="Which services to get notifications from {total, all, <name>} (the name can be a comma-separated list)") | ||
82 | argparser_unread.add_argument("-d", "--direct", action="store_true", default=False, help="Get only direct (mentions or DM) messages") | ||
83 | argparser_unread.add_argument("-t", "--tail", action="store_true", default=False, help="Keep running and print on change") | ||
84 | # Toggle mute and toggle window commands | ||
85 | argparser_toggle_mute = subparsers.add_parser("toggle-mute", help=toggle_mute.__doc__) | ||
86 | argparser_toggle_window = subparsers.add_parser("toggle-window", help=toggle_window.__doc__) | ||
87 | # Get args | ||
88 | args = argparser.parse_args() | ||
89 | |||
90 | # Initialise ferdium client | ||
91 | client = Client() | ||
92 | await client.connect() | ||
93 | if not client.running: | ||
94 | print("not running") | ||
95 | return | ||
96 | |||
97 | # Execute command | ||
98 | await commands[args.command](client, args) | ||
99 | |||
100 | |||
101 | if __name__ == "__main__": | ||
102 | asyncio.run(main()) | ||