Last active
July 19, 2021 06:34
-
-
Save av1d/d4766d63e233b5db5d7e434ff57872a3 to your computer and use it in GitHub Desktop.
Update a list/buffer of typed characters on one line. Shift the list to the left one space when list exceeds desired length. Polls keyboard for input then updates display. Useful for CLI or other interfaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tty | |
import sys | |
import termios | |
# live typed-character buffer | |
# https://github.com/av1d | |
the_list = ["1", "2", "3", "4", "5"] | |
def printList(): | |
os.system('clear') # refresh screen | |
print (''.join(the_list)) # place delimiter char between '' | |
def _getInput(): # get kb input | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(fd) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
printList() # print list for the first time | |
while True: | |
try: | |
dialed = _getInput() # poll for kb input | |
if dialed in ("x", "X"): # if x or X is pressed then exit | |
sys.exit() | |
else: | |
the_list.append(str(dialed)) # add element to list | |
printList() # clear screen & reprint list | |
countList = len(the_list) # count elements | |
if countList >= 21: # if we hit 20 chars then shift list left one place | |
the_list.pop(0) | |
printList() # clear & reprint | |
except: | |
sys.exit(0) # exit on any errors | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment