better handling of terminal in hidconsole

This commit is contained in:
Daniel Pavel 2012-12-07 14:40:48 +02:00
parent 7ada4af31b
commit 37daf3a192
1 changed files with 20 additions and 8 deletions

View File

@ -10,29 +10,38 @@ strhex = lambda d: hexlify(d).decode('ascii').upper()
interactive = os.isatty(0) interactive = os.isatty(0)
start_time = 0 start_time = 0
try: try: # python3 support
read_packet = raw_input read_packet = raw_input
except: except:
read_packet = input read_packet = input
from threading import Lock
print_lock = Lock()
def _print(marker, data, scroll=False): def _print(marker, data, scroll=False):
t = time.time() - start_time t = time.time() - start_time
if interactive and scroll:
sys.stdout.write('\033[s')
sys.stdout.write('\033[S') # scroll up
sys.stdout.write('\033[A\033[L\033[G') # insert new line above the current one, position on first column
hexs = strhex(data) hexs = strhex(data)
s = '%s (% 8.3f) [%s %s %s %s] %s' % (marker, t, hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data)) s = '%s (% 8.3f) [%s %s %s %s] %s' % (marker, t, hexs[0:2], hexs[2:4], hexs[4:8], hexs[8:], repr(data))
sys.stdout.write(s)
print_lock.acquire()
if interactive and scroll: if interactive and scroll:
# scroll the entire screen above the current line up by 1 line
sys.stdout.write('\033[s' # save cursor position
'\033[S' # scroll up
'\033[A' # cursor up
'\033[L' # insert 1 line
'\033[G') # move cursor to column 1
sys.stdout.write(s)
if interactive and scroll:
# restore cursor position
sys.stdout.write('\033[u') sys.stdout.write('\033[u')
else: else:
sys.stdout.write('\n') sys.stdout.write('\n')
print_lock.release()
def _continuous_read(handle, timeout=2000): def _continuous_read(handle, timeout=2000):
while True: while True:
@ -82,6 +91,9 @@ if __name__ == '__main__':
t.start() t.start()
prompt = '?? Input: ' if interactive else '' prompt = '?? Input: ' if interactive else ''
if interactive:
# move the cursor at the bottom of the screen
sys.stdout.write('\033[300B') # move cusor at most 300 lines down, don't scroll
while t.is_alive(): while t.is_alive():
line = read_packet(prompt).strip().replace(' ', '') line = read_packet(prompt).strip().replace(' ', '')