From 948a2d8766535f66eec6d3a1ce9954f7a4441cf7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 19 Apr 2020 17:05:43 -0700 Subject: [PATCH] Alphabetized python code and updated docs --- Python/btui.py | 170 ++++++++++++++++++++++++------------------------- README.md | 4 +- 2 files changed, 88 insertions(+), 86 deletions(-) diff --git a/Python/btui.py b/Python/btui.py index daa64f1..d940e58 100644 --- a/Python/btui.py +++ b/Python/btui.py @@ -102,8 +102,24 @@ BTUI_INVERSE_ATTRS = { class BTUI: - def enable(self): - self._btui = libbtui.btui_enable() + @contextmanager + def attributes(self, *attrs): + self.set_attributes(*attrs) + try: yield + finally: self.unset_attributes(*attrs) + + @contextmanager + def bg(self, r, g, b): + self.set_bg(r, g, b) + try: yield + finally: self.set_attributes("bg_normal") + + def clear(self, mode='screen'): + assert self._btui + if mode not in ('screen', 'above', 'below', 'line', 'left', 'right'): + raise ArgumentError("Not a supported clear type: "+repr(mode)) + clr = ctypes.c_uint.in_dll(libbtui, 'BTUI_CLEAR_' + mode.upper()) + libbtui.btui_clear(self._btui, clr) def disable(self): libbtui.btui_disable(self._btui) @@ -114,6 +130,27 @@ class BTUI: try: yield self finally: self.enable() + def draw_shadow(self, x, y, w, h): + assert self._btui + libbtui.btui_draw_shadow(self._btui, int(x), int(y), int(w), int(h)) + + def enable(self): + self._btui = libbtui.btui_enable() + + @contextmanager + def fg(self, r, g, b): + self.set_fg(r, g, b) + try: yield + finally: self.set_attributes("fg_normal") + + def fill_box(self, x, y, w, h): + assert self._btui + libbtui.btui_fill_box(self._btui, int(x), int(y), int(w), int(h)) + + def flush(self): + assert self._btui + libbtui.btui_flush(self._btui) + def getkey(self, timeout=None): assert self._btui timeout = -1 if timeout is None else int(timeout) @@ -127,16 +164,57 @@ class BTUI: else: return buf.value.decode('utf8'), mouse_x.value, mouse_y.value - @property - def width(self): - assert self._btui - return self._btui.contents.width - @property def height(self): assert self._btui return self._btui.contents.height + def move(self, x, y): + assert self._btui + libbtui.btui_move_cursor(self._btui, int(x), int(y)) + + def outline_box(self, x, y, w, h): + assert self._btui + libbtui.btui_draw_linebox(self._btui, int(x), int(y), int(w), int(h)) + + def scroll(self, firstline, lastline=None, amount=None): + assert self._btui + if amount is None: + amount = firstline + firstline, lastline = 1, self.height + libbtui.btui_scroll(self._btui, firstline, lastline, amount) + + def set_attributes(self, *attrs): + assert self._btui + attr_long = ctypes.c_longlong(0) + for a in attrs: + attr_long.value |= BTUI_ATTRS[a].value + libbtui.btui_set_attributes(self._btui, attr_long) + + def set_bg(self, r, g, b): + assert self._btui + libbtui.btui_set_bg(self._btui, int(r*255), int(g*255), int(b*255)) + + def set_fg(self, r, g, b): + assert self._btui + libbtui.btui_set_fg(self._btui, int(r*255), int(g*255), int(b*255)) + + def suspend(self): + assert self._btui + libbtui.btui_suspend(self._btui) + + def unset_attributes(self, *attrs): + assert self._btui + attr_long = ctypes.c_longlong(0) + for a in attrs: + attr_long.value |= BTUI_INVERSE_ATTRS[a].value + libbtui.btui_set_attributes(self._btui, attr_long) + + @property + def width(self): + assert self._btui + return self._btui.contents.width + def write(self, *args, sep=''): assert self._btui s = sep.join(args) @@ -146,84 +224,6 @@ class BTUI: assert self._btui libbtui.btui_puts(self._btui, b) - def move(self, x, y): - assert self._btui - libbtui.btui_move_cursor(self._btui, int(x), int(y)) - - def scroll(self, firstline, lastline=None, amount=None): - assert self._btui - if amount is None: - amount = firstline - firstline, lastline = 1, self.height - libbtui.btui_scroll(self._btui, firstline, lastline, amount) - - def flush(self): - assert self._btui - libbtui.btui_flush(self._btui) - - def suspend(self): - assert self._btui - libbtui.btui_suspend(self._btui) - - def clear(self, mode='screen'): - assert self._btui - if mode not in ('screen', 'above', 'below', 'line', 'left', 'right'): - raise ArgumentError("Not a supported clear type: "+repr(mode)) - clr = ctypes.c_uint.in_dll(libbtui, 'BTUI_CLEAR_' + mode.upper()) - libbtui.btui_clear(self._btui, clr) - - def set_attributes(self, *attrs): - assert self._btui - attr_long = ctypes.c_longlong(0) - for a in attrs: - attr_long.value |= BTUI_ATTRS[a].value - libbtui.btui_set_attributes(self._btui, attr_long) - - def unset_attributes(self, *attrs): - assert self._btui - attr_long = ctypes.c_longlong(0) - for a in attrs: - attr_long.value |= BTUI_INVERSE_ATTRS[a].value - libbtui.btui_set_attributes(self._btui, attr_long) - - @contextmanager - def attributes(self, *attrs): - self.set_attributes(*attrs) - try: yield - finally: self.unset_attributes(*attrs) - - def set_fg(self, r, g, b): - assert self._btui - libbtui.btui_set_fg(self._btui, int(r*255), int(g*255), int(b*255)) - - def set_bg(self, r, g, b): - assert self._btui - libbtui.btui_set_bg(self._btui, int(r*255), int(g*255), int(b*255)) - - @contextmanager - def fg(self, r, g, b): - self.set_fg(r, g, b) - try: yield - finally: self.set_attributes("fg_normal") - - @contextmanager - def bg(self, r, g, b): - self.set_bg(r, g, b) - try: yield - finally: self.set_attributes("bg_normal") - - def outline_box(self, x, y, w, h): - assert self._btui - libbtui.btui_draw_linebox(self._btui, int(x), int(y), int(w), int(h)) - - def fill_box(self, x, y, w, h): - assert self._btui - libbtui.btui_fill_box(self._btui, int(x), int(y), int(w), int(h)) - - def draw_shadow(self, x, y, w, h): - assert self._btui - libbtui.btui_draw_shadow(self._btui, int(x), int(y), int(w), int(h)) - _btui = BTUI() @contextmanager diff --git a/README.md b/README.md index 018eaa6..668e5ef 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ bt:getkey(timeout=-1) -- Returns a keypress (and optionally, mouse x and y coord bt:height() -- Return the screen height bt:linebox(x,y,w,h) -- Draw an outlined box around the given rectangle bt:move(x, y) -- Move the cursor to the given position. (0,0) is the top left corner. +bt:scroll(firstline, lastline, amount) -- Scroll the given screen region by the given amount. bt:setattributes(attrs...) -- Set the given attributes bt:shadow(x,y,w,h) -- Draw a shaded shadow to the bottom right of the given rectangle bt:suspend() -- Suspend the current process and drop back into normal terminal mode @@ -179,11 +180,12 @@ class BTUI: def fg(self, r, g, b): # R,G,B values are [0.0, 1.0] def fill_box(self, x, y, w, h): def flush(self): - def getkey(self, timeout=None): # returns key, mouse_x, mouse_y + def getkey(self, timeout=None): @property def height(self): def move(self, x, y): def outline_box(self, x, y, w, h): + def scroll(self, firstline, lastline=None, amount=None): def set_attributes(self, *attrs): def set_bg(self, r, g, b): # R,G,B values are [0.0, 1.0] def set_fg(self, r, g, b): # R,G,B values are [0.0, 1.0]