Commits

Henning Schröder  committed f657134

replaced cursed with own ansiengine, not fully working right now

  • Participants
  • Parent commits 7b98ee4

Comments (0)

Files changed (6)

File tgui/__init__.py

     TEventTypes, TKey, TFocusPolicy, TKeyEvent, TPaintEvent, TCloseEvent, THideEvent, TShowEvent,
     TResizeEvent, TFocusEvent, TMargins, TColor, TPalette, elide,
     TPainter, TColorScheme)
-from tgui.cursesengine import debug, TPaintEngine, TInputEngine
+from tgui.ansiengine import debug, TPaintEngine, TInputEngine
 from tgui.tapplication import TInputContext, TApplication, TDesktopWidget
 from tgui.buttons import TAbstractButton, TPushButton, TToolButton, TCheckBox, TRadioButton
 from tgui.widgets import AbstractTextWidgetMixin, TLineEdit, TMenu, TMenuBar, TLabel

File tgui/ansiconsole.py

 # http://en.wikipedia.org/wiki/ANSI_escape_code
 ESC_CLEAR_SCREEN = "\033[2J"
 ESC_GOTOXY = "\033[%d;%dH"
-ESC_COLOR = "\033[%dm " 
+ESC_COLOR = "\033[%dm"
 ESC_RESET = ESC_COLOR % 0
 ESC_BRIGHT = ESC_COLOR % 1
 ESC_DIM = ESC_COLOR % 2
     write(ESC_RESET)
     write(ESC_NORMAL)
 
-
 @atexit.register
 def _exit_handler():
     write(ESC_RESET)
 def gotoxy(x, y):
     write(ESC_GOTOXY % (y + 1, x))
 
-    
 def text(x, y, fg, bg, s):
     out = ESC_GOTOXY % (y, x) 
     out += ESC_BACKGROUND_COLORS[bg]
     gotoxy(x1, y2)
     write(bottom)
 
-
 def terminal_size():
     # adapted from
     # http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
     hide_cursor()
     color("bright-white", "black")
     box(0, 0, COLUMNS, ROWS)
-    if 1:
-        for y, fg in enumerate(ESC_BACKGROUND_COLORS.keys()):
-            for x, bg in enumerate(ESC_FOREGROUND_COLORS.keys()):
-                text(1+x * 5, 1+y, fg, bg, fg.replace("bright-", "")[:4])
-    gotoxy(1, 1)
-    color("red", "black")
+    colors = list(sorted(ESC_BACKGROUND_COLORS.keys()))
+    for x, bg in enumerate(colors):
+        text(2+x * 8, 2, "white", "black", bg.replace("bright-", "B")[:7].upper().ljust(7, " "))
+    for y, fg in enumerate(colors):
+        for x, bg in enumerate(colors):
+            text(2+x * 8, 3+y, fg, bg, fg.replace("bright-", "B")[:7].ljust(7, " "))
+    #gotoxy(1, 1)
+    color("bright-yellow", "black")
+    print "\n", colors
     #print "done",
     wait = readkeys.wait()

File tgui/ansiengine.py

