Skip to content

Commit 43f166e

Browse files
Merge pull request #46 from Kristinita/SashaGoddess
[Refactor] PEP8 compatibility
2 parents 09661b3 + bd09d00 commit 43f166e

1 file changed

Lines changed: 95 additions & 86 deletions

File tree

pyfancy/pyfancy.py

Lines changed: 95 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Provides methods for manipulating text styling in specific terminals.
22
# Uses a basic chaining method where text properties are added by calling
33
# methods with related names.
4-
#
4+
#
55
# For example, to print "Hello, world!" in red:
66
# print pyfancy().red("Hello, world!").get()
77
#
@@ -16,99 +16,104 @@
1616
# direct access to the string object called "out". However, accessing this
1717
# object will not reset the style, so any text outputted after will have
1818
# the same style as whatever the text was at the end of the chain.
19-
#
19+
#
2020
# The get() method is better for accessing text because it resets the text
2121
# style so no new text will have unwanted styling.
2222

23+
2324
class pyfancy:
25+
2426
def __str__(self): return self.get()
25-
def __init__(self, parseText="", obj=""):
27+
28+
def __init__(self, parseText='', obj=''):
2629
# Stores output text, for reset use get()
2730
self.out = str(obj)
2831
self.parseText = str(parseText)
29-
if (self.parseText != ""):
32+
if (self.parseText != ''):
3033
self.parse(self.parseText)
3134

