/
history.py
125 lines (97 loc) · 2.51 KB
/
history.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
import os
import itertools
from atomicwrites import atomic_write
from collections import deque
from talon import app, webview
from talon.engine import engine
from talon_init import TALON_HOME
from talon.voice import Context
from ..utils import optional_numerals, text_to_number
context = Context("history")
path = os.path.join(TALON_HOME, "last_phrase")
WEBVIEW = True
FONT_SIZE = 12
BORDER_SIZE = int(FONT_SIZE / 6)
NOTIFY = False
LAST_COUNT = 5
LAST_MAX = 50
css_template = (
"""
<style type="text/css">
body {
padding: 0;
margin: 0;
font-size: """
+ str(FONT_SIZE)
+ """px;
-webkit-border-vertical-spacing: """
+ str(BORDER_SIZE)
+ """px;
-webkit-border-horizontal-spacing: """
+ str(BORDER_SIZE)
+ """px;
}
.contents {
width: 100%;
}
td {
text-align: left;
margin: 0;
padding: 0;
padding-left: 5px;
width: 1px;
white-space: nowrap;
}
</style>
"""
)
last_template = (
css_template
+ """
<style type="text/css">
tr:first-child { background: black; color: white; }
</style>
<div class="contents">
<table>
{% for phrase in last_items %}
<tr><td>{{ phrase }}</td></tr>
{% endfor %}
</table>
</div>
"""
)
if WEBVIEW:
webview = webview.Webview()
webview.body = "<i>[waiting for phrase]</i>"
# webview.show()
# only use a deque for the webview
last_items = deque(maxlen=LAST_MAX)
def parse_phrase(phrase):
return " ".join(word.split("\\")[0] for word in phrase)
def on_phrase(j):
global LAST_COUNT
phrase = parse_phrase(j.get("phrase", []))
cmd = j["cmd"]
if cmd == "p.end":
if phrase:
with atomic_write(path, overwrite=True) as f:
f.write(phrase)
if WEBVIEW:
last_items.appendleft(phrase)
phrase_words = phrase.split()
if "history show" in phrase and phrase_words[-1].isdigit():
LAST_COUNT = int(phrase_words[-1])
webview.render(last_template, last_items=list(itertools.islice(last_items, 0, LAST_COUNT)))
if NOTIFY:
app.notify(body=phrase)
engine.register("phrase", on_phrase)
def close_history():
webview.hide()
def open_history():
webview.show()
context.keymap(
{
"history (close | hide)": lambda _: close_history(),
"history (open | show)" + optional_numerals(): lambda _: open_history(),
}
)