- commit
- c77e5eb4d0acf32069b1938070c85393a91011d2
- parent
- 0bdf745115d7383e28fcc0f2fb00ad6314e471c8
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-06 21:36
dirtywords: implement strings with additional attributes
Diffstat
| A | laneya/dirtywords/attr_string.py | 104 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | laneya/dirtywords/example.py | 5 | ++++- |
2 files changed, 108 insertions, 1 deletions
diff --git a/laneya/dirtywords/attr_string.py b/laneya/dirtywords/attr_string.py
@@ -0,0 +1,104 @@
-1 1 class AttrString(unicode):
-1 2 def __new__(cls, s, **kwargs):
-1 3 self = super(AttrString, cls).__new__(cls, s)
-1 4 self.set_attrs(s, **kwargs)
-1 5 return self
-1 6
-1 7 def set_attrs(self, reference, **kwargs):
-1 8 defaults = {
-1 9 'bold': False,
-1 10 'italic': False,
-1 11 'underline': False,
-1 12 'fg_color': (255, 255, 255),
-1 13 'bg_color': (0, 0, 0),
-1 14 }
-1 15
-1 16 for attr in defaults.iterkeys():
-1 17 if attr in kwargs:
-1 18 value = kwargs[attr]
-1 19 elif isinstance(reference, AttrString):
-1 20 value = getattr(reference, attr)
-1 21 else:
-1 22 value = defaults[attr]
-1 23
-1 24 setattr(self, attr, value)
-1 25
-1 26 def get_attrs(self):
-1 27 return {
-1 28 'bold': self.bold,
-1 29 'italic': self.italic,
-1 30 'underline': self.underline,
-1 31 'fg_color': self.fg_color,
-1 32 'bg_color': self.bg_color,
-1 33 }
-1 34
-1 35 def __iter__(self):
-1 36 return (self[i] for i in range(len(self)))
-1 37
-1 38 def __getitem__(self, i):
-1 39 ch = unicode.__getitem__(self, i)
-1 40 return AttrString(ch, **self.get_attrs())
-1 41
-1 42
-1 43 def normal(s):
-1 44 return unicode(s)
-1 45
-1 46
-1 47 def bold(s):
-1 48 return AttrString(s, bold=True)
-1 49
-1 50
-1 51 def italic(s):
-1 52 return AttrString(s, italic=True)
-1 53
-1 54
-1 55 def underline(s):
-1 56 return AttrString(s, underline=True)
-1 57
-1 58
-1 59 def white(s):
-1 60 return AttrString(s, fg_color=(255, 255, 255))
-1 61
-1 62
-1 63 def black(s):
-1 64 return AttrString(s, fg_color=(0, 0, 0))
-1 65
-1 66
-1 67 def red(s):
-1 68 return AttrString(s, fg_color=(255, 0, 0))
-1 69
-1 70
-1 71 def green(s):
-1 72 return AttrString(s, fg_color=(0, 255, 0))
-1 73
-1 74
-1 75 def blue(s):
-1 76 return AttrString(s, fg_color=(0, 0, 255))
-1 77
-1 78
-1 79 def yellow(s):
-1 80 return AttrString(s, fg_color=(255, 255, 0))
-1 81
-1 82
-1 83 def on_white(s):
-1 84 return AttrString(s, bg_color=(255, 255, 255))
-1 85
-1 86
-1 87 def on_black(s):
-1 88 return AttrString(s, bg_color=(0, 0, 0))
-1 89
-1 90
-1 91 def on_red(s):
-1 92 return AttrString(s, bg_color=(255, 0, 0))
-1 93
-1 94
-1 95 def on_green(s):
-1 96 return AttrString(s, bg_color=(0, 255, 0))
-1 97
-1 98
-1 99 def on_blue(s):
-1 100 return AttrString(s, bg_color=(0, 0, 255))
-1 101
-1 102
-1 103 def on_yellow(s):
-1 104 return AttrString(s, bg_color=(255, 255, 0))
diff --git a/laneya/dirtywords/example.py b/laneya/dirtywords/example.py
@@ -8,6 +8,9 @@ except ImportError: 8 8 except ImportError: 9 9 from stupid_dirtywords import Screen 10 10 -1 11 from attr_string import italic -1 12 from attr_string import blue -1 13 11 14 12 15 class Player(object): 13 16 def __init__(self, win): @@ -27,7 +30,7 @@ class Player(object): 27 30 elif direction == 'left': 28 31 self.x -= 1 29 3230 -1 self.win.putstr(self.y, self.x, 'X')-1 33 self.win.putstr(self.y, self.x, italic(blue('X'))) 31 34 self.win.refresh() 32 35 33 36