Skip to content

Commit 4da4e5a

Browse files
committed
Run pre-commit
1 parent 45e93eb commit 4da4e5a

File tree

1 file changed

+101
-94
lines changed

1 file changed

+101
-94
lines changed

adafruit_prompt_toolkit/__init__.py

Lines changed: 101 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -23,106 +23,111 @@
2323

2424
from .history import InMemoryHistory
2525

26+
2627
def _prompt(message="", *, input=None, output=None, history=None):
27-
output.write(message.encode("utf-8"))
28-
commands = []
29-
control_command = []
30-
offset = 0
31-
selected_history_entry = None
32-
while not commands or commands[-1] != ord('\r'):
33-
c = input.read(1)
34-
print(c, c[0])
35-
36-
if control_command:
37-
control_command.append(c[0])
38-
print("control", control_command)
39-
# Check for unsupported codes. If one is found, add the second character to the
40-
# plain command and ignore the escape.
41-
if len(control_command) == 2 and not control_command[-1] == ord(b"["):
42-
commands.append(control_command[-1])
43-
control_command = []
44-
# Command is done when it doesn't end with a number
45-
if len(control_command) > 2 and not (ord("0") <= control_command[-1] <= ord("9")):
46-
echo = False
47-
cc = control_command[-1]
48-
if cc == ord("A") or cc == ord("B"):
49-
if history is None:
50-
control_command = []
51-
output.write(b"\a")
52-
continue
53-
strings = history.get_strings()
54-
if not strings:
55-
control_command = []
56-
output.write(b"\a")
57-
continue
58-
if cc == ord("A"):
59-
#up
60-
if selected_history_entry is None:
61-
selected_history_entry = 1
62-
else:
63-
selected_history_entry += 1
64-
if selected_history_entry > len(strings):
65-
output.write(b"\a")
66-
selected_history_entry = len(strings)
28+
output.write(message.encode("utf-8"))
29+
commands = []
30+
control_command = []
31+
offset = 0
32+
selected_history_entry = None
33+
while not commands or commands[-1] != ord("\r"):
34+
c = input.read(1)
35+
print(c, c[0])
36+
37+
if control_command:
38+
control_command.append(c[0])
39+
print("control", control_command)
40+
# Check for unsupported codes. If one is found, add the second character to the
41+
# plain command and ignore the escape.
42+
if len(control_command) == 2 and not control_command[-1] == ord(b"["):
43+
commands.append(control_command[-1])
44+
control_command = []
45+
# Command is done when it doesn't end with a number
46+
if len(control_command) > 2 and not (
47+
ord("0") <= control_command[-1] <= ord("9")
48+
):
49+
echo = False
50+
cc = control_command[-1]
51+
if cc == ord("A") or cc == ord("B"):
52+
if history is None:
53+
control_command = []
54+
output.write(b"\a")
55+
continue
56+
strings = history.get_strings()
57+
if not strings:
58+
control_command = []
59+
output.write(b"\a")
60+
continue
61+
if cc == ord("A"):
62+
# up
63+
if selected_history_entry is None:
64+
selected_history_entry = 1
6765
else:
68-
# down
69-
print("down")
70-
if selected_history_entry is None:
66+
selected_history_entry += 1
67+
if selected_history_entry > len(strings):
7168
output.write(b"\a")
72-
else:
73-
selected_history_entry -= 1
74-
if selected_history_entry < 1:
75-
selected_history_entry = None
76-
# Move the cursor left as much as our current command
77-
for _ in commands:
78-
output.write(b"\b")
79-
# Set and print the new command
80-
commands = list(strings[-selected_history_entry].encode("utf-8"))
81-
output.write(bytes(commands))
82-
# Clear the rest of the line
83-
output.write(b"\x1b[K")
84-
elif cc == ord("C"):
85-
echo = True
86-
offset = max(0, offset - 1)
87-
elif cc == ord("D"):
88-
echo = True
89-
offset += 1
90-
91-
if echo:
92-
b = bytes(control_command)
93-
print("echo", b)
94-
output.write(b)
95-
control_command = []
96-
continue
97-
elif c == b"\x1b":
98-
control_command.append(c[0])
99-
continue
100-
elif offset == 0 or c == b"\r":
101-
commands.append(c[0])
102-
else:
103-
commands[-offset] = c[0]
104-
offset -= 1
105-
106-
if c[-1] == 127:
107-
commands.pop()
108-
commands.pop()
109-
output.write(b"\b\x1b[K")
110-
else:
111-
output.write(c)
112-
113-
print(commands, not commands)
114-
output.write(b"\n")
115-
print("encoded", commands)
116-
# Remove the newline
117-
commands.pop()
118-
decoded = bytes(commands).decode("utf-8")
119-
if history:
120-
history.append_string(decoded)
121-
return decoded
69+
selected_history_entry = len(strings)
70+
else:
71+
# down
72+
print("down")
73+
if selected_history_entry is None:
74+
output.write(b"\a")
75+
else:
76+
selected_history_entry -= 1
77+
if selected_history_entry < 1:
78+
selected_history_entry = None
79+
# Move the cursor left as much as our current command
80+
for _ in commands:
81+
output.write(b"\b")
82+
# Set and print the new command
83+
commands = list(strings[-selected_history_entry].encode("utf-8"))
84+
output.write(bytes(commands))
85+
# Clear the rest of the line
86+
output.write(b"\x1b[K")
87+
elif cc == ord("C"):
88+
echo = True
89+
offset = max(0, offset - 1)
90+
elif cc == ord("D"):
91+
echo = True
92+
offset += 1
93+
94+
if echo:
95+
b = bytes(control_command)
96+
print("echo", b)
97+
output.write(b)
98+
control_command = []
99+
continue
100+
elif c == b"\x1b":
101+
control_command.append(c[0])
102+
continue
103+
elif offset == 0 or c == b"\r":
104+
commands.append(c[0])
105+
else:
106+
commands[-offset] = c[0]
107+
offset -= 1
108+
109+
if c[-1] == 127:
110+
commands.pop()
111+
commands.pop()
112+
output.write(b"\b\x1b[K")
113+
else:
114+
output.write(c)
115+
116+
print(commands, not commands)
117+
output.write(b"\n")
118+
print("encoded", commands)
119+
# Remove the newline
120+
commands.pop()
121+
decoded = bytes(commands).decode("utf-8")
122+
if history:
123+
history.append_string(decoded)
124+
return decoded
125+
122126

123127
def prompt(message="", *, input=None, output=None):
124128
return _prompt(message, input=input, output=output)
125129

130+
126131
class PromptSession:
127132
def __init__(self, message="", *, input=None, output=None, history=None):
128133
self.message = message
@@ -134,6 +139,8 @@ def __init__(self, message="", *, input=None, output=None, history=None):
134139
def prompt(self, message=None) -> str:
135140
message = message if message else self.message
136141

137-
decoded = _prompt(message, input=self._input, output=self._output, history=self.history)
142+
decoded = _prompt(
143+
message, input=self._input, output=self._output, history=self.history
144+
)
138145

139146
return decoded

0 commit comments

Comments
 (0)