From dd63008a9de64908a7353401c52fcc3de342db85 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 24 Oct 2022 17:34:38 +0000 Subject: [PATCH] term.py: add `noecho` init arg, `reset` method --- mmgen/data/version | 2 +- mmgen/term.py | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/mmgen/data/version b/mmgen/data/version index 271aef66..07724dec 100644 --- a/mmgen/data/version +++ b/mmgen/data/version @@ -1 +1 @@ -13.3.dev8 +13.3.dev9 diff --git a/mmgen/term.py b/mmgen/term.py index 0ff4e5cb..050aeda3 100755 --- a/mmgen/term.py +++ b/mmgen/term.py @@ -42,7 +42,11 @@ class MMGenTerm(object): tdim = namedtuple('terminal_dimensions',['width','height']) @classmethod - def init(cls): + def init(cls,noecho=False): + pass + + @classmethod + def reset(cls): pass @classmethod @@ -52,8 +56,19 @@ class MMGenTerm(object): class MMGenTermLinux(MMGenTerm): @classmethod - def init(cls): + def reset(cls): + termios.tcsetattr( cls.stdin_fd, termios.TCSANOW, cls.orig_term ) + cls.old_term = cls.orig_term + + @classmethod + def init(cls,noecho=False): cls.stdin_fd = sys.stdin.fileno() + if not hasattr(cls,'orig_term'): + cls.orig_term = termios.tcgetattr(cls.stdin_fd) + if noecho: # don’t echo input characters + t = termios.tcgetattr(cls.stdin_fd) + t[3] &= ~(termios.ECHO | termios.ECHONL) + termios.tcsetattr( cls.stdin_fd, termios.TCSANOW, t ) cls.old_term = termios.tcgetattr(cls.stdin_fd) @classmethod @@ -119,9 +134,13 @@ class MMGenTermLinux(MMGenTerm): class MMGenTermLinuxStub(MMGenTermLinux): @classmethod - def init(cls): + def init(cls,noecho=False): cls.stdin_fd = sys.stdin.fileno() + @classmethod + def reset(cls): + pass + @classmethod def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_chars=None): msg_r(prompt) @@ -217,15 +236,21 @@ class MMGenTermMSWinStub(MMGenTermMSWin): get_char_raw = get_char -def init_term(): - - term = { +def get_term(): + return { 'linux': (MMGenTermLinux if sys.stdin.isatty() else MMGenTermLinuxStub), 'mswin': (MMGenTermMSWin if sys.stdin.isatty() else MMGenTermMSWinStub), }[_platform] - term.init() +def init_term(noecho=False): + + term = get_term() + + term.init(noecho=noecho) import mmgen.term as self for var in ('get_char','get_char_raw','kb_hold_protect','get_terminal_size'): setattr( self, var, getattr(term,var) ) + +def reset_term(): + get_term().reset()