-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathspoken_form_test.py
149 lines (119 loc) · 4.47 KB
/
spoken_form_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import json
from typing import Any, Optional
from talon import Context, Module, actions, scope, settings
mod = Module()
mod.mode(
"cursorless_spoken_form_test",
"Used to run tests on the Cursorless spoken forms/grammar",
)
mod.setting(
"cursorless_spoken_form_test_restore_microphone",
str,
desc="The microphone to switch to after the spoken form tests are done. If unset, the microphone that was active before tests started is restored. (If you want to switch back to 'System Default', you should set that as the value here)",
)
ctx = Context()
ctx.matches = r"""
mode: user.cursorless_spoken_form_test
"""
ctx.tags = [
"user.cursorless",
"user.cursorless_default_vocabulary",
]
# Keeps track of the microphone that was active before the spoken form test mode
saved_microphone = "None"
# Keeps a list of modes that were active before the spoken form test mode was
# enabled
saved_modes = []
# Keeps a list of commands run over the course of a spoken form test
commands_run = []
mockedGetValue = ""
@ctx.action_class("user")
class UserActions:
def did_emit_pre_phrase_signal():
return True
def private_cursorless_run_rpc_command_and_wait(
command_id: str, # pyright: ignore [reportGeneralTypeIssues]
arg1: Any,
arg2: Any = None,
):
commands_run.append(arg1)
def private_cursorless_run_rpc_command_no_wait(
command_id: str, # pyright: ignore [reportGeneralTypeIssues]
arg1: Any,
arg2: Any = None,
):
commands_run.append(arg1)
def private_cursorless_run_rpc_command_get(
command_id: str, # pyright: ignore [reportGeneralTypeIssues]
arg1: Any,
arg2: Any = None,
) -> Any:
commands_run.append(arg1)
return mockedGetValue
@mod.action_class
class Actions:
def private_cursorless_spoken_form_test_mode(enable: bool): # pyright: ignore [reportGeneralTypeIssues]
"""Enable/disable Cursorless spoken form test mode"""
global saved_modes, saved_microphone
if enable:
saved_modes = scope.get("mode")
saved_microphone = settings.get(
"user.cursorless_spoken_form_test_restore_microphone",
actions.sound.active_microphone(),
)
disable_modes()
actions.mode.enable("user.cursorless_spoken_form_test")
actions.sound.set_microphone("None")
actions.app.notify(
"Cursorless spoken form tests are running. Talon microphone is disabled."
)
else:
actions.mode.disable("user.cursorless_spoken_form_test")
enable_modes()
actions.sound.set_microphone(saved_microphone)
actions.app.notify(
"Cursorless spoken form tests are done. Talon microphone is re-enabled."
)
def private_cursorless_spoken_form_test(
phrase: str, # pyright: ignore [reportGeneralTypeIssues]
mockedGetValue_: Optional[str],
):
"""Run Cursorless spoken form test"""
global commands_run, mockedGetValue
commands_run = []
if mockedGetValue_:
mockedGetValue = json.loads(mockedGetValue_)
try:
actions.mimic(phrase)
print(json.dumps(commands_run))
except Exception as e:
print(f"{e.__class__.__name__}: {e}")
def private_cursorless_test_extract_decorated_marks(target: Any):
"""Run test for Cursorless private extract decorated marks api"""
marks = actions.user.cursorless_private_extract_decorated_marks(target)
all_decorated_marks_target = actions.user.cursorless_private_build_list_target(
[
actions.user.cursorless_private_build_primitive_target([], mark)
for mark in marks
]
)
actions.user.cursorless_private_action_highlight(
all_decorated_marks_target, "highlight1"
)
def private_cursorless_test_alternate_highlight_nothing():
"""Run test for Cursorless private highlight nothing api"""
actions.user.cursorless_private_action_highlight(
actions.user.cursorless_private_target_nothing(), "highlight1"
)
def enable_modes():
for mode in saved_modes:
try:
actions.mode.enable(mode)
except Exception:
pass
def disable_modes():
for mode in saved_modes:
try:
actions.mode.disable(mode)
except Exception:
pass