Add status message, make colors nice and add auto start instructions.
This commit is contained in:
17
README.md
17
README.md
@@ -2,3 +2,20 @@
|
|||||||
|
|
||||||
Utilize your computer purposefully.
|
Utilize your computer purposefully.
|
||||||
|
|
||||||
|
## Autostart with systemd
|
||||||
|
|
||||||
|
- Create `~/.config/systemd/user/antidrift.service`
|
||||||
|
- Add configuration below and save
|
||||||
|
- Run `systemctl --user enable antidrift.service`
|
||||||
|
|
||||||
|
```
|
||||||
|
[Unit]
|
||||||
|
Description=AntiDrift
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/home/felixm/dot/bin/antidrift
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
11
antidrift.py
11
antidrift.py
@@ -1,9 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
""" AntiDrift """
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import signal
|
import signal
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import antidrift.client as client
|
import antidrift.client as client
|
||||||
@@ -44,16 +42,17 @@ def check_for_xdotool():
|
|||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
""" Main routine that dispatches to client or daemon """
|
""" Main routine that dispatches to client or daemon """
|
||||||
config = Config.load("config.yaml")
|
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||||
|
|
||||||
check_for_xdotool()
|
check_for_xdotool()
|
||||||
if client.antidrift_daemon_is_running():
|
if client.antidrift_daemon_is_running():
|
||||||
init_logging(config.client_log_file)
|
init_logging(config.client_log_file)
|
||||||
client.client_mode(config)
|
client.client_mode(config)
|
||||||
else:
|
elif len(sys.argv) == 1:
|
||||||
init_logging(config.daemon_log_file)
|
init_logging(config.daemon_log_file)
|
||||||
add = AntiDriftDaemon(config)
|
add = AntiDriftDaemon(config)
|
||||||
add.run()
|
add.run()
|
||||||
|
else:
|
||||||
|
print("ad not running")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ def client_mode(config: Config):
|
|||||||
help='start session with whiteblocks')
|
help='start session with whiteblocks')
|
||||||
parser.add_argument('--stop', action='store_true',
|
parser.add_argument('--stop', action='store_true',
|
||||||
help='stop session')
|
help='stop session')
|
||||||
|
parser.add_argument('--status', action='store_true',
|
||||||
|
help='get status from daemon')
|
||||||
parser.add_argument('--tailf', action='store_true',
|
parser.add_argument('--tailf', action='store_true',
|
||||||
help='tail -f log file')
|
help='tail -f log file')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -55,6 +57,8 @@ def client_mode(config: Config):
|
|||||||
reply = interface.stop()
|
reply = interface.stop()
|
||||||
elif args.tailf:
|
elif args.tailf:
|
||||||
tailf(config)
|
tailf(config)
|
||||||
|
elif args.status:
|
||||||
|
reply = interface.status()
|
||||||
else:
|
else:
|
||||||
reply = '[red]no command[/red]'
|
reply = '[red]no command[/red]'
|
||||||
print(reply)
|
print(reply)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class Config(BaseModel):
|
|||||||
client_log_file: Path = Path()
|
client_log_file: Path = Path()
|
||||||
config_file: Path = Path()
|
config_file: Path = Path()
|
||||||
polling_cycle_ms: int = 500
|
polling_cycle_ms: int = 500
|
||||||
enforce_delay_ms: int = 10000
|
enforce_delay_ms: int = 5000
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
extra = 'forbid'
|
extra = 'forbid'
|
||||||
|
|||||||
@@ -32,11 +32,6 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
self.enforce_count = 0
|
self.enforce_count = 0
|
||||||
self.enforce_value = int(config.enforce_delay_ms / config.polling_cycle_ms)
|
self.enforce_value = int(config.enforce_delay_ms / config.polling_cycle_ms)
|
||||||
|
|
||||||
@dbus.service.method(dbus_interface=IFACE,
|
|
||||||
in_signature="", out_signature="s")
|
|
||||||
def status(self) -> str:
|
|
||||||
return "active"
|
|
||||||
|
|
||||||
@dbus.service.method(dbus_interface=IFACE,
|
@dbus.service.method(dbus_interface=IFACE,
|
||||||
in_signature="as", out_signature="s")
|
in_signature="as", out_signature="s")
|
||||||
def start(self, whiteblocks: List[str]) -> str:
|
def start(self, whiteblocks: List[str]) -> str:
|
||||||
@@ -70,6 +65,22 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
logging.info(m)
|
logging.info(m)
|
||||||
return m
|
return m
|
||||||
|
|
||||||
|
@dbus.service.method(dbus_interface=IFACE,
|
||||||
|
in_signature="", out_signature="s")
|
||||||
|
def status(self) -> str:
|
||||||
|
white_active = bool(self.config.active_whiteblocks)
|
||||||
|
black_active = bool(self.config.active_blackblocks)
|
||||||
|
m = 'ad '
|
||||||
|
match (white_active, black_active):
|
||||||
|
case (True, _):
|
||||||
|
m += 'wb: '
|
||||||
|
m += ' '.join(map(lambda b: b.name, self.config.active_whiteblocks))
|
||||||
|
case (False, True):
|
||||||
|
m += 'bb'
|
||||||
|
case _:
|
||||||
|
m = 'inactive'
|
||||||
|
return m
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
def _enforce():
|
def _enforce():
|
||||||
self.enforce()
|
self.enforce()
|
||||||
@@ -103,8 +114,8 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
self.enforce_count += 1
|
self.enforce_count += 1
|
||||||
elif self.enforce_count == 0 and window_is_blocked(config):
|
elif self.enforce_count == 0 and window_is_blocked(config):
|
||||||
self.enforce_count += 1
|
self.enforce_count += 1
|
||||||
delay = config.enforce_delay_ms
|
delay = int(config.enforce_delay_ms / 1000)
|
||||||
xwindow.notify(f"AntiDrift will minimize in {delay} ms.")
|
xwindow.notify(f"AntiDrift will minimize in {delay}s.")
|
||||||
elif self.enforce_count > 0:
|
elif self.enforce_count > 0:
|
||||||
xwindow.notify("We are gucci again.")
|
xwindow.notify("We are gucci again.")
|
||||||
self.enforce_count = 0
|
self.enforce_count = 0
|
||||||
@@ -134,9 +145,9 @@ def window_is_blocked(config: Config, silent: bool = False) -> bool:
|
|||||||
for k in w.keywords:
|
for k in w.keywords:
|
||||||
if k in window.keywords:
|
if k in window.keywords:
|
||||||
if not silent:
|
if not silent:
|
||||||
logging.debug(f"[green]{window.name[:30]}[/green] "
|
logging.debug(f"[pale_green3]{window.name[:30]}[/pale_green3] "
|
||||||
f"allowed by [blue]{w.name}[/blue].")
|
f"allowed by [sky_blue3]{w.name}[/sky_blue3].")
|
||||||
return False
|
return False
|
||||||
if not silent:
|
if not silent:
|
||||||
xwindow.notify(f"[red]{window.name[:30]}[/red] not on any whiteblock.")
|
xwindow.notify(f"'{window.name[:30]}…' not on any whiteblock.")
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user