From 271545a01f41334ac5a2b39c44a10fd4ce0c8f0f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 11 Sep 2013 12:54:57 +0200 Subject: the text area can take care of terminal settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/editor.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/editor.py b/src/editor.py index 05bc9ef..0a91f14 100644 --- a/src/editor.py +++ b/src/editor.py @@ -97,6 +97,25 @@ class TextArea(): + def initialise(self): + ''' + Initialise terminal and TTY settings + ''' + print('\033[H\033[2J') + self.old_stty = Popen('stty --save'.split(' '), stdout = PIPE).communicate()[0] + self.old_stty = self.old_stty.decode('utf-8', 'error')[:-1] + Popen('stty -icanon -echo -isig -ixon -ixoff'.split(' '), stdout = PIPE).communicate() + + def close(self): + ''' + Restore the terminal to the state before `initialise` as invoked + ''' + print('\033[H\033[2J', end = '') + sys.stdout.flush() + Popen(['stty', self.old_stty], stdout = PIPE).communicate() + + + def get_selection(self, for_display = False): ''' Get the selected texts start and end on the X-axis @@ -651,14 +670,12 @@ class TextArea(): def phonysaver(): return True -print('\033[H\033[2J') -old_stty = Popen('stty --save'.split(' '), stdout = PIPE).communicate()[0] -old_stty = old_stty.decode('utf-8', 'error')[:-1] -Popen('stty -icanon -echo -isig -ixon -ixoff'.split(' '), stdout = PIPE).communicate() +area = None try: - TextArea(('a be se de e eff ge hå i ji kå ell emm enn o pe ku ärr ess te u ve dubbel-ve eks y säta å ä ö').split(' '), {}, 6, 4, 40, 10).run(phonysaver) + area = TextArea(('a be se de e eff ge hå i ji kå ell emm enn o pe ku ärr ess te u ve dubbel-ve eks y säta å ä ö').split(' '), {}, 6, 4, 40, 10) + area.initialise() + area.run(phonysaver) finally: - print('\033[H\033[2J', end = '') - sys.stdout.flush() - Popen(['stty', old_stty], stdout = PIPE).communicate() + if area is not None: + area.close() -- cgit v1.2.3-70-g09d2