Skip to content

Commit ccee5d4

Browse files
committed
add Backspace handling :-)
1 parent cd4b9de commit ccee5d4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

nonblocking_serialinput.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,19 @@ def echo_print(self):
209209
# line_count += 1
210210
# moveback = terminal.ANSIControl.cursor.next_line(line_count)
211211

212-
# execute all the things ;-)
213-
print(
212+
text = (
214213
"{move}"
215214
"{line}"
216215
"{moveback}"
217216
"".format(
218217
move=move,
219218
line=line,
220219
moveback=moveback,
221-
),
222-
end="",
220+
)
223221
)
222+
print("\n\n\n\n\n\n{}\n\n\n\n\n\n".format(repr(text)))
223+
# execute all the things ;-)
224+
print(text, end="")
224225

225226
def print(self, *args):
226227
# def print(self, *args, end="\n"):
@@ -299,6 +300,14 @@ def _buffer_check_and_handle_line_ends(self):
299300
else:
300301
self.input_buffer = ""
301302

303+
def _buffer_handle_backspace(self):
304+
if "\x08" in self.input_buffer:
305+
while (pos := self.input_buffer.find("\x08")) > -1:
306+
# strip character before backspace and backspace itself.
307+
self.input_buffer = (
308+
self.input_buffer[: pos - 1] + self.input_buffer[pos + 1 :]
309+
)
310+
302311
def input(self):
303312
"""
304313
Input.
@@ -326,6 +335,7 @@ def update(self):
326335
while available:
327336
raw = self.serial.read(available)
328337
self.input_buffer += raw.decode(self.encoding)
338+
self._buffer_handle_backspace()
329339
if self.echo:
330340
# self.serial.write(raw)
331341
self.echo_print()

0 commit comments

Comments
 (0)