Skip to content

Commit 6a6a257

Browse files
committed
tweak echo handling
1 parent ccee5d4 commit 6a6a257

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

examples/nonblocking_serialinput_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def main():
3838
my_input.print(42 * "*")
3939

4040
runtime_print_next = time.monotonic()
41-
runtime_print_intervall = 5.0
41+
runtime_print_intervall = 1.0
4242
running = True
4343
while running:
4444
# input handling
@@ -57,7 +57,7 @@ def main():
5757
# live sign
5858
if runtime_print_next < time.monotonic():
5959
runtime_print_next = time.monotonic() + runtime_print_intervall
60-
my_input.print("{: > 7.2f}s".format(time.monotonic()))
60+
my_input.print("runtime: {: > 7.2f}s".format(time.monotonic()))
6161
led.value = not led.value
6262

6363

nonblocking_serialinput.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def statusline_print(self):
163163
move = ""
164164
# earease line
165165
move += terminal.ANSIControl.cursor.previous_line(1)
166-
move += terminal.ANSIControl.erase_line(0)
166+
move += terminal.ANSIControl.erase_line(2)
167167

168168
# reprint echo
169169
line = self._get_statusline()
@@ -196,7 +196,7 @@ def echo_print(self):
196196
# # eareas
197197
# move += terminal.ANSIControl.cursor.previous_line(line_count)
198198
# move += terminal.ANSIControl.cursor.previous_line(0)
199-
move += terminal.ANSIControl.erase_line(0)
199+
move += terminal.ANSIControl.erase_line(2)
200200

201201
# reprint line
202202
line = self._get_echo_line()
@@ -219,7 +219,7 @@ def echo_print(self):
219219
moveback=moveback,
220220
)
221221
)
222-
print("\n\n\n\n\n\n{}\n\n\n\n\n\n".format(repr(text)))
222+
print("\n\n\n{}\n\n\n".format(repr(text)))
223223
# execute all the things ;-)
224224
print(text, end="")
225225

@@ -242,21 +242,29 @@ def print(self, *args):
242242
if self.statusline:
243243
# earease statusline
244244
move += terminal.ANSIControl.cursor.previous_line(1)
245-
move += terminal.ANSIControl.erase_line(0)
245+
move += terminal.ANSIControl.erase_line(2)
246246
if self.echo:
247247
# earease echoline
248-
move += terminal.ANSIControl.cursor.previous_line(1)
249-
move += terminal.ANSIControl.erase_line(0)
248+
# move += terminal.ANSIControl.cursor.previous_line(1)
249+
move += terminal.ANSIControl.erase_line(2)
250+
# print("\n\n\n{}\n\n\n".format(repr(move)))
251+
# print(repr(terminal.ANSIControl.cursor.previous_line(1)))
252+
# print(repr(terminal.ANSIControl.erase_line(2)))
250253
print(move, end="")
254+
time.sleep(1)
251255
# *normally print output
252256
print(*args)
253257
# print(*args, end=end)
254258
# print statement is finished.
255259
# now we have to reprint echo & statusline
256260
if self.echo:
257-
print(self._get_echo_line())
261+
print(self._get_echo_line(), end="")
262+
# print(self._get_echo_line())
258263
if self.statusline:
259-
print(self._get_statusline())
264+
print(self._get_statusline(), end="")
265+
# if not self.echo and not self.statusline:
266+
# # add new end
267+
# print()
260268
else:
261269
# print(*args, end)
262270
print(*args)
@@ -308,6 +316,16 @@ def _buffer_handle_backspace(self):
308316
self.input_buffer[: pos - 1] + self.input_buffer[pos + 1 :]
309317
)
310318

319+
def _buffer_handle_cursor_position(self):
320+
# # TODO: implement Cursor position managment
321+
# if "\x08" in self.input_buffer:
322+
# while (pos := self.input_buffer.find("\x08")) > -1:
323+
# # strip character before backspace and backspace itself.
324+
# self.input_buffer = (
325+
# self.input_buffer[: pos - 1] + self.input_buffer[pos + 1 :]
326+
# )
327+
pass
328+
311329
def input(self):
312330
"""
313331
Input.
@@ -334,7 +352,9 @@ def update(self):
334352
available = self.serial.in_waiting
335353
while available:
336354
raw = self.serial.read(available)
337-
self.input_buffer += raw.decode(self.encoding)
355+
text = raw.decode(self.encoding)
356+
# self._buffer_handle_cursor_position()
357+
self.input_buffer += text
338358
self._buffer_handle_backspace()
339359
if self.echo:
340360
# self.serial.write(raw)

0 commit comments

Comments
 (0)