I want to write something like a terminal version of dmenu, which can be used to search for a file, and then pass the file's location to another program in the pipeline, like:
my_script | xargs vim # search for a file and open in vim
I tried to do it in python with output redirection, but it doesn't seem to work with curses.
import sys
import curses
prev_out = sys.stdout
print("1:", sys.stdout)
sys.stdout = open("/dev/tty", "w")
print("2:", sys.stdout)
window = curses.initscr()
window.addstr(1, 1, "testing")
window.getch()
curses.endwin()
sys.stdout = prev_out
print("3:", sys.stdout)
When I call it like this:
myscript > /dev/pts/1 # redirect output to another tty
print's behave how I'd expect them to (2 in original tty, 1 and 3 in the other one), but the curses UI is displayed in the /dev/pts/1.
So my question is, is there a way to redirect curses output back to /dev/tty, or is there a different way to display a text based GUI, which can be redirected by altering sys.stdout?
I achieved this behaviour by writing a short bash wrapper for my python script.