Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
authorRadek Brich <radek.brich@devl.cz>
Fri, 28 Mar 2014 14:58:12 +0100
changeset 95 05392e369ede
parent 94 e50dae408fe9
child 96 68c562e0eb1f
Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
demos/01_buffer.py
demos/02_curses.py
tuikit/core/buffer.py
tuikit/core/window.py
tuikit/driver/curses.py
tuikit/widgets/button.py
tuikit/widgets/label.py
--- a/demos/01_buffer.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/demos/01_buffer.py	Fri Mar 28 14:58:12 2014 +0100
@@ -7,11 +7,11 @@
 
 
 buffer = Buffer(16, 8)
-buffer.fill(c='#')
-buffer.hline(1, 1, 14, c='-')
-buffer.hline(1, 6, 14, c='-')
-buffer.vline(7, 2, 4, c='|')
-buffer.puts(8, 4, 'Hello!')
+buffer.fill('#')
+buffer.hline('-', 1, 1, 14)
+buffer.hline('-', 1, 6, 14)
+buffer.vline('|', 7, 2, 4)
+buffer.puts('Hello!', 8, 4)
 buffer.frame()
 
 driver = Driver()
--- a/demos/02_curses.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/demos/02_curses.py	Fri Mar 28 14:58:12 2014 +0100
@@ -12,11 +12,11 @@
           'lightmagenta', 'yellow', 'white')
 
 buffer = Buffer(16, 8)
-buffer.fill(c='#')
-buffer.hline(1, 1, 14, c='-')
-buffer.hline(1, 6, 14, c='-')
-buffer.vline(7, 2, 4, c='|')
-buffer.puts(8, 4, 'Hello!')
+buffer.fill('#')
+buffer.hline('-', 1, 1, 14)
+buffer.hline('-', 1, 6, 14)
+buffer.vline('|', 7, 2, 4)
+buffer.puts('Hello!', 8, 4)
 buffer.frame()
 
 buffer2 = Buffer(30, 10)
@@ -25,7 +25,7 @@
 
 for idx, color in enumerate(colors):
     buffer2.setattr(color)
