Make the resize work, using an ugly workaround

It seems efficient (more than the curses.endwin(); stdscr.refresh() stuf,
which is slow and really really ugly) and it doesn't break my lovely main
loop, so it’s all good (except that it uses shutil…)
This commit is contained in:
Florent Le Coz 2014-07-31 04:45:02 +02:00
parent 0da967850d
commit 55daf9d49d
2 changed files with 25 additions and 1 deletions

View file

@ -11,6 +11,7 @@ log = logging.getLogger(__name__)
import asyncio
import collections
import shutil
import curses
import os
import pipes
@ -482,6 +483,27 @@ class Core(object):
pass
sys.__excepthook__(typ, value, trace)
def sigwinch_handler(self):
"""A work-around for ncurses resize stuff, which sucks. Normally, ncurses
catches SIGWINCH itself. In its signal handler, it updates the
windows structures (for example the size, etc) and it
ungetch(KEY_RESIZE). That way, the next time we call getch() we know
that a resize occured and we can act on it. BUT poezio doesnt call
getch() until it knows it will return something. The problem is we
cant know that, because stdin is not affected by this KEY_RESIZE
value (it is only inserted in a ncurses internal fifo that we cant
access).
The (ugly) solution is to handle SIGWINCH ourself, trigger the
change of the internal windows sizes stored in ncurses module, using
sizes that we get using shutil, ungetch the KEY_RESIZE value and
then call getch to handle the resize on poezios side properly.
"""
size = shutil.get_terminal_size()
curses.resizeterm(size.lines, size.columns)
curses.ungetch(curses.KEY_RESIZE)
self.on_input_readable()
def on_input_readable(self):
"""
main loop waiting for the user to press a key
@ -562,8 +584,8 @@ class Core(object):
self.do_command(replace_line_breaks(char), False)
else:
self.do_command(''.join(char_list), True)
self.doupdate()
self.xmpp.plugin['xep_0012'].begin_idle(jid=self.xmpp.boundjid)
self.doupdate()
def save_config(self):
"""

View file

@ -61,7 +61,9 @@ def main():
# the asyncio logger will not follow our configuration and won't write
# the tracebacks in the correct file, etc
import asyncio
asyncio.get_event_loop().add_reader(sys.stdin, cocore.on_input_readable)
asyncio.get_event_loop().add_signal_handler(signal.SIGWINCH, cocore.sigwinch_handler)
cocore.xmpp.start()
asyncio.get_event_loop().run_forever()
# We reach this point only when loop.stop() is called