|
| 1 | +import json |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from talon import Context, Module, actions, scope |
| 5 | + |
| 6 | +mod = Module() |
| 7 | + |
| 8 | +mod.mode( |
| 9 | + "cursorless_spoken_form_test", |
| 10 | + "Used to run tests on the Cursorless spoken forms/grammar", |
| 11 | +) |
| 12 | + |
| 13 | +ctx = Context() |
| 14 | + |
| 15 | +ctx.matches = r""" |
| 16 | +mode: user.cursorless_spoken_form_test |
| 17 | +""" |
| 18 | + |
| 19 | +ctx.tags = [ |
| 20 | + "user.cursorless", |
| 21 | + "user.cursorless_default_vocabulary", |
| 22 | +] |
| 23 | + |
| 24 | +# Keeps track of the microphone that was active before the spoken form test mode |
| 25 | +saved_microphone = "None" |
| 26 | + |
| 27 | +# Keeps a list of modes that were active before the spoken form test mode was |
| 28 | +# enabled |
| 29 | +saved_modes = [] |
| 30 | + |
| 31 | +# Keeps a list of commands run over the course of a spoken form test |
| 32 | +commands_run = [] |
| 33 | + |
| 34 | + |
| 35 | +@ctx.action_class("user") |
| 36 | +class UserActions: |
| 37 | + def did_emit_pre_phrase_signal(): |
| 38 | + return True |
| 39 | + |
| 40 | + def private_cursorless_run_rpc_command_and_wait( |
| 41 | + command_id: str, arg1: Any, arg2: Any = None |
| 42 | + ): |
| 43 | + commands_run.append(arg1) |
| 44 | + |
| 45 | + def private_cursorless_run_rpc_command_no_wait( |
| 46 | + command_id: str, arg1: Any, arg2: Any = None |
| 47 | + ): |
| 48 | + commands_run.append(arg1) |
| 49 | + |
| 50 | + def private_cursorless_run_rpc_command_get( |
| 51 | + command_id: str, arg1: Any, arg2: Any = None |
| 52 | + ) -> Any: |
| 53 | + commands_run.append(arg1) |
| 54 | + |
| 55 | + |
| 56 | +@mod.action_class |
| 57 | +class Actions: |
| 58 | + def private_cursorless_spoken_form_test_mode(enable: bool): |
| 59 | + """Enable/disable Cursorless spoken form test mode""" |
| 60 | + global saved_modes, saved_microphone |
| 61 | + |
| 62 | + if enable: |
| 63 | + saved_modes = scope.get("mode") |
| 64 | + saved_microphone = actions.sound.active_microphone() |
| 65 | + |
| 66 | + disable_modes() |
| 67 | + actions.mode.enable("user.cursorless_spoken_form_test") |
| 68 | + actions.sound.set_microphone("None") |
| 69 | + |
| 70 | + actions.app.notify( |
| 71 | + "Cursorless spoken form tests are running. Talon microphone is disabled." |
| 72 | + ) |
| 73 | + else: |
| 74 | + actions.mode.disable("user.cursorless_spoken_form_test") |
| 75 | + enable_modes() |
| 76 | + actions.sound.set_microphone(saved_microphone) |
| 77 | + |
| 78 | + actions.app.notify( |
| 79 | + "Cursorless spoken form tests are done. Talon microphone is re-enabled." |
| 80 | + ) |
| 81 | + |
| 82 | + def private_cursorless_spoken_form_test(phrase: str): |
| 83 | + """Run Cursorless spoken form test""" |
| 84 | + global commands_run |
| 85 | + commands_run = [] |
| 86 | + |
| 87 | + try: |
| 88 | + actions.mimic(phrase) |
| 89 | + print(json.dumps(commands_run)) |
| 90 | + except Exception as e: |
| 91 | + print(f"{e.__class__.__name__}: {e}") |
| 92 | + |
| 93 | + |
| 94 | +def enable_modes(): |
| 95 | + for mode in saved_modes: |
| 96 | + try: |
| 97 | + actions.mode.enable(mode) |
| 98 | + except Exception: |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +def disable_modes(): |
| 103 | + for mode in saved_modes: |
| 104 | + try: |
| 105 | + actions.mode.disable(mode) |
| 106 | + except Exception: |
| 107 | + pass |
0 commit comments