32-
codes = { # The different escape codes
33-
'raw': 0,
34-
'bold': 1,
35-
'dim': 2,
36-
'underlined': 4,
37-
'blinking': 5,
38-
'inverted': 7,
39-
'hidden': 8,
40-
'black': 30,
41-
'red': 31,
42-
'green': 32,
43-
'yellow': 33,
44-
'blue': 34,
45-
'magenta': 35,
46-
'cyan': 36,
47-
'light_gray': 37,
48-
'black_bg': 40,
49-
'red_bg': 41,
50-
'green_bg': 42,
51-
'yellow_bg': 43,
52-
'blue_bg': 44,
53-
'purple_bg': 45,
54-
'cyan_bg': 46,
55-
'gray_bg': 47,
56-
'dark_gray': 90,
57-
'light_red': 91,
58-
'light_green': 92,
59-
'light_yellow': 93,
60-
'light_blue': 94,
61-
'light_magenta': 95,
62-
'light_cyan': 96,
63-
'white': 97,
64-
'dark_gray_bg': 100,
65-
'light_red_bg': 101,
66-
'light_green_bg': 102,
35+
codes = { # The different escape codes
36+
'raw': 0,
37+
'bold': 1,
38+
'dim': 2,
39+
'underlined': 4,
40+
'blinking': 5,
41+
'inverted': 7,
42+
'hidden': 8,
43+
'black': 30,
44+
'red': 31,
45+
'green': 32,
46+
'yellow': 33,
47+
'blue': 34,
48+
'magenta': 35,
49+
'cyan': 36,
50+
'light_gray': 37,
51+
'black_bg': 40,
52+
'red_bg': 41,
53+
'green_bg': 42,
54+
'yellow_bg': 43,
55+
'blue_bg': 44,
56+
'purple_bg': 45,
57+
'cyan_bg': 46,
58+
'gray_bg': 47,
59+
'dark_gray': 90,
60+
'light_red': 91,
61+
'light_green': 92,
62+
'light_yellow': 93,
63+
'light_blue': 94,
64+
'light_magenta': 95,
65+
'light_cyan': 96,
66+
'white': 97,
67+
'dark_gray_bg': 100,
68+
'light_red_bg': 101,
69+
'light_green_bg': 102,
6770
'light_yellow_bg': 103,
68-
'light_blue_bg': 104,
71+
'light_blue_bg': 104,
6972
'light_purple_bg': 105,
70-
'light_cyan_bg': 106,
71-
'white_bg': 107
73+
'light_cyan_bg': 106,
74+
'white_bg': 107
7275
}
7376

7477
# Stores output text, for reset use get()
75-
out = ""
78+
out = ''
7679

7780
# Returns output text and resets properties
7881
def get(self):
79-
return self.out + "\033[0m"
82+
return self.out + '\033[0m'
8083

8184
# Outputs text using print (should work in Python 2 and 3)
8285
def output(self):
8386
print(self.get())
8487

8588
# Adds new text without changing the styling
86-
def add(self,addition):
87-
self.out += addition;
89+
def add(self, addition):
90+
self.out += addition
8891
return self
89-
90-
def read(self,file):
92+
93+
def read(self, file):
9194
f = open(file, 'r')
9295
self.out += f.read()
9396
f.close()
9497
return self
95-
98+
9699
def reset(self):
97-
self.out = ""
100+
self.out = ''
98101
return self
99-
100-
#Alternate between all the colours of the rainbow
101-
#No orange, replaced with lightRed
102-
#No purple/violet so I ignored it
103-
def rainbow(self,addition=""):
102+
103+
# Alternate between all the colours of the rainbow
104+
# No orange, replaced with lightRed
105+
# No purple/violet so I ignored it
106+
def rainbow(self, addition=''):
104107
x = 0
105-
for i in range(len(addition)):
106-
if (addition[i] in [" ", "\t", "\n", "\r"]): x+=1
107-
[self.red, self.light_red, self.yellow, self.green, self.light_blue, self.blue][(i-x) % 6](addition[i])
108+
for i in range(len(addition)):
109+
if (addition[i] in [' ', '\t', '\n', '\r']):
110+
x += 1
111+
[self.red, self.light_red, self.yellow, self.green,
112+
self.light_blue, self.blue][(i - x) % 6](addition[i])
108113
return self
109-
114+
110115
def strip(self):
111-
text = ""
116+
text = ''
112117
i = 0
113118
while i < len(self.out):
114119
if self.out[i] == '\033':
@@ -125,37 +130,37 @@ def strip(self):
125130
text += self.out[i]
126131
i += 1
127132
return text
128-
133+
129134
# Simply apply the attribute with the given name
130-
def attr(self,name):
135+
def attr(self, name):
131136
if name in self.codes:
132-
self.out += "\033[%dm" % self.codes[name]
137+
self.out += '\033[%dm' % self.codes[name]
133138

134139
# Parses text and automatically assigns attributes
135140
# Attributes are specified through brackets
136141
# For example, .parse("{red Hello}") is the same as .red("Hello")
137142
# Multiple attributes can be specified by commas, eg {red,bold Hello}
138143
# Brackets can be nested, eg {red Hello, {bold world}!}
139-
# Brackets can be escaped with backslashes
140-
def parse(self,text):
141-
i = 0 # Current index
142-
props = [] # Property stack; required for nested brackets
144+
# Brackets can be escaped with backslashes
145+
def parse(self, text):
146+
i = 0 # Current index
147+
props = [] # Property stack; required for nested brackets
143148
while i < len(text):
144149
c = text[i]
145-
if c == '\\': # Escape character
150+
if c == '\\': # Escape character
146151
i += 1
147152
if i < len(text):
148153
self.out += text[i]
149-
elif c == '{': # New property list
150-
prop = '' # Current property
154+
elif c == '{': # New property list
155+
prop = '' # Current property
151156
i += 1
152-
curprops = [] # Properties that are part of this bracket
157+
curprops = [] # Properties that are part of this bracket
153158
while text[i] != ' ':
154159
if i + 1 == len(text):
155160
return self
156161
if text[i] == ',':
157162
# Properties separated by commas
158-
self.attr(prop);
163+
self.attr(prop)
159164
curprops.append(prop)
160165
prop = ''
161166
i += 1
@@ -179,26 +184,30 @@ def parse(self,text):
179184
self.out += c
180185
i += 1
181186
return self
182-
183187

184188
# Multicolored text
185-
def multi(self,string):
186-
i = 31 # ID of escape code; starts at 31 (red) and goes to 36 (cyan)
187-
for c in string: # Iterate through string
188-
self.out += "\033[" + str(i) + "m" + c
189-
i += 1 # Why u no have ++i? >:(
190-
if(i > 36): i = 31
189+
def multi(self, string):
190+
i = 31 # ID of escape code; starts at 31 (red) and goes to 36 (cyan)
191+
for c in string: # Iterate through string
192+
self.out += '\033[' + str(i) + 'm' + c
193+
i += 1 # Why u no have ++i? >:(
194+
if(i > 36):
195+
i = 31
191196
return self
192197

193-
# Adds a formatting function to pyfancy with the specified name and formatting code
198+
# Adds a formatting function to pyfancy with the specified name
199+
# and formatting code
194200
# This shouldn't be exported
195-
def _add(name,number):
196-
def inner(self, addition = ""):
197-
self.out += "\033[%dm%s" % (number, addition)
201+
202+
203+
def _add(name, number):
204+
def inner(self, addition=''):
205+
self.out += '\033[%dm%s' % (number, addition)
198206
return self
199-
setattr(pyfancy,name,inner)
207+
setattr(pyfancy, name, inner)
208+
200209

201210
# Generate all default color / format codes
202211
for item in pyfancy.codes.items():
203-
if len(item) > 1: # Just in case
204-
_add(item[0],item[1])
212+
if len(item) > 1: # Just in case
213+
_add(item[0], item[1])

0 commit comments

Comments
 (0)