Setup
I've been playing with a script to pipe tail -f
into and highlight certain keywords. Not a big project but something I find useful.
Right now the main loop is basically:
...
line = True
while line:
line = sys.stdin.readline()
sys.stdout.write(highlight(line))
...
I'd like to listen for certain keypresses during this loop to print a marker line in the log. The method I've found that looks like it would work for getting a keypress is described on http://code.activestate.com/recipes/134892/ but it reads a single character at a time from stdin which won't work when my main loop is reading entire lines from it.
Question
Is there a way in Python to listen for keypresses while also reading piped input?
I have wrapped the main function in a try
block which excepts KeyboardInterrupt
and prints a nice little goodbye message instead of an error stack trace. Is there a way to piggyback on this behavior with another key?
I'd rather not use a beefy (compared to my small script) module like pygame or tkinter and be forced to use their main loop just to get access to keypresses. (also I'm not familiar with how either would behave when receiving piped input)