-
Notifications
You must be signed in to change notification settings - Fork 0
/
input02.py
188 lines (159 loc) · 6.16 KB
/
input02.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Test script for phrase.
import sys
import termios
import select
import time
import glob
import os
prompt_dict = {} # Added prompt_dict
table1_dict = {}
def parse_so61utf8(filename):
with open(filename, 'r', encoding='utf-8') as file:
lines = file.read().split('\n')
TABLE = "qwertyuiopasdfghjkl;zxcvbnm,./_"
TABLE = TABLE.upper()
# Create a dictionary to map TABLE keys to characters
table_dict = {}
for line in lines:
if line: # and not line.startswith("___"):
key, value = line[:3], line[3:]
key = key.strip()
value = value.strip()
replaced_key = key.replace('_', '')
prompt_dict[replaced_key] = value # Populate prompt_dict as well
for i, char in enumerate(value):
# Use modulo to ensure cyclic access to TABLE characters
replaced_key = key.replace('_', TABLE[i % len(TABLE)], 1)
table1_dict[replaced_key] = char
if char in table_dict:
table_dict[char].append(replaced_key)
else:
table_dict[char] = [replaced_key]
return table_dict
def build_ab_dict(ab_folder, table_dict):
ab_dict = {}
for ab_file in glob.glob(os.path.join(ab_folder, '*.ab')):
with open(ab_file, 'r', encoding='utf-8') as file:
lines = file.read().split('\n')
ab_entry = ''
for line in lines:
if line:
key, data = line.split(None, 1)
if key == '_':
key = ''
for char in data:
if char in table_dict:
key += table_dict[char][0][0]
ab_entry = key.strip()
data = data.strip()
ab_dict[ab_entry] = data
return ab_dict
def debug_ab(ab_dict):
for key, value in ab_dict.items():
print(f'{key} => {value}')
# time.sleep(1)
# Global variables
ab_folder = "ab"
so61utf8_dict = parse_so61utf8("so61utf8.txt")
ab_dict = build_ab_dict(ab_folder, so61utf8_dict)
def read_key():
key = None
oldt = termios.tcgetattr(sys.stdin)
newt = termios.tcgetattr(sys.stdin)
newt[3] = newt[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(sys.stdin, termios.TCSANOW, newt)
rlist, _, _ = select.select([sys.stdin], [], [], 10)
if rlist:
key = sys.stdin.read(1)
print(f"Input Key: {key}") # Prompt for the key you input
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, oldt)
return key
def show_dict(keys, table):
keys = keys.upper()
if keys in table:
output_prompt = table[keys]
print("1234567890")
for i in range(0, len(output_prompt), 10):
print(output_prompt[i:i+10])
def prompt_keys(input_buffer):
# Display all keys in the input buffer, with spaces shown as "_"
prompt_buffer = input_buffer.replace(' ', '_')
print(f"Prompt Keys: {prompt_buffer}")
def process_input_buffer(input_buffer, output_buffer):
# Split input_buffer into 3-key pairs and perform the TABLE search
output_table_keys = ""
# ab_buffer = input_buffer.replace('_', '').upper()
# if ab_buffer in ab_dict:
# output_table_keys += ab_dict[ab_buffer]
# else:
for i in range(0, len(input_buffer), 3):
current_key = input_buffer[i:i+3]
while len(current_key) < 3:
current_key += "_"
current_key = current_key.upper()
if current_key in table1_dict:
output_table_keys += table1_dict[current_key]
# output_table_keys += so61utf8_dict[current_key]
output_buffer = output_table_keys
print(f"Input Buffer: {input_buffer.replace(' ', '_')}")
print(f"Output Buffer: {output_buffer.replace(' ', '_')}")
print(f"Current Input Key: {input_buffer}")
print(f"Output String: {output_table_keys}")
def clear_outstr():
# Define your clear_outstr function if needed
pass
def do_work():
input_buffer = ""
output_buffer = ""
prompt_buffer = "" # Added prompt_buffer
oldt = termios.tcgetattr(sys.stdin) # Capture the terminal settings
# show_dict('', ab_dict)
show_dict('', prompt_dict)
while True:
key = read_key()
if key is None:
break
if ord(key) == 10: # Enter key (newline)
process_input_buffer(input_buffer, output_buffer)
clear_outstr()
input_buffer = ""
output_buffer = ""
prompt_buffer = ""
# show_dict('', ab_dict)
show_dict('', prompt_dict)
elif key == ' ':
ab_buffer = input_buffer.replace('_', '').upper()
if ab_buffer in ab_dict:
output_table_keys = ab_dict[ab_buffer]
print(f"Output String: {output_table_keys}")
input_buffer = ""
else:
input_buffer += '_'
for i in range(0, len(input_buffer), 3):
current_key = input_buffer[i:i+3]
while len(current_key) < 3:
current_key += "_"
input_buffer += '_'
process_input_buffer(input_buffer, output_buffer)
print(f"Current Input Key: {input_buffer}")
print(f"Output Buffer: {output_buffer}")
prompt_buffer = ""
# show_dict('', ab_dict)
show_dict('', prompt_dict)
elif ord(key) == 127: # Handle backspace
if input_buffer:
input_buffer = input_buffer[:-1]
output_buffer = output_buffer[:-1]
prompt_buffer = prompt_buffer[:-1]
elif key == '`':
debug_ab(ab_dict)
else:
input_buffer += key
output_buffer += key
prompt_buffer += key
# show_dict(prompt_buffer, ab_dict)
show_dict(prompt_buffer, prompt_dict)
prompt_keys(input_buffer) # Call the prompt_keys function for each input key
termios.tcsetattr(sys.stdin, termios.TCSANOW, oldt) # Restore the terminal settings
# Call the do_work function to start reading and processing input keys
do_work()