- commit
- 0930d2f14c35f3ead7a5b3a1aa8d61fdfbdb9906
- parent
- 77fb7a9152ef16242fc7f568fa1f25889ddd06c8
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-07 20:00
move dirtywords to separate repo requires to update dependencies via `python setup.py develop`
Diffstat
| D | laneya/dirtywords/__init__.py | 11 | ----------- |
| D | laneya/dirtywords/attr_string.py | 104 | ------------------------------------------------------------ |
| D | laneya/dirtywords/base.py | 83 | ------------------------------------------------------------ |
| D | laneya/dirtywords/curses_dirtywords.py | 74 | ------------------------------------------------------------ |
| D | laneya/dirtywords/example.py | 61 | ------------------------------------------------------------ |
| D | laneya/dirtywords/pygame_dirtywords.py | 46 | ---------------------------------------------- |
| D | laneya/dirtywords/stupid_dirtywords.py | 28 | ---------------------------- |
| M | setup.py | 1 | + |
8 files changed, 1 insertions, 407 deletions
diff --git a/laneya/dirtywords/__init__.py b/laneya/dirtywords/__init__.py
@@ -1,11 +0,0 @@1 -1 # flake8: noqa2 -13 -1 try:4 -1 from pygame_dirtywords import Screen5 -1 except ImportError:6 -1 try:7 -1 from curses_dirtywords import Screen8 -1 except ImportError:9 -1 from stupid_dirtywords import Screen10 -111 -1 from base import Window
diff --git a/laneya/dirtywords/attr_string.py b/laneya/dirtywords/attr_string.py
@@ -1,104 +0,0 @@1 -1 class AttrString(unicode):2 -1 def __new__(cls, s, **kwargs):3 -1 self = super(AttrString, cls).__new__(cls, s)4 -1 self.set_attrs(s, **kwargs)5 -1 return self6 -17 -1 def set_attrs(self, reference, **kwargs):8 -1 defaults = {9 -1 'bold': False,10 -1 'italic': False,11 -1 'underline': False,12 -1 'fg_color': (255, 255, 255),13 -1 'bg_color': (0, 0, 0),14 -1 }15 -116 -1 for attr in defaults.iterkeys():17 -1 if attr in kwargs:18 -1 value = kwargs[attr]19 -1 elif isinstance(reference, AttrString):20 -1 value = getattr(reference, attr)21 -1 else:22 -1 value = defaults[attr]23 -124 -1 setattr(self, attr, value)25 -126 -1 def get_attrs(self):27 -1 return {28 -1 'bold': self.bold,29 -1 'italic': self.italic,30 -1 'underline': self.underline,31 -1 'fg_color': self.fg_color,32 -1 'bg_color': self.bg_color,33 -1 }34 -135 -1 def __iter__(self):36 -1 return (self[i] for i in range(len(self)))37 -138 -1 def __getitem__(self, i):39 -1 ch = unicode.__getitem__(self, i)40 -1 return AttrString(ch, **self.get_attrs())41 -142 -143 -1 def normal(s):44 -1 return unicode(s)45 -146 -147 -1 def bold(s):48 -1 return AttrString(s, bold=True)49 -150 -151 -1 def italic(s):52 -1 return AttrString(s, italic=True)53 -154 -155 -1 def underline(s):56 -1 return AttrString(s, underline=True)57 -158 -159 -1 def white(s):60 -1 return AttrString(s, fg_color=(255, 255, 255))61 -162 -163 -1 def black(s):64 -1 return AttrString(s, fg_color=(0, 0, 0))65 -166 -167 -1 def red(s):68 -1 return AttrString(s, fg_color=(255, 0, 0))69 -170 -171 -1 def green(s):72 -1 return AttrString(s, fg_color=(0, 255, 0))73 -174 -175 -1 def blue(s):76 -1 return AttrString(s, fg_color=(0, 0, 255))77 -178 -179 -1 def yellow(s):80 -1 return AttrString(s, fg_color=(255, 255, 0))81 -182 -183 -1 def on_white(s):84 -1 return AttrString(s, bg_color=(255, 255, 255))85 -186 -187 -1 def on_black(s):88 -1 return AttrString(s, bg_color=(0, 0, 0))89 -190 -191 -1 def on_red(s):92 -1 return AttrString(s, bg_color=(255, 0, 0))93 -194 -195 -1 def on_green(s):96 -1 return AttrString(s, bg_color=(0, 255, 0))97 -198 -199 -1 def on_blue(s):100 -1 return AttrString(s, bg_color=(0, 0, 255))101 -1102 -1103 -1 def on_yellow(s):104 -1 return AttrString(s, bg_color=(255, 255, 0))
diff --git a/laneya/dirtywords/base.py b/laneya/dirtywords/base.py
@@ -1,83 +0,0 @@1 -1 class BaseScreen(object):2 -1 def __init__(self, height, width):3 -1 """Initialize screen."""4 -1 self.height = height5 -1 self.width = width6 -1 self.data = [[' ' for xx in range(width)] for yy in range(height)]7 -18 -1 def getch(self):9 -1 """Get next keystroke (blocking)."""10 -1 raise NotImplementedError11 -112 -1 def putstr(self, y, x, s):13 -1 """Write string to position."""14 -1 for i, ch in enumerate(s):15 -1 try:16 -1 self.data[y][x + i] = ch17 -1 except IndexError:18 -1 pass19 -120 -1 def refresh(self):21 -1 """Print the current state to the screen."""22 -1 raise NotImplementedError23 -124 -1 def cleanup(self):25 -1 """Deinitialize screen."""26 -1 pass27 -128 -129 -1 class Screen(BaseScreen):30 -1 """Additional utility functions for :py:class:`BaseScreen`."""31 -132 -1 def delch(self, y, x):33 -1 """Delete character at position."""34 -1 self.putstr(y, x, ' ')35 -136 -1 def fill_row(self, y, ch):37 -1 """Fill a complete row with character."""38 -1 self.putstr(y, 0, ch * self.width)39 -140 -1 def fill_column(self, x, ch):41 -1 """Fill a complete column with character."""42 -1 for y in range(self.height):43 -1 self.putstr(y, x, ch)44 -145 -1 def fill(self, ch):46 -1 """Fill whole screen with character."""47 -1 for y in range(self.height):48 -1 self.row(y, ch)49 -150 -1 def clear(self):51 -1 """Clear whole screen."""52 -1 self.fill(' ')53 -154 -1 def border(self, ls='|', rs='|', ts='-', bs='-',55 -1 tl='+', tr='+', bl='+', br='+'):56 -1 """Draw border around screen."""57 -1 self.fill_column(0, ls)58 -1 self.fill_column(self.width - 1, rs)59 -1 self.fill_row(0, ts)60 -1 self.fill_row(self.height - 1, bs)61 -1 self.putstr(0, 0, tl)62 -1 self.putstr(0, self.width - 1, tr)63 -1 self.putstr(self.height - 1, 0, bl)64 -1 self.putstr(self.height - 1, self.width - 1, br)65 -166 -167 -1 class Window(Screen):68 -1 """A screen that is rendered onto another screen."""69 -170 -1 def __init__(self, parent, height, width, y, x):71 -1 super(Window, self).__init__(height, width)72 -1 self.parent = parent73 -1 self.y = y74 -1 self.x = x75 -176 -1 def getch(self):77 -1 return self.parent.getch()78 -179 -1 def refresh(self):80 -1 for y in range(self.height):81 -1 for x in range(self.width):82 -1 self.parent.putstr(self.y + y, self.x + x, self.data[y][x])83 -1 self.parent.refresh()
diff --git a/laneya/dirtywords/curses_dirtywords.py b/laneya/dirtywords/curses_dirtywords.py
@@ -1,74 +0,0 @@1 -1 try:2 -1 from ncurses import curses3 -1 except ImportError:4 -1 import curses5 -16 -1 import base7 -1 from attr_string import AttrString8 -19 -110 -1 class Screen(base.Screen):11 -1 def __init__(self, height, width):12 -1 self.height = height13 -1 self.width = width14 -115 -1 curses.initscr()16 -1 curses.start_color()17 -1 curses.noecho()18 -1 curses.curs_set(0)19 -120 -1 # use black background everywhere21 -1 for i in range(1, 8):22 -1 curses.init_pair(i, i, 0)23 -124 -1 self.curses_window = curses.newwin(height, width, 0, 0)25 -126 -1 def getch(self):27 -1 return self.curses_window.getch()28 -129 -1 def _get_color(self, color):30 -1 r, g, b = color31 -1 r = int(round(r / 255.0)) * 25532 -1 g = int(round(g / 255.0)) * 25533 -1 b = int(round(b / 255.0)) * 25534 -135 -1 if (r, g, b) == (0, 0, 0):36 -1 return 037 -1 elif (r, g, b) == (255, 0, 0):38 -1 return 139 -1 elif (r, g, b) == (0, 255, 0):40 -1 return 241 -1 elif (r, g, b) == (255, 255, 0):42 -1 return 343 -1 elif (r, g, b) == (0, 0, 255):44 -1 return 445 -1 elif (r, g, b) == (255, 0, 255):46 -1 return 547 -1 elif (r, g, b) == (0, 255, 255):48 -1 return 649 -1 elif (r, g, b) == (255, 255, 255):50 -1 return 751 -152 -1 def putstr(self, y, x, s):53 -1 for i, ch in enumerate(s):54 -1 ch = AttrString(ch)55 -1 if ch.bold:56 -1 self.curses_window.attron(curses.A_BOLD)57 -1 if ch.underline:58 -1 self.curses_window.attron(curses.A_UNDERLINE)59 -1 color = self._get_color(ch.fg_color)60 -1 self.curses_window.attron(curses.color_pair(color))61 -162 -1 try:63 -1 self.curses_window.addstr(y, x + i, ch)64 -1 except:65 -1 pass66 -167 -1 self.curses_window.attroff(curses.A_BOLD)68 -1 self.curses_window.attroff(curses.A_UNDERLINE)69 -170 -1 def refresh(self):71 -1 self.curses_window.refresh()72 -173 -1 def cleanup(self):74 -1 curses.endwin()
diff --git a/laneya/dirtywords/example.py b/laneya/dirtywords/example.py
@@ -1,61 +0,0 @@1 -1 import sys2 -13 -1 try:4 -1 from pygame_dirtywords import Screen5 -1 except ImportError:6 -1 try:7 -1 from curses_dirtywords import Screen8 -1 except ImportError:9 -1 from stupid_dirtywords import Screen10 -111 -1 from attr_string import italic12 -1 from attr_string import blue13 -114 -115 -1 class Player(object):16 -1 def __init__(self, win):17 -1 self.win = win18 -1 self.y = win.height / 219 -1 self.x = win.width / 220 -121 -1 def move(self, direction):22 -1 self.win.putstr(self.y, self.x, ' ')23 -124 -1 if direction == 'up':25 -1 self.y -= 126 -1 elif direction == 'right':27 -1 self.x += 128 -1 elif direction == 'down':29 -1 self.y += 130 -1 elif direction == 'left':31 -1 self.x -= 132 -133 -1 self.win.putstr(self.y, self.x, italic(blue('X')))34 -1 self.win.refresh()35 -136 -137 -1 if __name__ == '__main__':38 -1 scr = Screen(32, 100)39 -140 -1 try:41 -1 scr.border()42 -143 -1 player = Player(scr)44 -1 player.move('down') # initial refresh45 -146 -1 while 1:47 -1 ch = scr.getch()48 -1 if ch == ord('h'):49 -1 player.move('left')50 -1 elif ch == ord('j'):51 -1 player.move('down')52 -1 elif ch == ord('k'):53 -1 player.move('up')54 -1 elif ch == ord('l'):55 -1 player.move('right')56 -1 elif ch == ord('q'):57 -1 scr.cleanup()58 -1 sys.exit()59 -1 except:60 -1 scr.cleanup()61 -1 raise
diff --git a/laneya/dirtywords/pygame_dirtywords.py b/laneya/dirtywords/pygame_dirtywords.py
@@ -1,46 +0,0 @@1 -1 import pygame2 -1 from pygame.locals import KEYDOWN3 -14 -1 import base5 -1 from attr_string import AttrString6 -17 -18 -1 class Screen(base.Screen):9 -1 def __init__(self, height, width):10 -1 super(Screen, self).__init__(height, width)11 -112 -1 pygame.init()13 -1 self.clock = pygame.time.Clock()14 -115 -1 self.font = pygame.font.SysFont('monospace', 10)16 -1 self.fontwidth, self.fontheight = self.font.size(' ')17 -118 -1 self.pygame_screen = pygame.display.set_mode(19 -1 (self.fontwidth * width, self.fontheight * height))20 -121 -1 def getch(self):22 -1 while True:23 -1 event = pygame.event.wait()24 -1 if event.type == KEYDOWN:25 -1 return event.key26 -127 -1 def _render_ch(self, ch):28 -1 ch = AttrString(ch)29 -1 self.font.set_bold(ch.bold)30 -1 self.font.set_italic(ch.italic)31 -1 self.font.set_underline(ch.underline)32 -1 return self.font.render(ch, True, ch.fg_color, ch.bg_color)33 -134 -1 def putstr(self, y, x, s):35 -1 super(Screen, self).putstr(y, x, s)36 -1 for i, ch in enumerate(s):37 -1 self.pygame_screen.blit(38 -1 self._render_ch(ch),39 -1 ((x + i) * self.fontwidth, y * self.fontheight))40 -141 -1 def refresh(self):42 -1 self.clock.tick()43 -1 pygame.display.flip()44 -145 -1 def cleanup(self):46 -1 pygame.quit()
diff --git a/laneya/dirtywords/stupid_dirtywords.py b/laneya/dirtywords/stupid_dirtywords.py
@@ -1,28 +0,0 @@1 -1 import sys2 -13 -1 import base4 -15 -16 -1 class Screen(base.Screen):7 -1 def getch(self):8 -1 # http://code.activestate.com/recipes/134892/9 -110 -1 try:11 -1 import msvcrt12 -1 return msvcrt.getch()13 -1 except ImportError:14 -1 import termios15 -1 import tty16 -1 fd = sys.stdin.fileno()17 -1 old_settings = termios.tcgetattr(fd)18 -1 try:19 -1 tty.setraw(sys.stdin.fileno())20 -1 ch = sys.stdin.read(1)21 -1 finally:22 -1 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)23 -1 return ord(ch)24 -125 -1 def refresh(self):26 -1 spacing = '\n' * self.height * 227 -1 s = '\n'.join([''.join(row) for row in self.data])28 -1 print(spacing + s)
diff --git a/setup.py b/setup.py
@@ -12,6 +12,7 @@ setup( 12 12 packages=['laneya'], 13 13 install_requires=[ 14 14 'twisted', -1 15 'dirtywords', 15 16 ], 16 17 entry_points={'console_scripts': [ 17 18 'laneya=laneya.client:main',