Skip to content

Commit f8a63a1

Browse files
committed
Refactored the console color code a little, tweaked the colors to my liking
1 parent f53fae6 commit f8a63a1

2 files changed

Lines changed: 88 additions & 46 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Copyright (c) 2010 Jonathan Hartley <[email protected]>
2+
3+
Released under the New BSD license (reproduced below), or alternatively you may
4+
use this software under any OSI approved open source license such as those at
5+
http://opensource.org/licenses/alphabetical
6+
7+
All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without
10+
modification, are permitted provided that the following conditions are met:
11+
12+
* Redistributions of source code must retain the above copyright notice, this
13+
list of conditions and the following disclaimer.
14+
15+
* Redistributions in binary form must reproduce the above copyright notice,
16+
this list of conditions and the following disclaimer in the documentation
17+
and/or other materials provided with the distribution.
18+
19+
* Neither the name(s) of the copyright holders, nor those of its contributors
20+
may be used to endorse or promote products derived from this software without
21+
specific prior written permission.
22+
23+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+

python 2/runner/sensei.py

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import helper
88
from mockable_test_result import MockableTestResult
99

10-
from libs.colorama import init, Fore
10+
from libs.colorama import init, Fore, Style
1111
init()
1212

1313

@@ -25,13 +25,14 @@ def startTest(self, test):
2525
self.prevTestClassName = helper.cls_name(test)
2626
if not self.failures:
2727
self.stream.writeln()
28-
self.stream.writeln(Fore.RESET + "Thinking {0}".format(helper.cls_name(test)))
28+
self.stream.writeln("{0}{1}Thinking {2}".format(
29+
Fore.RESET, Style.NORMAL, helper.cls_name(test)))
2930

3031
def addSuccess(self, test):
3132
if self.passesCount():
3233
MockableTestResult.addSuccess(self, test)
33-
self.stream.writeln(Fore.GREEN + " {0} has expanded your"
34-
"awareness.".format(test._testMethodName))
34+
self.stream.writeln(" {0}{1}{2} has expanded your awareness." \
35+
.format(Fore.GREEN, Style.BRIGHT, test._testMethodName))
3536
self.pass_count += 1
3637

3738
def addError(self, test, err):
@@ -40,7 +41,8 @@ def addError(self, test, err):
4041
self.addFailure(test, err)
4142

4243
def passesCount(self):
43-
return not (self.failures and helper.cls_name(self.failures[0][0]) != self.prevTestClassName)
44+
return not (self.failures and helper.cls_name(self.failures[0][0]) !=
45+
self.prevTestClassName)
4446

4547
def addFailure(self, test, err):
4648
MockableTestResult.addFailure(self, test, err)
@@ -77,25 +79,30 @@ def learn(self):
7779
self.stream.writeln(self.say_something_zenlike())
7880

7981
if self.failures: return
80-
self.stream.writeln("\n**************************************************")
81-
self.stream.writeln("That was the last one, well done!")
82-
self.stream.writeln("\nIf you want more, take a look at about_extra_credit_task.py")
82+
self.stream.writeln(
83+
"\n{0}**************************************************" \
84+
.format(Fore.RESET))
85+
self.stream.writeln("\n{0}That was the last one, well done!" \
86+
.format(Fore.MAGENTA))
87+
self.stream.writeln(
88+
"\nIf you want more, take a look at about_extra_credit_task.py")
8389

8490
def errorReport(self):
8591
problem = self.firstFailure()
8692
if not problem: return
8793
test, err = problem
88-
self.stream.writeln(Fore.RED + " {0} has damaged your"
89-
"karma.".format(test._testMethodName))
94+
self.stream.writeln(" {0}{1}{2} has damaged your "
95+
"karma.".format(Fore.RED, Style.BRIGHT, test._testMethodName))
9096

97+
self.stream.writeln("\n{0}{1}You have not yet reached enlightenment ..." \
98+
.format(Fore.RESET, Style.NORMAL))
99+
self.stream.writeln("{0}{1}{2}".format(Fore.RED, \
100+
Style.BRIGHT, self.scrapeAssertionError(err)))
91101
self.stream.writeln("")
92-
self.stream.writeln(Fore.RESET + "You have not yet reached enlightenment ...")
93-
self.stream.writeln(Fore.RED + ""
94-
"{0}".format(self.scrapeAssertionError(err)))
95-
self.stream.writeln("")
96-
self.stream.writeln(Fore.RESET + "Please meditate on the following code:")
97-
self.stream.writeln(Fore.YELLOW +
98-
(self.scrapeInterestingStackDump(err)))
102+
self.stream.writeln("{0}{1}Please meditate on the following code:" \
103+
.format(Fore.RESET, Style.NORMAL))
104+
self.stream.writeln("{0}{1}{2}".format(Fore.YELLOW, Style.BRIGHT, \
105+
self.scrapeInterestingStackDump(err)))
99106

