Primer contacto con curses, estoy aprendiendo
authordanigm <danigm@arch_box>
Thu, 28 Aug 2008 22:39:34 +0000 (00:39 +0200)
committerdanigm <danigm@arch_box>
Thu, 28 Aug 2008 22:39:34 +0000 (00:39 +0200)
sweetterm.py [new file with mode: 0755]

diff --git a/sweetterm.py b/sweetterm.py
new file mode 100755 (executable)
index 0000000..18f256f
--- /dev/null
@@ -0,0 +1,81 @@
+#!/usr/bin/python
+
+API = 'B87G50SksB23C3t417d6'
+SERVER = 'http://localhost:8080'
+
+import time
+import curses
+from pysweetter import Sweetter
+
+class Sweetterm:
+    def __init__(self, apikey='', server=''):
+        # Starting curses
+        self.stdscr = curses.initscr()
+        curses.start_color()
+        curses.echo()
+        self.sweetter = Sweetter(apikey, server)
+
+        try:
+            self.run()
+
+        except Exception, inst:
+            curses.nocbreak(); self.stdscr.keypad(0); curses.echo()
+            curses.endwin()
+            raise inst
+
+        # Ending curses
+        curses.nocbreak(); self.stdscr.keypad(0); curses.echo()
+        curses.endwin()
+
+    def run(self):
+        c_width = 20
+        begin_x = 0 ; begin_y = curses.LINES - 5
+        height = 5 ; width = curses.COLS - c_width
+        text_border = curses.newwin(height, width, begin_y, begin_x)
+        textbox = curses.newwin(height - 2, width - 2, begin_y + 1, begin_x + 1)
+
+        counter_border = curses.newwin(height, c_width - 2, begin_y, width + 1) 
+        counter = curses.newwin(height - 2, c_width - 4, begin_y + 1,
+                width + 2)
+
+        counter_border.border(); counter_border.refresh()
+        text_border.border(); text_border.refresh()
+        text = ''
+        count = 20
+
+        # Initializing counter
+        counter.addstr(str(count))
+        counter.refresh()
+        while 1:
+            c = textbox.getch()
+            if c == ord('z'): break
+            elif c == 10:
+                textbox.clear()
+                textbox.refresh()
+
+            elif c == 127:
+                if count >= 20:
+                    continue
+                count += 1
+                counter.clear()
+                counter.addstr(str(count))
+                counter.refresh()
+                text = text[0:-1]
+                textbox.clear()
+                textbox.addstr(text)
+                textbox.refresh()
+                
+            elif count > 0:
+                count -= 1
+                counter.clear()
+                counter.addstr(str(count))
+                counter.refresh()
+                text += chr(c)
+                #textbox.clear()
+
+            elif count == 0:
+                curses.echo(False)
+
+if __name__ == '__main__':
+    Sweetterm(API, SERVER)
+