From 7ddf5ebe2e88be1012cee9d0cb4c47dc8a3bc86b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 17 Sep 2014 16:34:32 -0400 Subject: add accessor methods for all of the cell attributes this should provide a much saner api --- vt100/__init__.py | 96 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/vt100/__init__.py b/vt100/__init__.py index 2161866..ccc6d87 100644 --- a/vt100/__init__.py +++ b/vt100/__init__.py @@ -2,53 +2,105 @@ from ctypes import * import vt100_raw -class vt100_rgb_color(Union): +class vt100_rgb_color(Structure): _fields_ = [ - ("r", c_ubyte), - ("g", c_ubyte), - ("b", c_ubyte), + ("_r", c_ubyte), + ("_g", c_ubyte), + ("_b", c_ubyte), + ] + +class vt100_color_type(Union): + _anonymous_ = ("_rgb",) + _fields_ = [ + ("_rgb", vt100_rgb_color), + ("_idx", c_ubyte), ] class vt100_color(Structure): - _anonymous_ = ("rgb",) + _anonymous_ = ("_color",) _fields_ = [ - ("rgb", vt100_rgb_color), - ("type", c_ubyte), + ("_color", vt100_color_type), + ("_type", c_ubyte), ] + def type(self): + if self._type == 0: + return "default" + elif self._type == 1: + return "indexed" + elif self._type == 2: + return "rgb" + else: + raise Exception("unknown color type: %d" % self._type) + + def color(self): + color_type = self.type() + if color_type == "default": + return None + elif color_type == "indexed": + return self._idx + elif color_type == "rgb": + return (self._r, self._g, self._b) + else: + raise Exception("unknown color type: %s" % color_type) + class vt100_named_attrs(Structure): _fields_ = [ - ("bold", c_ubyte, 1), - ("italic", c_ubyte, 1), - ("underline", c_ubyte, 1), - ("inverse", c_ubyte, 1), + ("_bold", c_ubyte, 1), + ("_italic", c_ubyte, 1), + ("_underline", c_ubyte, 1), + ("_inverse", c_ubyte, 1), ] class vt100_attrs(Union): - _anonymous_ = ("named",) + _anonymous_ = ("_named",) _fields_ = [ - ("named", vt100_named_attrs), - ("attrs", c_ubyte), + ("_named", vt100_named_attrs), + ("_attrs", c_ubyte), ] class vt100_cell_attrs(Structure): - _anonymous_ = ("cell_attrs",) + _anonymous_ = ("_cell_attrs",) _fields_ = [ - ("fgcolor", vt100_color), - ("bgcolor", vt100_color), - ("cell_attrs", vt100_attrs), + ("_fgcolor", vt100_color), + ("_bgcolor", vt100_color), + ("_cell_attrs", vt100_attrs), ] class vt100_cell(Structure): _fields_ = [ ("_contents", c_char * 8), - ("len", c_size_t), - ("attrs", vt100_cell_attrs), - ("is_wide", c_ubyte, 1), + ("_len", c_size_t), + ("_attrs", vt100_cell_attrs), + ("_is_wide", c_ubyte, 1), ] def contents(self): - return self._contents[:self.len].decode('utf-8') + return self._contents[:self._len].decode('utf-8') + + def fgcolor(self): + return self._attrs._fgcolor + + def bgcolor(self): + return self._attrs._bgcolor + + def all_attrs(self): + return self._attrs._attrs + + def bold(self): + return self._attrs._bold != 0 + + def italic(self): + return self._attrs._italic != 0 + + def underline(self): + return self._attrs._underline != 0 + + def inverse(self): + return self._attrs._inverse != 0 + + def is_wide(self): + return self._is_wide # XXX process/cell need mutexes class vt100(object): -- cgit v1.2.3-54-g00ecf