100107
def scrapeAssertionError(self, err):
101108
if not err: return ""
@@ -148,55 +155,57 @@ def say_something_zenlike(self):
148155
if self.failures:
149156
turn = self.pass_count % 37
150157

158+
zenness = "";
151159
if turn == 0:
152-
return Fore.CYAN + "Beautiful is better than ugly."
160+
zenness = "Beautiful is better than ugly."
153161
elif turn == 1 or turn == 2:
154-
return Fore.CYAN + "Explicit is better than implicit."
162+
zenness = "Explicit is better than implicit."
155163
elif turn == 3 or turn == 4:
156-
return Fore.CYAN + "Simple is better than complex."
164+
zenness = "Simple is better than complex."
157165
elif turn == 5 or turn == 6:
158-
return Fore.CYAN + "Complex is better than complicated."
166+
zenness = "Complex is better than complicated."
159167
elif turn == 7 or turn == 8:
160-
return Fore.CYAN + "Flat is better than nested."
168+
zenness = "Flat is better than nested."
161169
elif turn == 9 or turn == 10:
162-
return Fore.CYAN + "Sparse is better than dense."
170+
zenness = "Sparse is better than dense."
163171
elif turn == 11 or turn == 12:
164-
return Fore.CYAN + "Readability counts."
172+
zenness = "Readability counts."
165173
elif turn == 13 or turn == 14:
166-
return Fore.CYAN + ("Special cases aren't special enough to"
167-
"break the rules.")
174+
zenness = "Special cases aren't special enough to " \
175+
"break the rules."
168176
elif turn == 15 or turn == 16:
169-
return Fore.CYAN + "Although practicality beats purity."
177+
zenness = "Although practicality beats purity."
170178
elif turn == 17 or turn == 18:
171-
return Fore.CYAN + "Errors should never pass silently."
179+
zenness = "Errors should never pass silently."
172180
elif turn == 19 or turn == 20:
173-
return Fore.CYAN + "Unless explicitly silenced."
181+
zenness = "Unless explicitly silenced."
174182
elif turn == 21 or turn == 22:
175-
return Fore.CYAN + ("In the face of ambiguity, refuse the"
176-
"temptation to guess.")
183+
zenness = "In the face of ambiguity, refuse the " \
184+
"temptation to guess."
177185
elif turn == 23 or turn == 24:
178-
return Fore.CYAN + ("There should be one-- and preferably only"
179-
"one --obvious way to do it.")
186+
zenness = "There should be one-- and preferably only " \
187+
"one --obvious way to do it."
180188
elif turn == 25 or turn == 26:
181-
return Fore.CYAN + ("Although that way may not be obvious at"
182-
"first unless you're Dutch.")
189+
zenness = "Although that way may not be obvious at " \
190+
"first unless you're Dutch."
183191
elif turn == 27 or turn == 28:
184-
return Fore.CYAN + "Now is better than never."
192+
zenness = "Now is better than never."
185193
elif turn == 29 or turn == 30:
186-
return Fore.CYAN + ("Although never is often better than right"
187-
"now.")
194+
zenness = "Although never is often better than right " \
195+
"now."
188196
elif turn == 31 or turn == 32:
189-
return Fore.CYAN + ("If the implementation is hard to explain,"
190-
"it's a bad idea.")
197+
zenness = "If the implementation is hard to explain, " \
198+
"it's a bad idea."
191199
elif turn == 33 or turn == 34:
192-
return Fore.CYAN + ("If the implementation is easy to explain,"
193-
"it may be a good idea.")
200+
zenness = "If the implementation is easy to explain, " \
201+
"it may be a good idea."
194202
else:
195-
return Fore.CYAN + ("Namespaces are one honking great idea --"
196-
"let's do more of those!")
197-
203+
zenness = "Namespaces are one honking great idea -- " \
204+
"let's do more of those!"
205+
return "{0}{1}".format(Fore.CYAN, zenness);
198206
else:
199-
return Fore.BLUE + "Nobody ever expects the Spanish Inquisition."
207+
return "{0}Nobody ever expects the Spanish Inquisition." \
208+
.format(Fore.CYAN)
200209

201210
# Hopefully this will never ever happen!
202211
return "The temple in collapsing! Run!!!"

0 commit comments

Comments
 (0)