-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextdecorator.py
130 lines (106 loc) · 5.29 KB
/
textdecorator.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
from abc import ABCMeta, abstractmethod
class Decorator():
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, textElement):
self.textElement = textElement
@abstractmethod
def getText(self):
return self.textElement.getText()
@abstractmethod
def getLength(self):
return self.textElement.getLength()
@abstractmethod
def getRawText(self):
return self.textElement.getRawText()
# Renders the underlying text element with a bold font
class BoldText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
def __init__(self, textElement):
self.textElement = textElement
def getText(self):
return "\033[1m" + self.textElement.getText()
# Renders the underlying text element with inverted colors
class InvertedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
def __init__(self, textElement):
self.textElement = textElement
def getText(self):
return "\033[7m" + self.textElement.getText()
# Renders the underlying text element with a blinking font
class BlinkingText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
def __init__(self, textElement):
self.textElement = textElement
def getText(self):
return "\033[5m" + self.textElement.getText()
# Renders the underlying text element with an underlined font
class UnderlinedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
def __init__(self, textElement):
self.textElement = textElement
def getText(self):
return "\033[4m" + self.textElement.getText()
# Renders the underlying text element with a specific foreground color
class ColoredText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
# color must be one of the colors defined in the FontColor enum
def __init__(self, textElement, color):
self.textElement = textElement
self.color = str(color)
def getText(self):
return "\033[" + self.color + "m" + self.textElement.getText()
# Renders the underlying text element with a background color
class HighlightedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
# highlightColor must be one of the colors defined in the FontColor enum
def __init__(self, textElement, highlightColor):
self.textElement = textElement
self.highlightColor = str(highlightColor)
def getText(self):
return "\033[" + self.highlightColor + "m" + self.textElement.getText()
# Not implemented
class AbsolutePositionedText():
pass # TODO
# Renders the underlying text element on the left side of the screen with
# a certain padding on the left
class LeftAlignedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
# padding must be an integer. If a value <= 0 is supplied, 1 is used for the padding
def __init__(self, textElement, padding):
self.textElement = textElement
self.padding = (padding) if (padding >= 0) else 1
def getText(self):
return "\033[" + str(self.padding) + "C" + self.textElement.getText()
# Renders the underlying text element on the right side of the screen with
# a certain padding on the right
class RightAlignedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
# padding must be an integer. If a value <= 0 is supplied, 1 is used for the padding
# columns must be an integer that contains the number of columns in the console window
def __init__(self, textElement, padding, columns):
self.textElement = textElement
# Subtract 2 from the padding because the first character is insterted
# one position after the cursor and the first index of the console is 1.
self.padding = padding if (padding >= 0) else 1
self.columns = columns if (columns > self.padding) else 80
def getText(self):
return "\033[" + str(self.columns - self.textElement.getLength() - self.padding) + "C" + self.textElement.getText()
# Adds spaces to the both sides of the underlying Text object
class PaddedText(Decorator):
# Creates a new instance of this decorator
# textElement must be a Text object and may have any number of decorators applied to it
# padding must be an integer. If a value <= 0 is supplied, 1 is used for the padding
def __init__(self, textElement, padding):
self.textElement = textElement
self.padding = padding if (padding >= 0) else 1
def getText(self):
return (" " * self.padding) + self.textElement.getRawText() + (" " * self.padding)