I'm trying to make a python script that takes input from stdin, displays GUI in terminal using curses and then when user finishes interaction outputs the result to the stdout. Good example of this behaviour is selecta but it's written in ruby.
I can't make curses display anything. This is minimal (it only displays one character and waits for one character) example of what I tried so far:
import os, sys
import curses
c = None
old_out = sys.__stdout__
old_in = sys.__stdin__
old_err = sys.__stderr__
sys.__stdout__ = sys.stdout = open('/dev/tty', 'w')
sys.__stdin__ = sys.stdin = open('/dev/tty')
sys.__stderr__ = sys.stderr = open('/dev/tty')
def show_a(s):
global c
s.addch(ord('a'))
c = s.getch()
curses.wrapper(show_a)
sys.stdin.flush()
sys.stdout.flush()
sys.stderr.flush()
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()
sys.__stdout__ = sys.stdout = old_out
sys.__stdin__ = sys.stdin = old_in
sys.__stderr__ = sys.stderr = old_err
print(c)
When I try to use echo $(python3 show_a.py)
nothing is displayed but after pressing any key its number is displayed:
Is something like this even possible using curses, if so how to do this?