-
Notifications
You must be signed in to change notification settings - Fork 19
/
curses_scrolling.py
122 lines (100 loc) · 3.52 KB
/
curses_scrolling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
"""
Lyle Scott, III
lyle@digitalfoo.net
A simple demo that uses curses to scroll the terminal.
"""
import curses
import sys
import random
import time
class MenuDemo:
DOWN = 1
UP = -1
SPACE_KEY = 32
ESC_KEY = 27
PREFIX_SELECTED = '_X_'
PREFIX_DESELECTED = '___'
outputLines = []
screen = None
def __init__(self):
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
self.screen.keypad(1)
self.screen.border(0)
self.topLineNum = 0
self.highlightLineNum = 0
self.markedLineNums = []
self.getOutputLines()
self.run()
def run(self):
while True:
self.displayScreen()
# get user command
c = self.screen.getch()
if c == curses.KEY_UP:
self.updown(self.UP)
elif c == curses.KEY_DOWN:
self.updown(self.DOWN)
elif c == self.SPACE_KEY:
self.markLine()
elif c == self.ESC_KEY:
self.exit()
def markLine(self):
linenum = self.topLineNum + self.highlightLineNum
if linenum in self.markedLineNums:
self.markedLineNums.remove(linenum)
else:
self.markedLineNums.append(linenum)
def getOutputLines(self):
### !!!
### This is where you would write a function to parse lines into rows
### and columns. For this demo, I'll just create a bunch of random ints
### !!!
self.outputLines = [x.strip() for x in open('lines.txt').readlines()]
self.nOutputLines = len(self.outputLines)
def displayScreen(self):
# clear screen
self.screen.erase()
# now paint the rows
top = self.topLineNum
bottom = self.topLineNum+curses.LINES
for (index,line,) in enumerate(self.outputLines[top:bottom]):
linenum = self.topLineNum + index
if linenum in self.markedLineNums:
prefix = self.PREFIX_SELECTED
else:
prefix = self.PREFIX_DESELECTED
line = '%s %s' % (prefix, line,)
# highlight current line
if index != self.highlightLineNum:
self.screen.addstr(index, 0, line)
else:
self.screen.addstr(index, 0, line, curses.A_BOLD)
self.screen.refresh()
# move highlight up/down one line
def updown(self, increment):
nextLineNum = self.highlightLineNum + increment
# paging
if increment == self.UP and self.highlightLineNum == 0 and self.topLineNum != 0:
self.topLineNum += self.UP
return
elif increment == self.DOWN and nextLineNum == curses.LINES and (self.topLineNum+curses.LINES) != self.nOutputLines:
self.topLineNum += self.DOWN
return
# scroll highlight line
if increment == self.UP and (self.topLineNum != 0 or self.highlightLineNum != 0):
self.highlightLineNum = nextLineNum
elif increment == self.DOWN and (self.topLineNum+self.highlightLineNum+1) != self.nOutputLines and self.highlightLineNum != curses.LINES:
self.highlightLineNum = nextLineNum
def restoreScreen(self):
curses.initscr()
curses.nocbreak()
curses.echo()
curses.endwin()
# catch any weird termination situations
def __del__(self):
self.restoreScreen()
if __name__ == '__main__':
ih = MenuDemo()