-    buffer2.puts(3 + 12 * (idx // 8), 1 + idx % 8, color)
+    buffer2.puts(color, 3 + 12 * (idx // 8), 1 + idx % 8)
 
 buffer3 = Buffer(30, 10)
 buffer3.setattr('white on black')
@@ -33,12 +33,12 @@
 
 for idx, color in enumerate(colors[:8]):
     buffer3.setattr('black on {}'.format(color))
-    buffer3.puts(2, 1 + idx, ' '+color.ljust(10))
+    buffer3.puts(' '+color.ljust(10), 2, 1 + idx)
     buffer3.setattr('{}, underline, bold'.format(color))
-    buffer3.puts(15, 1 + idx, ' '+color.ljust(10))
+    buffer3.puts(' '+color.ljust(10), 15, 1 + idx)
 
 buffer3.setattr('lightgray, blink')
-buffer3.puts(3, 8, 'blink')
+buffer3.puts('blink', 3, 8)
 
 driver = CursesDriver()
 with driver:
--- a/tuikit/core/buffer.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/tuikit/core/buffer.py	Fri Mar 28 14:58:12 2014 +0100
@@ -32,24 +32,26 @@
             for bufx in range(buffer.size.w):
                 char, attr_desc = buffer.get(bufx, bufy)
                 self.setattr(attr_desc)
-                self.putch(x + bufx, y + bufy, char)
+                self.putch(char, x + bufx, y + bufy)
 
-    def puts(self, x, y, s):
+    def puts(self, s, x=0, y=0):
         """Output string of characters."""
-        for c in s:
-            self.putch(x, y, c)
+        for ch in s:
+            self.putch(ch, x, y)
             x += 1
 
-    def hline(self, x, y, w, c=' '):
+    def hline(self, ch='-', x=0, y=0, w=0):
         """Draw horizontal line."""
-        self.puts(x, y, [c] * w)
+        w = self.size.w if not w else w
+        self.puts([ch] * w, x, y)
 
-    def vline(self, x, y, h, c=' '):
+    def vline(self, ch='|', x=0, y=0, h=0):
         """Draw vertical line."""
+        h = self.size.h if not h else h
         for i in range(h):
-            self.putch(x, y + i, c)
+            self.putch(ch, x, y + i)
 
-    def fill(self, x=0, y=0, w=0, h=0, c=' '):
+    def fill(self, ch=' ', x=0, y=0, w=0, h=0):
         """Fill rectangular area.
 
         Fill whole buffer if width or height is not specified (zero).
@@ -58,7 +60,7 @@
         w = self.size.w if not w else w
         h = self.size.h if not h else h
         for i in range(h):
-            self.hline(x, y + i, w, c)
+            self.hline(ch, x, y + i, w)
 
     def frame(self, x=0, y=0, w=0, h=0, theme=default_theme):
         """Draw rectangular frame.
@@ -69,14 +71,14 @@
         """
         w = self.size.w if not w else w
         h = self.size.h if not h else h
-        self.putch(x,      y,      theme.frame_ulcorner)
-        self.putch(x+w-1,  y,      theme.frame_urcorner)
-        self.putch(x,      y+h-1,  theme.frame_llcorner)
-        self.putch(x+w-1,  y+h-1,  theme.frame_lrcorner)
-        self.hline(x+1,    y,      w-2,  theme.frame_hline)
-        self.hline(x+1,    y+h-1,  w-2,  theme.frame_hline)
-        self.vline(x,      y+1,    h-2,  theme.frame_vline)
-        self.vline(x+w-1,  y+1,    h-2,  theme.frame_vline)
+        self.putch(theme.frame_ulcorner,    x,      y)
+        self.putch(theme.frame_urcorner,    x+w-1,  y)
+        self.putch(theme.frame_llcorner,    x,      y+h-1)
+        self.putch(theme.frame_lrcorner,    x+w-1,  y+h-1)
+        self.hline(theme.frame_hline,       x+1,    y,      w-2)
+        self.hline(theme.frame_hline,       x+1,    y+h-1,  w-2)
+        self.vline(theme.frame_vline,       x,      y+1,    h-2)
+        self.vline(theme.frame_vline,       x+w-1,  y+1,    h-2)
 
     @contextmanager
     def attr(self, attr_desc):
@@ -218,7 +220,7 @@
             self._attr_map[attr_desc] = attr
         self._current_attr = attr
 
-    def putch(self, x, y, ch):
+    def putch(self, ch, x=0, y=0):
         """Set character on `x`, `y` coords to `ch`.
 
         Coords are adjusted by origin.
@@ -265,7 +267,7 @@
         """Set attribute to be used for subsequent draw operations."""
         self.target.setattr(attr_desc)
 
-    def putch(self, x, y, ch):
+    def putch(self, ch, x=0, y=0):
         """Set character on `x`, `y` coords to `ch`.
 
         Coords are adjusted by origin.
@@ -274,5 +276,5 @@
         x += self.origin.x
         y += self.origin.y
         if self.testxy(x, y):
-            self.target.putch(x, y, ch)
+            self.target.putch(ch, x, y)
 
--- a/tuikit/core/window.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/tuikit/core/window.py	Fri Mar 28 14:58:12 2014 +0100
@@ -40,7 +40,7 @@
     def redraw(self):
         self.buffer.reset_origin()
         Container.draw(self, self.buffer)
-        self.buffer.puts(10, 5, '{0.w} {0.h}'.format(self.size))
+        self.buffer.puts('{0.w} {0.h}'.format(self.size), 10, 5)
         self.buffer.frame()
 
     def draw(self, buffer):
--- a/tuikit/driver/curses.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/tuikit/driver/curses.py	Fri Mar 28 14:58:12 2014 +0100
@@ -79,7 +79,7 @@
 
     def __init__(self):
         Driver.__init__(self)
-        self.log = logging.getLogger('tuikit')
+        self._log = logging.getLogger('tuikit')
         self.stdscr = None
         self.cursor = None
         self.colors = {}     # maps names to curses attributes
@@ -119,21 +119,23 @@
     def clear(self):
         self.stdscr.erase()
 
-    def putch(self, x, y, ch):
+    def putch(self, ch, x, y):
         try:
-            if isinstance(ch, str) and len(ch) == 1:
+            if isinstance(ch, int):
+                self.stdscr.addch(y, x, ch)
+            elif isinstance(ch, str) and len(ch) == 1:
                 self.stdscr.addstr(y, x, ch)
             else:
-                self.stdscr.addch(y, x, ch)
-        except curses.error:
-            pass
+                raise TypeError('Integer or one-char string is required.')
+        except curses.error as e:
+            self._log.exception('putch(%r, %s, %s) error:' % (ch, x, y))
 
     def draw(self, buffer, x=0, y=0):
         for bufy in range(buffer.size.h):
             for bufx in range(buffer.size.w):
                 char, attr_desc = buffer.get(bufx, bufy)
                 self.setattr(attr_desc)
-                self.putch(x + bufx, y + bufy, char)
+                self.putch(char, x + bufx, y + bufy)
 
     def flush(self):
         if self.cursor:
--- a/tuikit/widgets/button.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/tuikit/widgets/button.py	Fri Mar 28 14:58:12 2014 +0100
@@ -57,19 +57,19 @@
         lpad, rpad = self._divide_padding(pad)
         with buffer.attr(self._get_color()):
             # prefix
-            buffer.puts(0, 0, self.prefix)
+            buffer.puts(self.prefix)
             pos = len(self.prefix)
             # left pad
-            buffer.puts(pos, 0, ' ' * lpad)
+            buffer.puts(' ' * lpad, pos)
             pos += lpad
             # label
-            buffer.puts(pos, 0, self.label)
+            buffer.puts(self.label, pos)
             pos += len(self.label)
             # right pad
-            buffer.puts(pos, 0, ' ' * rpad)
+            buffer.puts(' ' * rpad, pos)
             pos += rpad
             # suffix
-            buffer.puts(pos, 0, self.suffix)
+            buffer.puts(self.suffix, pos)
 
     def on_mousedown(self, ev):
         self.highlight = True
--- a/tuikit/widgets/label.py	Fri Mar 28 10:44:29 2014 +0100
+++ b/tuikit/widgets/label.py	Fri Mar 28 14:58:12 2014 +0100
@@ -14,4 +14,4 @@
 
     def draw(self, buffer):
         with buffer.attr(self.color):
-            buffer.puts(0, 0, self.label)
+            buffer.puts(self.label)