jquast / blessed

Blessed is an easy, practical library for making python terminal apps

Home Page:http://pypi.python.org/pypi/blessed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handling suspend (SIGTSTP) in Linux

jaggzh opened this issue · comments

How do we handle resetting cbreak and whatnot upon suspend and resume?
I'm using:

signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTSTP, handle_suspend)
signal.signal(signal.SIGCONT, handle_continue)
...
with term.fullscreen(), term.cbreak(), term.hidden_cursor():
      while True:

And tried various methods in the handlers, but nothing works. I end up in a line-buffered mode upon returning from shell (bash).

My current attempt is:

def get_termset(): return termios.tcgetattr(sys.stdin)
def set_termset(s): termios.tcsetattr(sys.stdin, termios.TCSADRAIN, s)
def store_termset(): termset_storage = get_termset()
def reset_termset(): set_termset(termset_storage)

termset_storage = termios.tcgetattr(sys.stdin)

def handle_exit(signum, frame):
    # with term.location(0, term.height - 1):
    #     print(term.normal + "Restoring terminal...")
    exit(0)

def handle_suspend(signum, frame):
    store_termset()
    with term.location(0, term.height - 1):
        print(term.cnorm + "Suspending...")
    signal.signal(signal.SIGTSTP, signal.SIG_DFL)
    signal.raise_signal(signal.SIGTSTP)
    signal.signal(signal.SIGTSTP, handle_suspend)

def handle_continue(signum, frame):
    with term.location(0, term.height - 1):
        print("Resuming...")
    reset_termset()

But "still no dice", as the saying goes.

My guess is because you're using termset_storage as a global but you didn't use the global statement.
Does this work?

def store_termset():
    global termset_storage
    termset_storage = get_termset()

I'd recommend moving those functions and termset_storage to a class. Then create an instance and reference the methods for signal.signal().

@jaggzh Did adding global resolve your issue?