+from tgui.ansiconsole import terminal_size, readkeys, gotoxy, text, hide_cursor, show_cursor, box
+
+
+def debug(*args):
+    print " ".join(map(str, args))
+
+class BaseEngine(object):
+
+    def palette(self):
+        from tgui import TPalette
+        return TPalette()
+
+    def display_text(self, text, width):
+        return text
+        from tgui import elide
+        if len(text) > width:
+            text = elide(text, width)
+        else:
+            text = text.ljust(width)
+        return text
+
+    def draw_window(self, x, y, width, height, pal=None):
+        from tgui import TPalette
+        if pal is None:
+            pal = TPalette()
+        self.rectangle(x, y, x + width, self.y + height, pal.window())
+
+    def draw_button(self, x, y, width, height, text,
+                    current=True, enabled=True, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).button()
+        self.text(x, y, u"< %s >" % self.display_text(text, text_width), color)
+
+    def draw_radiobutton(self, x, y, width, height, text, checked=True,
+                         current=True, enabled=True, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).text()
+        self.text(x, y, u"(%s) %s" % (checked and "*" or " ",
+                                     self.display_text(text, text_width)), color)
+
+    def draw_checkbox(self, x, y, width, height, text, checked=True,
+                         current=True, enabled=True, pal=None):
+        assert height == 1, "checkbox height is %s" % height
+        pal = pal or self.palette()
+        text_width = width - 4
+        color = (pal or self.palette()).text()
+        self.text(x, y, u"[%s] %s" % (checked and "x" or " ",
+                                     self.display_text(text, text_width)), color)
+
+    def draw_label(self, x, y, width, height, text, pal=None):
+        assert height == 1
+        pal = pal or self.palette()
+        color = pal.text()
+        self.text(x, y, self.display_text(text, width), color)
+
+    def fill_rect(self, x1, y1, width, height, char, color):
+        y2 = y1 + height - 1
+        x2 = x1 + width - 1
+        for y in range(y1, y2):
+            for x in range(x1, x2):
+                self.text(x, y, char, color)
+
+    def draw_desktop(self, x, y, width, height, pal=None, char=" "):
+        pal = pal or self.palette()
+        self.fill_rect(x, y, width, height, char, pal.desktop())
+
+
+
+
+class TPaintEngine(BaseEngine):
+    instance = None
+
+    def __init__(self, width=None, height=None):
+        if TPaintEngine.instance:
+            raise RuntimeError("Only one TPaintEngine instance allowed")
+        TPaintEngine.instance = self
+        term_width, term_height = terminal_size()
+        self.width = width or term_width
+        self.height = height or term_height
+        self._currentxy = (0, 0)
+
+    def currentxy(self):
+        return self._currentxy
+
+    def gotoxy(self, x, y):
+        gotoxy(x, y)
+        self._currentxy = (x, y)
+
+    def text(self, column, row, value, scheme):
+        color_pair = scheme.color_pair()
+        text(column, row, color_pair[0], color_pair[1], str(value))
+
+    def refresh(self):
+        pass
+
+    def rectangle(self, x1, y1, x2, y2, pal=None):
+        box(x1, y1, x2, y2)
+
+    def hide_cursor(self):
+        hide_cursor()
+
+    def show_cursor(self):
+        show_cursor()
+
+
+
+class TInputEngine(object):
+    instance = None
+
+    def __init__(self):
+        if TInputEngine.instance:
+            raise RuntimeError("Only one TInputEngine instance allowed")
+        TInputEngine.instance = self
+
+    def xreadkeys(self):
+        keys = readkeys.poll() or ""
+        if keys == "\x7f":
+            return True, "backspace"
+        if keys.startswith(chr(27)):
+            xkeys = keymap.get(keys[1:], None)
+            if xkeys:
+                return True, xkeys
+            elif len(keys) == 2:
+                return True, "meta %s" % keys[1]
+        elif len(keys) == 1:
+            code = ord(keys[0])
+            if code < 32 and code not in (9, 10):
+                return True, "ctrl %s" % chr(ord('A') + code - 1) + str(code)
+        return False, keys
+
+
+
+#intput_engine = TInputEngine()
+#paint_engine = TPaintEngine()

File tgui/base.py

 from tcore import TEvent, TPoint, dispatch, TRect
 from tgui.palette import TColorScheme
 # will be imported from other modules from here
-from tgui.cursesengine import TPaintEngine
+from tgui.ansiengine import TPaintEngine
 
 
 class TEventTypes:
     def __init__(self, value):
         self.value = value
 
-TColor.GRAY = TColor("light gray")
+TColor.GRAY = TColor("white")
 TColor.BLACK = TColor("black")
-TColor.WHITE = TColor("white")
-TColor.RED = TColor("dark red")
-TColor.GREEN = TColor("dark green")
-TColor.BLUE = TColor("dark blue")
-TColor.DARKGRAY = TColor("dark gray")
+TColor.WHITE = TColor("bright-white")
+TColor.RED = TColor("red")
+TColor.GREEN = TColor("green")
+TColor.BLUE = TColor("blue")
+TColor.DARKGRAY = TColor("bright-black")
 
 
 class TPalette(object):

File tgui/palette.py

 # -*- coding: utf-8 -*-
-import curses
-
-colors = {
-    'default': -1,
-    'black':          curses.COLOR_BLACK,
-    'dark red':       curses.COLOR_RED,
-    'dark green':     curses.COLOR_GREEN,
-    'brown':          curses.COLOR_YELLOW,
-    'dark blue':      curses.COLOR_BLUE,
-    'dark magenta':   curses.COLOR_MAGENTA,
-    'dark cyan':      curses.COLOR_CYAN,
-    'light gray':     curses.COLOR_WHITE,
-    'dark gray':      curses.COLOR_BLACK,
-    'light red':      curses.COLOR_RED,
-    'light green':    curses.COLOR_GREEN,
-    'yellow':         curses.COLOR_YELLOW,
-    'light blue':     curses.COLOR_BLUE,
-    'light magenta':  curses.COLOR_MAGENTA,
-    'light cyan':     curses.COLOR_CYAN,
-    'white':          curses.COLOR_WHITE,
-}
 
 
 class TColorScheme(object):
     registered = {}
 
     def __init__(self, foreground, background):
-        key = (foreground, background)
-        found = self.registered.get(key, None)
-        if not found:
-            self.num = len(self.registered) + 1
-            curses.start_color()
-            curses.init_pair(self.num, colors[foreground], colors[background])
-            self.registered[key] = self.num
-        else:
-            self.num = found
+        self._pair = (foreground, background)
 
     def color_pair(self):
-        return curses.color_pair(self.num)
+        return self._pair
 

File tgui/tapplication.py

 from tcore import TObject, Signal, TEventLoop, TPoint, TSize, Property
 from tgui.base import TKeyEvent, TFocusEvent, TEventTypes, TPainter
 from tgui.twidget import TWidget
-from tgui.cursesengine import TPaintEngine, TInputEngine
+from tgui.ansiengine import TPaintEngine, TInputEngine
 
 
 class TInputContext(TObject):