Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import contextlib
- import sys
- import termios
- import tty
- @contextlib.contextmanager
- def raw():
- fd = sys.stdin.fileno()
- stash = termios.tcgetattr(fd)
- tty.setraw(fd)
- yield
- termios.tcsetattr(fd, termios.TCSANOW, stash)
- def lineify(src):
- merged = ''
- for line in src:
- if line.startswith('\x1b[A'):
- merged += line
- else:
- if merged:
- yield merged
- merged = line
- yield merged.rstrip('\n') + '\n'
- with raw():
- HEIGHT = 23
- with open(sys.argv[1], 'rb') as f:
- lines = [line.decode('cp437') for line in lineify(f)]
- top = 0
- while True:
- print('\n'*25)
- print(''.join(lines[top:top+HEIGHT]))
- print('{}-{} of {}'.format(top, top+HEIGHT, len(lines)))
- key = sys.stdin.read(1)
- if key == 'n':
- top += 1
- elif key == 'p':
- top -= 1
- elif key == ' ':
- top += HEIGHT
- elif key == 'q':
- break
- top = max(0, min(len(lines)-HEIGHT, top))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement