Browse Source

It's official

v0.1-dev is ready for release, as v1.0
apollo 4 days ago
parent
commit
892d3b94f8
1 changed files with 31 additions and 11 deletions
  1. 31 11
      tyrell.py

+ 31 - 11
tyrell.py

@@ -8,7 +8,7 @@ from time import sleep as _sleep
 from typing import Dict as _Dict, List as _List, Optional as _Option, Any as _Any
 
 try:
-    from click import echo as _echo, style as _style
+    from click import echo as _echo, style as _style, command as _command, option as _option, argument as _arg
 except ImportError:
     print("ERROR Please activate a virtual environment and install requirements.txt")
     exit()
@@ -284,7 +284,7 @@ def _match_key(key: str, event: _KeyboardEvent) -> bool:
             return True
     return event.name == k
 
-def _key_output(write: str, delay:float=50.0, hold:float=20.0):
+def _key_write(write: str, delay:float=50.0, hold:float=20.0):
     if ', ' in write:
         for part in write.split(', '):
             if '+' in part:
@@ -424,14 +424,14 @@ class Action:
         elif self.kind == 'key' and self.write is not None:
             if self.hold is not None:
                 if self.delay is not None:
-                    _key_output(self.write, delay=self.delay, hold=self.hold)
+                    _key_write(self.write, delay=self.delay, hold=self.hold)
                 else:
-                    _key_output(self.write, delay=global_delay, hold=self.hold)
+                    _key_write(self.write, delay=global_delay, hold=self.hold)
             else:
                 if self.delay is not None:
-                    _key_output(self.write, delay=self.delay, hold=global_hold)
+                    _key_write(self.write, delay=self.delay, hold=global_hold)
                 else:
-                    _key_output(self.write, delay=global_delay, hold=global_hold)
+                    _key_write(self.write, delay=global_delay, hold=global_hold)
         elif self.kind == 'click' and self.button is not None:
             _mouse_down(self.button)
             if self.hold is not None:
@@ -550,10 +550,18 @@ class Profile:
                         self._help_screen.append(_style(f"{a.key:15}", fg='bright_cyan') + f" executes {entry.removesuffix(".toml")}")
                 else:
                     self._help_screen.append(_style(f"{a.key:15}", fg='bright_cyan') + f" toggles {entry.removesuffix(".toml")} (Mirrors {_style(f'{a.mirror}', fg='bright_cyan')})")
+                if a.write is not None:
+                    a.write = self.fill_in(a.write)
                 self._actions[entry.removesuffix(".toml")] = a
             except _TomlDecodeError:
                 _print_warn(f"{file} => invalid toml")
 
+    def fill_in(self, text: str) -> str:
+        if self.placeholders['name']:
+            if "{name}" in text:
+                return text.replace("{name}", self.name)
+        return text
+
     def is_on(self) -> bool:
         return self._state
 
@@ -564,6 +572,9 @@ class Profile:
             self._state = True
         elif off and not on:
             self._state = False
+            for act in self._actions:
+                a = self._actions[act]
+                a.toggle(off=True)
         return self._state
     
     def show_help(self):
@@ -593,6 +604,7 @@ class Profile:
             elif self._trigger_helper(event) and self.is_on():
                 self.show_help()
                 self.toggle(off=True)
+                _print_off("Tyrell")
                 return
         # Process actions defined
         for act in self._actions:
@@ -608,6 +620,7 @@ class Profile:
                         _print_off(f"{act}")
                     #_print_ok(f"{act} => '{a.kind}' action, trigger on '{a.mirror}' (toggle with '{a.key}')")
                 self.toggle(off=True)
+                _print_off("Tyrell")
                 return
             elif a.trigger_mirror(event):
                 if a.is_on() and event.event_type is not None:
@@ -624,6 +637,7 @@ class Profile:
             await _asleep(self.tick / 1000)
 
     async def run(self):
+        self.show_help()
         _h = _key_hook(self._callback)
         # I'm not sure why if I don't have any async TaskGroup
         # We get some ugly errors
@@ -642,13 +656,19 @@ class Profile:
             background.create_task(self._ticker())
         _key_unhook(_h)
 
+
+@_command()
+@_arg('profile', required=True)
+def _main(profile: str):
+    p = Profile(profile)
+    try:
+        _run(p.run())
+    except KeyboardInterrupt:
+        print()
+
 if __name__ == "__main__":
     if not _exists("_example"):
         _print_warn("Missing '_example' profile, creating...")
         Profile("_example")
         _print_ok("")
-    p = Profile("_test")
-    try:
-        _run(p.run())
-    except KeyboardInterrupt:
-        print()
+    _main()