-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurseTextbox.py
119 lines (98 loc) · 4.19 KB
/
curseTextbox.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
import copy
import curses
import math
import textwrap
import string
class CurseTextbox(object):
"""description of class"""
def __init__(self, **kwargs):
self.parent = kwargs["parent"]
self.y = kwargs["size"][0]
self.x = kwargs["size"][1]
self.height = kwargs["size"][2]
self.width = kwargs["size"][3]
if "base_text" in kwargs: self.base_text = kwargs["base_text"]
else: self.base_text = ""
self.line_text = []
self.total_height = 0
self.page_height = 0
self.pages = 0
self.current_pg = 0
self.index = 0
self.more_content = False
self.style = kwargs["style"]
if "center" in kwargs: self.center = kwargs["center"]
else: self.center = False
self.resetText()
def load(self):
if hasattr(self, "_on_load"):
if self._on_load["action"] == "call_function":
func = getattr(self, _on_load["action_name"])
func(*_on_load["action_args"])
def refresh(self, new_text=None):
if new_text != None: self.resetText(new_text)
self.drawText()
self.parent.win.refresh()
def drawText(self):
""" draws textbox text to screen """
txt_atrclr = self.style.txt_atr | self.style.txt_clr
self.parent.win.attron(txt_atrclr)
self.clearText()
for l in range(0, self.height):
if l + self.index >= len(self.line_text): break
self.parent.win.addstr(
self.y + l,
self.x,
self.line_text[self.index + l],
txt_atrclr)
if self.more_content == True:
self.parent.win.addstr(
self.y + self.height,
self.x,
"(MORE...)",
txt_atrclr)
self.parent.win.attroff(txt_atrclr)
self.parent.changed = True
def clearText(self):
""" removes text from textbox on screen """
bg_chtype = self.parent.win.getbkgd()
for l in range(0, self.height):
self.parent.win.hline(self.y + l, self.x, bg_chtype, self.width)
def resetText(self, text=None):
""" resets textbox data to base text or passed text"""
if text == None: text = self.base_text
else: self.base_text = copy.copy(text)
self.index = 0
self.current_pg = 0
self.textToLines()
self.setPages()
self.turnPage("prev")
def textToLines(self):
if self.center == True:
self.line_text = textwrap.wrap(self.base_text, self.width)
for l in range (0, len(self.line_text)):
self.line_text[l] = textwrap.dedent(
self.line_text[l]).center(self.width," ")
else:
wr = textwrap.TextWrapper(
width=self.width,
replace_whitespace=False,
drop_whitespace=False)
self.line_text = wr.wrap(self.base_text)
def setPages(self):
self.total_height = len(self.line_text)
self.pages = self.total_height / self.height
if self.total_height % self.height != 0 : self.pages += 1
if self.pages > 1 : self.more_content = True
def turnPage(self, direction):
if direction == "next":
if self.index + self.height < self.total_height :
self.index += self.height
self.current_pg += 1
if self.current_pg + 1 == self.pages: self.more_content = False
else: self.more_content = True
elif direction == "prev":
if self.index - self.height >= 0:
self.index -= self.height
self.current_pg -= 1
if self.pages > 1: self.more_content = True