Advertisement
Guest User

Untitled

a guest
Aug 21st, 2013
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import contextlib
  2. import sys
  3. import termios
  4. import tty
  5.  
  6. @contextlib.contextmanager
  7. def raw():
  8.     fd = sys.stdin.fileno()
  9.     stash = termios.tcgetattr(fd)
  10.     tty.setraw(fd)
  11.     yield
  12.     termios.tcsetattr(fd, termios.TCSANOW, stash)
  13.  
  14. def lineify(src):
  15.     merged = ''
  16.     for line in src:
  17.         if line.startswith('\x1b[A'):
  18.             merged += line
  19.         else:
  20.             if merged:
  21.                 yield merged
  22.             merged = line
  23.     yield merged.rstrip('\n') + '\n'
  24.    
  25. with raw():
  26.     HEIGHT = 23
  27.     with open(sys.argv[1], 'rb') as f:
  28.         lines = [line.decode('cp437') for line in lineify(f)]
  29.     top = 0
  30.     while True:
  31.         print('\n'*25)
  32.         print(''.join(lines[top:top+HEIGHT]))
  33.         print('{}-{} of {}'.format(top, top+HEIGHT, len(lines)))
  34.         key = sys.stdin.read(1)
  35.         if key == 'n':
  36.             top += 1
  37.         elif key == 'p':
  38.             top -= 1
  39.         elif key == ' ':
  40.             top += HEIGHT
  41.         elif key == 'q':
  42.             break
  43.         top = max(0, min(len(lines)-HEIGHT, top))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement