dirtywords

portable text interface framework  https://pypi.python.org/pypi/dirtywords
git clone https://git.ce9e.org/dirtywords.git

commit
a2bf92f1eed6064f1f77c69efcb96a3e575e07d2
parent
fbbffc2f4c1feac7c9c0a0c2c40d168c00051f86
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-09 07:26
implement ANSI escapes in stupid core

Diffstat

M dirtywords/stupid_core.py 47 ++++++++++++++++++++++++++++++++++++++++++++++-

1 files changed, 46 insertions, 1 deletions


diff --git a/dirtywords/stupid_core.py b/dirtywords/stupid_core.py

@@ -8,6 +8,7 @@ cross-platform.
    8     8 """
    9     9 
   10    10 import base
   -1    11 from constants import KEYS
   11    12 
   12    13 
   13    14 class Screen(base.Screen):
@@ -16,7 +17,7 @@ class Screen(base.Screen):
   16    17 
   17    18         self._pressed_keys = {}
   18    19 
   19    -1     def getch(self, blocking=True):
   -1    20     def _getch(self, blocking=True):
   20    21         # http://code.activestate.com/recipes/134892/
   21    22 
   22    23         try:  # windows
@@ -43,6 +44,50 @@ class Screen(base.Screen):
   43    44             finally:
   44    45                 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
   45    46 
   -1    47     def getch(self, blocking=True):
   -1    48         # Convert ANSI escape sequences to key constants
   -1    49         # This is implemented as a wrapper around :py:meth:`_getch` because
   -1    50         # it needs to get a variable number of bytes from stdin.
   -1    51         l = [self._getch()]
   -1    52 
   -1    53         if l == [27]:
   -1    54             # TODO: single ESC is valid, so this should not block
   -1    55             l.append(self._getch())
   -1    56         elif l == [155]:
   -1    57             l = [27, 91]
   -1    58 
   -1    59         if l[0] == 27:
   -1    60             if l[1] == 91:
   -1    61                 l.append(self._getch())
   -1    62                 while l[-1] not in range(64, 127):
   -1    63                     l.append(self._getch())
   -1    64             elif l[-1] in range(64, 96):
   -1    65                 pass
   -1    66             else:
   -1    67                 # invalid?
   -1    68                 pass
   -1    69 
   -1    70         if l == [27, 27]:
   -1    71             return 27
   -1    72         if l == [27, 91, 68]:
   -1    73             return KEYS['Left']
   -1    74         elif l == [27, 91, 67]:
   -1    75             return KEYS['Right']
   -1    76         elif l == [27, 91, 66]:
   -1    77             return KEYS['Down']
   -1    78         elif l == [27, 91, 65]:
   -1    79             return KEYS['Up']
   -1    80         elif l == [27, 91, 54, 126]:
   -1    81             return KEYS['PageDown']
   -1    82         elif l == [27, 91, 53, 126]:
   -1    83             return KEYS['PageUp']
   -1    84         elif l == [27, 91, 51, 126]:
   -1    85             return KEYS['Delete']
   -1    86         elif l == [27, 91, 50, 126]:
   -1    87             return KEYS['Insert']
   -1    88         elif len(l) == 1:
   -1    89             return l[0]
   -1    90 
   46    91     def refresh(self):
   47    92         spacing = '\n' * self.height * 2
   48    93         s = '\n'.join([''.join(row) for row in self.data])