class Scale(str):
'''
This is essentially an implimentation of Ruby's .method_missing
that shortens the code dramatically, and allows for simply extending
to support other escape codes. As a note, the modifier methods like
.bold() and .underline() and such, need to come before the color
methods. The color should always be the last modifier.
'''
def __getattr__(self, method):
method_map = {
'black': {'color': True, 'value': 30, 'mode': 'm'},
'red': {'color': True, 'value': 31, 'mode': 'm'},
'green': {'color': True, 'value': 32, 'mode': 'm'},
'yellow': {'color': True, 'value': 33, 'mode': 'm'},
'blue': {'color': True, 'value': 34, 'mode': 'm'},
'purple': {'color': True, 'value': 35, 'mode': 'm'},
'cyan': {'color': True, 'value': 36, 'mode': 'm'},
'white': {'color': True, 'value': 37, 'mode': 'm'},
'clean': {'color': False, 'value': 0, 'mode': 'm'},
'bold': {'color': False, 'value': 1, 'mode': 'm'},
'underline': {'color': False, 'value': 4, 'mode': 'm'},
'blink': {'color': False, 'value': 5, 'mode': 'm'},
'reverse': {'color': False, 'value': 7, 'mode': 'm'},
'conceal': {'color': False, 'value': 8, 'mode': 'm'},
}
def get(self, **kwargs):
if method_map[method]['color']:
reset='[0m'
else:
reset=''
return(
Test('%s[%s%s%s%s' % (
reset,
method_map[method]['value'],
method_map[method]['mode'],
self,
reset
)
))
if method in method_map:
return get.__get__(self)
else:
raise(AttributeError, method)
Diff to Previous Revision
--- revision 2 2014-07-14 04:12:10
+++ revision 3 2014-07-24 01:03:13
@@ -1,48 +1,46 @@
-class AnsiColor(unicode):
- # worker
- def colorize(self, color, mode='m'):
- return "%s%s%s[0m" % (color, mode, self)
+class Scale(str):
+ '''
+ This is essentially an implimentation of Ruby's .method_missing
+ that shortens the code dramatically, and allows for simply extending
+ to support other escape codes. As a note, the modifier methods like
+ .bold() and .underline() and such, need to come before the color
+ methods. The color should always be the last modifier.
+ '''
+ def __getattr__(self, method):
+ method_map = {
+ 'black': {'color': True, 'value': 30, 'mode': 'm'},
+ 'red': {'color': True, 'value': 31, 'mode': 'm'},
+ 'green': {'color': True, 'value': 32, 'mode': 'm'},
+ 'yellow': {'color': True, 'value': 33, 'mode': 'm'},
+ 'blue': {'color': True, 'value': 34, 'mode': 'm'},
+ 'purple': {'color': True, 'value': 35, 'mode': 'm'},
+ 'cyan': {'color': True, 'value': 36, 'mode': 'm'},
+ 'white': {'color': True, 'value': 37, 'mode': 'm'},
+ 'clean': {'color': False, 'value': 0, 'mode': 'm'},
+ 'bold': {'color': False, 'value': 1, 'mode': 'm'},
+ 'underline': {'color': False, 'value': 4, 'mode': 'm'},
+ 'blink': {'color': False, 'value': 5, 'mode': 'm'},
+ 'reverse': {'color': False, 'value': 7, 'mode': 'm'},
+ 'conceal': {'color': False, 'value': 8, 'mode': 'm'},
+ }
- # Normal colors
- def black(self):
- return AnsiColor(self.colorize('[0m[30'))
+ def get(self, **kwargs):
+ if method_map[method]['color']:
+ reset='[0m'
+ else:
+ reset=''
- def red(self):
- return AnsiColor(self.colorize('[0m[31'))
+ return(
+ Test('%s[%s%s%s%s' % (
+ reset,
+ method_map[method]['value'],
+ method_map[method]['mode'],
+ self,
+ reset
+ )
+ ))
- def green(self):
- return AnsiColor(self.colorize('[0m[32'))
-
- def yellow(self):
- return AnsiColor(self.colorize('[0m[33'))
-
- def blue(self):
- return AnsiColor(self.colorize('[0m[34'))
-
- def purple(self):
- return AnsiColor(self.colorize('[0m[35'))
-
- def cyan(self):
- return AnsiColor(self.colorize('[0m[36'))
-
- def white(self):
- return AnsiColor(self.colorize('[0m[37'))
-
- # Fun stuff
- def clean(self):
- return AnsiColor(self.colorize('[0'))
-
- def bold(self):
- return AnsiColor(self.colorize('[1'))
-
- def underline(self):
- return AnsiColor(self.colorize('[4'))
-
- def blink(self):
- return AnsiColor(self.colorize('[5'))
-
- def reverse(self):
- return AnsiColor(self.colorize('[7'))
-
- def conceal(self):
- return AnsiColor(self.colorize('[8'))
+ if method in method_map:
+ return get.__get__(self)
+ else:
+ raise(AttributeError, method)