Add status message, make colors nice and add auto start instructions.
parent
b69d426d85
commit
c3b9e9c067
17
README.md
17
README.md
|
@ -2,3 +2,20 @@
|
|||
|
||||
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 shutil
|
||||
import sys
|
||||
import os
|
||||
import signal
|
||||
from pathlib import Path
|
||||
import antidrift.client as client
|
||||
|
@ -44,16 +42,17 @@ def check_for_xdotool():
|
|||
|
||||
def main() -> None:
|
||||
""" 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()
|
||||
if client.antidrift_daemon_is_running():
|
||||
init_logging(config.client_log_file)
|
||||
client.client_mode(config)
|
||||
else:
|
||||
elif len(sys.argv) == 1:
|
||||
init_logging(config.daemon_log_file)
|
||||
add = AntiDriftDaemon(config)
|
||||
add.run()
|
||||
else:
|
||||
print("ad not running")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -45,6 +45,8 @@ def client_mode(config: Config):
|
|||
help='start session with whiteblocks')
|
||||
parser.add_argument('--stop', action='store_true',
|
||||
help='stop session')
|
||||
parser.add_argument('--status', action='store_true',
|
||||
help='get status from daemon')
|
||||
parser.add_argument('--tailf', action='store_true',
|
||||
help='tail -f log file')
|
||||
args = parser.parse_args()
|
||||
|
@ -55,6 +57,8 @@ def client_mode(config: Config):
|
|||
reply = interface.stop()
|
||||
elif args.tailf:
|
||||
tailf(config)
|
||||
elif args.status:
|
||||
reply = interface.status()
|
||||
else:
|
||||
reply = '[red]no command[/red]'
|
||||
print(reply)
|
||||
|
|
|
@ -19,7 +19,7 @@ class Config(BaseModel):
|
|||
client_log_file: Path = Path()
|
||||
config_file: Path = Path()
|
||||
polling_cycle_ms: int = 500
|
||||
enforce_delay_ms: int = 10000
|
||||
enforce_delay_ms: int = 5000
|
||||
|
||||
class Config:
|
||||
extra = 'forbid'
|
||||
|
|
|
@ -32,11 +32,6 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||
self.enforce_count = 0
|
||||
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,
|
||||
in_signature="as", out_signature="s")
|
||||
def start(self, whiteblocks: List[str]) -> str:
|
||||
|
@ -70,6 +65,22 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||
logging.info(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 _enforce():
|
||||
self.enforce()
|
||||
|
@ -103,8 +114,8 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||
self.enforce_count += 1
|
||||
elif self.enforce_count == 0 and window_is_blocked(config):
|
||||
self.enforce_count += 1
|
||||
delay = config.enforce_delay_ms
|
||||
xwindow.notify(f"AntiDrift will minimize in {delay} ms.")
|
||||
delay = int(config.enforce_delay_ms / 1000)
|
||||
xwindow.notify(f"AntiDrift will minimize in {delay}s.")
|
||||
elif self.enforce_count > 0:
|
||||
xwindow.notify("We are gucci again.")
|
||||
self.enforce_count = 0
|
||||
|
@ -134,9 +145,9 @@ def window_is_blocked(config: Config, silent: bool = False) -> bool:
|
|||
for k in w.keywords:
|
||||
if k in window.keywords:
|
||||
if not silent:
|
||||
logging.debug(f"[green]{window.name[:30]}[/green] "
|
||||
f"allowed by [blue]{w.name}[/blue].")
|
||||
logging.debug(f"[pale_green3]{window.name[:30]}[/pale_green3] "
|
||||
f"allowed by [sky_blue3]{w.name}[/sky_blue3].")
|
||||
return False
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue