-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemi.py
More file actions
155 lines (140 loc) · 5.85 KB
/
Copy pathmemi.py
File metadata and controls
155 lines (140 loc) · 5.85 KB
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
from blessed import Terminal
from threading import Thread
from utils import out, autocomplete
from unicodedata import category
import subprocess
import shlex
import time
import dao
import sys
import io
import os
import re
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MEM_CMD_EXT = 'bat' if sys.platform.startswith('win32') else 'sh'
MEM_CMD = os.path.join(BASE_DIR, f"mem.{MEM_CMD_EXT}")
def _portable_getch(term):
if sys.platform.startswith("win32"):
import msvcrt
return msvcrt.getwch()
return term.getch()
def _draw(term, mem_output_buffer, user_input_buffer):
term.location(0, 0)
print(term.clear)
print(mem_output_buffer.getvalue())
print(term.move_y(term.height - 3))
term.location(0, term.height - 3)
print("mem> ", end="", flush=True)
print(user_input_buffer.getvalue(), end="", flush=True)
def _display_message(term, content):
term.location(0, 0)
print(term.clear)
print(content)
print("\n--------- PRESS ANY KEY TO CONTINUE ----------")
_portable_getch(term)
def memi():
term = Terminal()
mem_streaming_on = True
mem_output_buffer = io.StringIO()
user_input_buffer = io.StringIO()
def stream_mem_content():
process = subprocess.run([MEM_CMD, 'l'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
output = process.stdout.decode("utf-8")
mem_output_buffer.write(output)
_draw(term, mem_output_buffer, user_input_buffer)
while mem_streaming_on:
process = subprocess.run([MEM_CMD, 'l'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
new_output = process.stdout.decode("utf-8")
if output != new_output:
output = new_output
mem_output_buffer.truncate(0)
mem_output_buffer.seek(0)
mem_output_buffer.write(output)
_draw(term, mem_output_buffer, user_input_buffer)
time.sleep(0.5)
mem_thread = Thread(target=stream_mem_content)
mem_thread.daemon = True
mem_thread.start()
with term.fullscreen(), term.cbreak():
_draw(term, mem_output_buffer, user_input_buffer)
while True:
if not term.kbhit(timeout=0):
time.sleep(0.01)
continue
ch = _portable_getch(term)
out(f"ch: {ch}")
out(f"ord: {str(ord(ch))}")
out(f"category: {category(ch)}")
if ch == "\t":
memstate = dao.load_memstate()
command = user_input_buffer.getvalue()
command_frags = re.split(r'\s+', command)
if len(command_frags) < 2:
_draw(term, mem_output_buffer, user_input_buffer)
continue
prefix = command_frags[-1]
if not prefix:
_draw(term, mem_output_buffer, user_input_buffer)
continue
targets = [t for t in memstate["focus_targets"].keys()]
completion = autocomplete(prefix, targets)
user_input_buffer.truncate(0)
user_input_buffer.seek(0)
command = command[:(-1 * len(prefix))] + completion
user_input_buffer.write(command)
_draw(term, mem_output_buffer, user_input_buffer)
continue
if ord(ch) == 127:
out("backspace")
command = user_input_buffer.getvalue()
command = str(command[:-1])
user_input_buffer.truncate(0)
user_input_buffer.seek(0)
user_input_buffer.write(command)
_draw(term, mem_output_buffer, user_input_buffer)
continue
if ch in ["\r", "\n"]:
command = user_input_buffer.getvalue()
user_input_buffer.truncate(0)
user_input_buffer.seek(0)
if command.strip().lower() == "":
_draw(term, mem_output_buffer, user_input_buffer)
continue
if command.strip().lower() in ["q", "quit"]:
mem_streaming_on = False
mem_thread.join()
break
try:
command = [MEM_CMD] + shlex.split(command)
except:
_display_message(term, f'Unable to parse command: {command}')
_draw(term, mem_output_buffer, user_input_buffer)
continue
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
output = process.stderr.decode("utf-8")
except:
_display_message(term, "Unable to decode sub-process stderr using UTF-8")
else:
if output:
_display_message(term, output)
try:
output = process.stdout.decode("utf-8")
except:
_display_message(term, "Unable to decode sub-process stdout using UTF-8")
else:
if output:
_display_message(term, output)
_draw(term, mem_output_buffer, user_input_buffer)
continue
if category(ch)[0] == "C":
out("Control Character")
# Discard control character and immediate following characters
while term.kbhit(timeout=0):
_portable_getch(term)
_draw(term, mem_output_buffer, user_input_buffer)
continue
user_input_buffer.write(ch)
_draw(term, mem_output_buffer, user_input_buffer)
if __name__ == "__main__":
memi()