-
Notifications
You must be signed in to change notification settings - Fork 0
/
_terminal.py
162 lines (118 loc) · 4.74 KB
/
_terminal.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
150
151
152
153
154
155
156
157
158
159
160
161
162
from dragonfly import (Grammar, CompoundRule, Text, MappingRule, Dictation, Function, Choice)
from dragonfly.windows.clipboard import (Clipboard)
from vim.rules.letter import (barbecue, formatting_choice, format_dictation)
class TerminalEnabler(CompoundRule):
spec = "enable Terminal"
def _process_recognition(self, node, extras):
terminal_bootstrap.disable()
terminal_grammar.enable()
print("Terminal grammar enabled!")
class TerminalDisabler(CompoundRule):
spec = "disable Terminal"
def _process_recognition(self, node, extras):
terminal_grammar.disable()
terminal_bootstrap.enable()
print("Terminal grammar disabled")
git_command_choice_map = {
"pull from master": "pull origin master",
"pull from main": "pull origin main",
"check out": "checkout ",
"new branch": "checkout -b ",
"status": "status",
"stash": "stash",
"log": "log",
"in it": "init",
"initialize": "init",
}
def git_command_choice(name="git_command"):
return Choice(name, git_command_choice_map)
def output_git_command(git_command=None):
command_text = "git "
if git_command is None:
command = Text(command_text)
else:
command = Text(command_text + str(git_command))
command.execute()
def output_git_pull(branch_name):
branch_name = format_branch_name(branch_name)
Text("git pull %s" % branch_name).execute()
def format_branch_name(branch_name):
return barbecue(str(branch_name))
def output_git_push(branch_name, set_up_stream=False):
upstream_text = " -u origin" if set_up_stream else ""
branch_name = format_branch_name(branch_name)
Text("git push%s %s" % (upstream_text, branch_name)).execute()
def get_clipboard_as_text():
clipboard_instance = Clipboard()
clipboard_instance.copy_from_system()
return clipboard_instance.text
def format_directory_name(directory_name, format_type):
if format_type is None:
directory_name = barbecue(str(directory_name))
else:
directory_name = format_dictation(directory_name, format_type)
return directory_name
directory_command_choice_map = {
"make": "mkdir",
"change": "cd",
"remove": "rmdir",
}
def directory_command_choice(name="directory_command"):
return Choice(name, directory_command_choice_map)
def output_directory_command(directory_name, format_type=None, directory_command=None):
if directory_command is not None:
Text(
"%s %s" % (directory_command, format_directory_name(directory_name, format_type))
).execute()
git_clipboard_command_choice_map = {
"clone": "clone",
"reset": "reset",
"revert": "revert",
"add origin": "remote add origin",
}
def git_clipboard_command_choice(name="git_clipboard_command"):
return Choice(name, git_clipboard_command_choice_map)
def output_git_clipboard_command(git_clipboard_command=None):
if git_clipboard_command is not None:
clipboard_output = get_clipboard_as_text()
Text("git %s %s" % (git_clipboard_command, clipboard_output)).execute()
class TerminalUtilities(MappingRule):
mapping = {
"get [<git_command>]": Function(output_git_command),
"get pull [<branch_name>]": Function(output_git_pull),
"get push upstream [<branch_name>]": Function(output_git_push, set_up_stream=True),
"get push [<branch_name>]": Function(output_git_push),
"get <git_clipboard_command> from clipboard": Function(output_git_clipboard_command),
"<directory_command> directory [<format_type>] [<directory_name>]":
Function(output_directory_command),
"zig cash been": Text("zig-cache/bin"),
"code here": Text("code ."),
"code without accessibility here": Text("code --disable-renderer-accessibility ."),
"been": Text("bin"),
"source": Text("src"),
"neo-vim": Text("nvim"),
"neo-vim QT": Text("nvim-qt"),
}
extras = [
Dictation("branch_name", default=""),
Dictation("repository_name", default=""),
Dictation("directory_name", default=""),
git_command_choice("git_command"),
formatting_choice("format_type"),
directory_command_choice("directory_command"),
git_clipboard_command_choice("git_clipboard_command"),
]
# The main Terminal grammar rules are activated here
terminal_bootstrap = Grammar("terminal bootstrap")
terminal_bootstrap.add_rule(TerminalEnabler())
terminal_bootstrap.load()
terminal_grammar = Grammar("terminal grammar")
terminal_grammar.add_rule(TerminalUtilities())
terminal_grammar.add_rule(TerminalDisabler())
terminal_grammar.load()
terminal_grammar.enable()
def unload():
global terminal_grammar
if terminal_grammar:
terminal_grammar.unload()
terminal_grammar = None