Commits

anatoly techtonik  committed 4dc1e97

Update pager to 1.3 to fix key problems on Windows.

  • Participants
  • Parent commits c90e54d

Comments (0)

Files changed (1)

 License: Public Domain (use MIT if Public Domain doesn't work for you)
 """
 
-__version__ = '1.2'
+__version__ = '1.3'
 
 import os,sys
 
+WINDOWS = os.name == 'nt'
+PY3K = sys.version_info >= (3,)
+
 # Windows constants
 # http://msdn.microsoft.com/en-us/library/ms683231%28v=VS.85%29.aspx
 
 STD_OUTPUT_HANDLE = -11
 STD_ERROR_HANDLE  = -12
 
-if os.name == 'nt':
+
+if WINDOWS:
     # get console handle
     from ctypes import windll, Structure, byref
     try:
     *nix part uses termios ioctl TIOCGWINSZ call.
     """
     width = None
-    if os.name == 'nt':
+    if WINDOWS:
         return _windows_get_window_size()[0]
     elif os.name == 'posix':
         return _posix_get_window_size()[0]
     *nix part uses termios ioctl TIOCGWINSZ call.
     """
     height = None
-    if os.name == 'nt':
+    if WINDOWS:
         return _windows_get_window_size()[1]
     elif os.name == 'posix':
         return _posix_get_window_size()[1]
 
 # --- getch() constants and input logic ---
 
-ENTER = '\n'
-ESC = '\x1B'
-LEFT = ['\x1b', '[', 'D']
-RIGHT = ['\x1b', '[', 'C']
-UP = ['\x1b', '[', 'A']
-DOWN = ['\x1b', '[', 'B']
+if WINDOWS:
+    ENTER = '\x0d'
+    LEFT =  ['\xe0', 'K']
+    UP =    ['\xe0', 'H']
+    RIGHT = ['\xe0', 'M']
+    DOWN =  ['\xe0', 'P']
+else:
+    ENTER = '\n'
+    LEFT =  ['\x1b', '[', 'D']
+    UP =    ['\x1b', '[', 'A']
+    RIGHT = ['\x1b', '[', 'C']
+    DOWN =  ['\x1b', '[', 'B']
+ESC = '\x1b'
 
 def dumpkey(key):
     """
     def hex3fy(key):
         """Helper to convert string into hex string (Python 3 compatible)"""
         from binascii import hexlify
-        py3k = sys.version_info > (3,)
         # Python 3 strings are no longer binary, encode them for hexlify()
-        if py3k:
+        if PY3K:
            key = key.encode('utf-8')
         keyhex = hexlify(key).upper()
-        if py3k:
+        if PY3K:
            keyhex = keyhex.decode('utf-8')
         return keyhex
     if type(key) == str:
     ch = None
     morech = []
     try:
-        import msvcrt
-        ch = msvcrt.getch()
-        # [ ] TODO - test this on Windows
-        while msvcrt.kbhit():
-            morech.append(msvcrt.getch())
+        if PY3K:
+            from msvcrt import kbhit, getwch as _getch
+        else:
+            from msvcrt import kbhit, getch as _getch
+        ch = _getch()
+        # [ ] deal with buffered output - return when a key is
+        #     recognized or scan code exceeds max len
+        while kbhit():
+            morech.append(_getch())
     except ImportError:
         ''' we're not on Windows, so we try the Unix-like approach '''
         import sys, tty, termios
                 linelist = ['']
             lines2print = min(len(linelist), linesleft)
             for i in range(lines2print):
-                if os.name == 'nt' and len(line) == width:
+                if WINDOWS and len(line) == width:
                     # avoid extra blank line by skipping linefeed print
                     echo(linelist[i])
                 else:
 
 # --- Manual tests when pager executed as a module ---
 
-def manual_test_console():
+def _manual_test_console():
     print("\nconsole size: width %s, height %s" % (getwidth(), getheight()))
     echo("--<enter>--")
     getch()
     print("")
 
 
-def manual_test_getch():
+def _manual_test_getch():
     echo("\n")
     # special keys
     keys = 'ENTER LEFT UP RIGHT DOWN ESC'.split()
       print("0. Exit")
       ch = getch()
       if ch == '1':
-        manual_test_console()
+        _manual_test_console()
       elif ch == '2':
-        manual_test_getch()
+        _manual_test_getch()