-
Notifications
You must be signed in to change notification settings - Fork 0
/
pad.py
301 lines (269 loc) · 8.6 KB
/
pad.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# coding=UTF-8
'''
@Author: cenmmy
@LastEditors: cenmmy
@Description:
@Date: 2019-04-13 11:08:09
@LastEditTime: 2019-04-21 20:17:27
'''
import curses
from curses.textpad import rectangle
# const variable
_up = 0
_right = 1
_down = 2
_left = 3
#######################################################################
"""
classname: Pad
description: Abstracting pad objects
"""
class Pad(object):
'''
@description: Initialization function of Pad
@param {stdscr: standard screen
nlines: pad's height,
ncoles: pas's width,
sminrow: display area upper left y,
smincol: dispaly area upper left x,
smaxrow: display area lower right y,
smaxcol: display area lower right x,
fgcolor: foreground color,
bgcolor: background color,
color_pair: color_pair}
@return: void
'''
def __init__(self, stdscr, nlines, ncols, sminrow, smincol, smaxrow, smaxcol, fgcolor, bgcolor, color_pair):
# Parameter initialization
self.stdscr = stdscr
self.nlines = nlines
self.ncols = ncols
self.sminrow= sminrow
self.smincol= smincol
self.smaxrow= smaxrow
self.smaxcol= smaxcol
# Create pad objects
self.pad = curses.newpad(self.nlines, self.ncols)
# upper left corner of the pad region to be displayed
self.pminrow = 0
self.pmincol = 0
# color_pair
self._fgcolor = fgcolor
self._bgcolor = bgcolor
self.color_pair = color_pair
curses.init_pair(self.color_pair, self._fgcolor, self._bgcolor)
'''
@description: specify the part of the pad to be displayed and the location on the screen to be used for the display
@param {void}
@return: void
'''
def refresh(self):
self.pad.refresh(self.pminrow, self.pmincol, self.sminrow, self.smincol, self.smaxrow, self.smaxcol)
'''
@description: Reset the size of pad
@param {nlines: Nlines of the new pad,
ncols: Ncols of the new pad}
@return: void
'''
def resize_pad(self, nlines, ncols):
self.nlines = nlines
self.ncols = ncols
self.pad = curses.newpad(self.nlines, self.ncols)
'''
@description: Create pad borders
@param {void}
@return: void
'''
def border(self):
# Specifies the upper left and lower right corner of the border
_uly = self.sminrow - 1
_ulx = self.smincol - 1
_lry = self.smaxrow + 1
_lrx = self.smaxcol + 1
# Create borders
try:
rectangle(self.stdscr, _uly, _ulx, _lry, _lrx)
except curses.error:
pass
# Refresh stdscr
self.stdscr.refresh()
'''
@description: draw content in the pad
@param {void}
@return: void
'''
def content(self):
pass
'''
@description: Display components on the screen
@param {void}
@return: void
'''
def draw(self):
self.border()
self.content()
def clear(self):
self.pad.clear()
@property
def fgcolor(self):
return self._fgcolor
@fgcolor.setter
def fgcolor(self, value):
self._fgcolor = value
curses.init_pair(self.color_pair, self._fgcolor, self._bgcolor)
@property
def bgcolor(self):
return self._bgcolor
@bgcolor.setter
def bgcolor(self, value):
self._bgcolor = value
curses.init_pair(self.color_pair, self._fgcolor, self._bgcolor)
################################################################################
"""
classname: ScrollablePad
description: inherits from Pad class and realizes the function of scrolling.
"""
class ScrollablePad(Pad):
'''
@description: Initialization function of ScrollablePad
@param {
stdscr: standard screen
nlines: pad's height,
ncoles: pas's width,
sminrow: display area upper left y,
smincol: dispaly area upper left x,
smaxrow: display area lower right y,
smaxcol: display area lower right x
fgcolor: foreground color,
bgcolor: background color,
color_pair: color_pair}
@return: void
'''
def __init__(self, stdscr, nlines, ncols, sminrow, smincol, smaxrow, smaxcol, fgcolor, bgcolor, color_pair):
Pad.__init__(self, stdscr, nlines, ncols, sminrow, smincol, smaxrow, smaxcol, fgcolor, bgcolor, color_pair)
self._direction = _up
self._current_row = 0
self._current_col = 0
'''
@description: Modify the direction of the scroll
@param {direction: Scrolling direction}
@return: void
'''
def modify_direction(self, direction):
self._direction = direction
'''
@description: Invoked when the up key is pressed
@param {lines: Number of rows scrolling up}
@return: void
'''
def onkeyup(self, lines=1):
self.modify_direction(_up)
self.current_row = self.current_row - lines
self.modify_pminrow()
'''
@description: Invoked when the right key is pressed
@param {lines: Number of cols scrolling up}
@return: void
'''
def onkeyright(self, cols=1):
self.modify_direction(_right)
self.current_col = self.current_col + cols
self.modify_pmincol()
'''
@description: Invoked when the down key is pressed
@param {lines: Number of rows scrolling down}
@return: void
'''
def onkeydown(self, lines=1):
self.modify_direction(_down)
self.current_row = self.current_row + lines
self.modify_pminrow()
'''
@description: Invoked when the left key is pressed
@param {lines: Number of cols scrolling up}
@return: void
'''
def onkeyleft(self, cols=1):
self.modify_direction(_left)
self.current_col = self.current_col - cols
self.modify_pmincol()
'''
@description: modify the pminrow
@param {type}
@return:
'''
def modify_pminrow(self, delete=False):
if delete:
if self.current_row > self.smaxrow - self.sminrow:
self.pminrow = self.current_row - self.smaxrow + self.sminrow
else:
self.pminrow = 0
else:
if self.current_row > self.pminrow + self.smaxrow - self.sminrow and self._direction == _down:
self.pminrow = self.current_row - self.smaxrow + self.sminrow
elif self.current_row < self.pminrow and self._direction == _up:
self.pminrow = self.current_row
'''
@description: modify the pminrow
@param {type}
@return:
'''
def modify_pmincol(self, delete=False):
if delete:
if self.current_col > self.smaxcol - self.smincol:
self.pmincol = self.current_col - self.smaxcol + self.smincol
else:
self.pmincol = 0
else:
if self.current_col > self.pmincol + self.smaxcol - self.smincol and self._direction == _right:
self.pmincol = self.current_col - self.smaxcol + self.smincol
elif self.current_col < self.pmincol and self._direction == _left:
self.pmincol = self.current_col
'''
@description: set style of current row
@param {style: the style of current row}
@return: void
'''
def current_row_style(self, style):
self.pad.chgat(self.current_row, 0, -1, style)
'''
@description: Scroll to the bottom of the list
@param {void}
@return: void
'''
def move2bottom(self):
self.current_row = self.nlines - 1
self.modify_direction(_down)
self.modify_pminrow()
'''
@description: Scroll to the top of the list
@param {void}
@return: void
'''
def move2top(self):
self.current_row = 0
self.modify_direction(_up)
self.modify_pminrow()
def move2left(self):
self.current_col = 0
self.modify_direction(_left)
self.modify_pmincol()
def move2right(self):
self.current_col = self.ncols - 1
self.modify_direction(_right)
self.modify_pmincol()
@property
def current_row(self):
return self._current_row
@current_row.setter
def current_row(self, value):
if not (value < 0 or value >= self.nlines):
self._current_row = value
@property
def current_col(self):
return self._current_col
@current_col.setter
def current_col(self, value):
if not (value < 0 or value >= self.ncols):
self._current_col = value