From 1e604f2408861120d8168a3f88c29f9da3ddf7df Mon Sep 17 00:00:00 2001 From: makefu Date: Tue, 19 Jul 2011 20:52:28 +0200 Subject: added initial version of cholerab-live --- cholerab/cholerab-live/view.py | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 cholerab/cholerab-live/view.py (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py new file mode 100644 index 00000000..34891e5c --- /dev/null +++ b/cholerab/cholerab-live/view.py @@ -0,0 +1,71 @@ +#!/usr/bin/python2 + +from curses import * +import socket +import threading + +class CursesView(threading.Thread): + def stop(self): + self.running = False + nocbreak(); self.scr.keypad(0); echo() + endwin() + + def run(self): + self.running = True + def try_move(x,y): + if x >= self.width: x = 0;y = y+1 + if x < 0 : x = self.width-1; y= y-1 + if y >= self.height : x = x+1;y = 0 + if y < 0 : x = x-1; y = self.height-1 + self.win.move(y,x); return x,y + + while self.running: + c = self.scr.getch() + if c == KEY_LEFT : self.x -=1 + elif c == KEY_RIGHT : self.x +=1 + elif c == KEY_UP : self.y -=1 + elif c == KEY_DOWN : self.y +=1 + elif c == ord('q') : self.stop() + else : + try: + self.win.addch(c) #TODO UTF8 here + #self.cholerab.write_char(self.x,self.y,c) + except: + pass + self.x +=1 + self.x,self.y = try_move(self.x,self.y) + self.refresh() + + def write_char(self,x,y,char): + self.win.addch(y,x,char) + self.win.move(self.y,self.x) + self.refresh() + def refresh(self): + self.scr.refresh() + self.win.refresh() + def clear(self): + pass + def write_field(self,ar): + """ + writes the whole field with given 2-dimensional array + """ + self.clear() + pass + + + def __init__(self,height=24,width=80,cholerab=None): + self.cholerab = cholerab + threading.Thread.__init__(self) + self.scr = initscr() + noecho() + cbreak() + self.scr.keypad(1) + curs_set(2) + + begin_x = 0;begin_y = 0 + self.height = height + self.width = width + self.x = 0 ; self.y = 0 + + self.win = newwin(height,width,begin_y,begin_x) + -- cgit v1.2.3 From 82fda4544b2d3a04a2c67d8af2e11ddce0c61195 Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 21 Jul 2011 00:37:13 +0200 Subject: fixed cur_set bug/added todo --- cholerab/cholerab-live/view.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index 34891e5c..b798e712 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -26,6 +26,7 @@ class CursesView(threading.Thread): elif c == KEY_UP : self.y -=1 elif c == KEY_DOWN : self.y +=1 elif c == ord('q') : self.stop() + #TODO handle backspace correctly else : try: self.win.addch(c) #TODO UTF8 here @@ -57,10 +58,13 @@ class CursesView(threading.Thread): self.cholerab = cholerab threading.Thread.__init__(self) self.scr = initscr() - noecho() + noecho() cbreak() self.scr.keypad(1) - curs_set(2) + try: + curs_set(2) + except: # go home with your non-standard terminals! + pass begin_x = 0;begin_y = 0 self.height = height -- cgit v1.2.3 From 531d9d4309fa09e893a19fdcf8165dc7bf1b3731 Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 02:41:13 +0200 Subject: added curses stuff cholerab-live/view.py: now able to write basic utf-8 on the plane, handles some rudimentary colors handles backspace correctly --- cholerab/cholerab-live/view.py | 142 ++++++++++++++++++++++++----------------- 1 file changed, 82 insertions(+), 60 deletions(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index b798e712..fe90b926 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -3,73 +3,95 @@ from curses import * import socket import threading +import logging +log = logging.getLogger('cholerab-curses') class CursesView(threading.Thread): - def stop(self): - self.running = False - nocbreak(); self.scr.keypad(0); echo() - endwin() + def stop(self): + self.running = False + nocbreak(); self.scr.keypad(0); echo() + endwin() - def run(self): - self.running = True - def try_move(x,y): - if x >= self.width: x = 0;y = y+1 - if x < 0 : x = self.width-1; y= y-1 - if y >= self.height : x = x+1;y = 0 - if y < 0 : x = x-1; y = self.height-1 - self.win.move(y,x); return x,y + def run(self): + """ + input loop - while self.running: - c = self.scr.getch() - if c == KEY_LEFT : self.x -=1 - elif c == KEY_RIGHT : self.x +=1 - elif c == KEY_UP : self.y -=1 - elif c == KEY_DOWN : self.y +=1 - elif c == ord('q') : self.stop() - #TODO handle backspace correctly - else : - try: - self.win.addch(c) #TODO UTF8 here - #self.cholerab.write_char(self.x,self.y,c) - except: - pass - self.x +=1 - self.x,self.y = try_move(self.x,self.y) - self.refresh() + TODO add Unicode Input Support + """ + self.running = True + def try_move(x,y): + if x >= self.width: x = 0;y = y+1 + if x < 0 : x = self.width-1; y= y-1 + if y >= self.height : x = x+1;y = 0 + if y < 0 : x = x-1; y = self.height-1 + self.win.move(y,x); return x,y - def write_char(self,x,y,char): - self.win.addch(y,x,char) - self.win.move(self.y,self.x) - self.refresh() - def refresh(self): - self.scr.refresh() - self.win.refresh() - def clear(self): - pass - def write_field(self,ar): - """ - writes the whole field with given 2-dimensional array - """ - self.clear() - pass + while self.running: + c = self.scr.getch() #get_char(self.scr) + log.debug(str(c)) + if c == KEY_LEFT : self.x -=1 + elif c == KEY_RIGHT : self.x +=1 + elif c == KEY_UP : self.y -=1 + elif c == KEY_DOWN : self.y +=1 + elif c == ord('q') : self.stop() + elif c == 127: + log.info('backspace pressed') + self.x -=1; + self.x,self.y = try_move(self.x,self.y) + self.win.addch(' ') + #TODO handle backspace correctly + else : + try: + self.win.addch(c) #TODO UTF8 here + #self.cholerab.write_char(self.x,self.y,c) + except: + pass + self.x +=1 + self.x,self.y = try_move(self.x,self.y) + self.refresh() - def __init__(self,height=24,width=80,cholerab=None): - self.cholerab = cholerab - threading.Thread.__init__(self) - self.scr = initscr() - noecho() - cbreak() - self.scr.keypad(1) - try: - curs_set(2) - except: # go home with your non-standard terminals! - pass + def write_char(self,x,y,char): + self.win.addch(y,x,char) + self.win.move(self.y,self.x) + self.refresh() + def write_str(self,x,y,string,user=1): + self.win.addstr(y,x,string,color_pair(user)) + self.win.move(self.y,self.x) + self.refresh() + def refresh(self): + self.scr.refresh() + self.win.refresh() + def clear(self): + pass + def write_field(self,ar): + """ + writes the whole field with given 2-dimensional array + """ + self.clear() + pass - begin_x = 0;begin_y = 0 - self.height = height - self.width = width - self.x = 0 ; self.y = 0 - self.win = newwin(height,width,begin_y,begin_x) + def __init__(self,height=24,width=80,cholerab=None,scr=None): + init_pair(1,COLOR_WHITE,COLOR_BLACK) + init_pair(2,COLOR_RED,COLOR_BLACK) + init_pair(3,COLOR_GREEN,COLOR_BLACK) + self.cholerab = cholerab + threading.Thread.__init__(self) + if scr: + self.scr = scr + else: + self.scr = initscr() + noecho() + cbreak() + self.scr.keypad(1) + try: curs_set(2) + except: pass # go home with your non-standard terminals! + + begin_x = 0;begin_y = 0 + self.height = height + self.width = width + self.x = 0 ; self.y = 0 + + self.win = newwin(height,width,begin_y,begin_x) -- cgit v1.2.3 From f1f12489514f47f9154f8e4b6232399e747cfcd8 Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 04:13:44 +0200 Subject: emergency commit --- cholerab/cholerab-live/view.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index fe90b926..3d5cd873 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -7,10 +7,21 @@ import logging log = logging.getLogger('cholerab-curses') class CursesView(threading.Thread): + def addch(self,char): + """ + adds a char at the current cursor position + abstraction to the curses win.addch() + """ + try: self.win.addch(char) + except: pass + self.cholerab.send_char(self.x,self.y,chr(char)) def stop(self): + #TODO setting back the whole terminal currently does not work correctly, fix me harder self.running = False + self.clear() + self.win.refresh() nocbreak(); self.scr.keypad(0); echo() - endwin() + #endwin() def run(self): """ @@ -28,7 +39,7 @@ class CursesView(threading.Thread): while self.running: c = self.scr.getch() #get_char(self.scr) - log.debug(str(c)) + #TODO UTF8 here, get_wch not yet implemented if c == KEY_LEFT : self.x -=1 elif c == KEY_RIGHT : self.x +=1 elif c == KEY_UP : self.y -=1 @@ -39,19 +50,14 @@ class CursesView(threading.Thread): self.x -=1; self.x,self.y = try_move(self.x,self.y) self.win.addch(' ') - - #TODO handle backspace correctly else : - try: - self.win.addch(c) #TODO UTF8 here - #self.cholerab.write_char(self.x,self.y,c) - except: - pass + self.addch(c) self.x +=1 self.x,self.y = try_move(self.x,self.y) self.refresh() - def write_char(self,x,y,char): + def write_char(self,x,y,char,user=1): + user = user % 3 + 1 self.win.addch(y,x,char) self.win.move(self.y,self.x) self.refresh() @@ -63,6 +69,7 @@ class CursesView(threading.Thread): self.scr.refresh() self.win.refresh() def clear(self): + self.win.clear() pass def write_field(self,ar): """ @@ -71,17 +78,19 @@ class CursesView(threading.Thread): self.clear() pass - def __init__(self,height=24,width=80,cholerab=None,scr=None): - init_pair(1,COLOR_WHITE,COLOR_BLACK) - init_pair(2,COLOR_RED,COLOR_BLACK) - init_pair(3,COLOR_GREEN,COLOR_BLACK) - self.cholerab = cholerab - threading.Thread.__init__(self) + # TODO handle sessions somehow if scr: self.scr = scr else: self.scr = initscr() + start_color() + init_pair(1,COLOR_WHITE,COLOR_BLACK) + init_pair(2,COLOR_RED,COLOR_BLACK) + init_pair(3,COLOR_GREEN,COLOR_BLACK) + init_pair(3,COLOR_CYAN,COLOR_BLACK) + threading.Thread.__init__(self) + self.cholerab = cholerab noecho() cbreak() @@ -95,3 +104,4 @@ class CursesView(threading.Thread): self.x = 0 ; self.y = 0 self.win = newwin(height,width,begin_y,begin_x) + self.clear() -- cgit v1.2.3 From 2862f1ad4ef0439721779a1a93e29bc5dc1c84de Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 04:56:05 +0200 Subject: added robustness, removed utf8 support to be able to send special characters over the line the characters are translated into their number value and written as string. this essentially breaks utf-8 support for the clients, needs to be fixed some time later added ignore for own messages, message colorization --- cholerab/cholerab-live/view.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index 3d5cd873..29608a7d 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -12,7 +12,7 @@ class CursesView(threading.Thread): adds a char at the current cursor position abstraction to the curses win.addch() """ - try: self.win.addch(char) + try: self.win.addch(char,2) except: pass self.cholerab.send_char(self.x,self.y,chr(char)) def stop(self): @@ -49,7 +49,11 @@ class CursesView(threading.Thread): log.info('backspace pressed') self.x -=1; self.x,self.y = try_move(self.x,self.y) - self.win.addch(' ') + self.addch(ord(' ')) + elif c == ord('\n'): + log.info('enter pressed') + self.y +=1; + self.x,self.y = try_move(self.x,self.y) else : self.addch(c) self.x +=1 @@ -58,7 +62,7 @@ class CursesView(threading.Thread): def write_char(self,x,y,char,user=1): user = user % 3 + 1 - self.win.addch(y,x,char) + self.win.addch(y,x,char,color_pair(user)) self.win.move(self.y,self.x) self.refresh() def write_str(self,x,y,string,user=1): -- cgit v1.2.3 From 1a0324c732577fa703c006f8188fc424c17f66e0 Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 05:02:29 +0200 Subject: fixed wrong displaying in output window --- cholerab/cholerab-live/view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index 29608a7d..01b53b8c 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -12,7 +12,7 @@ class CursesView(threading.Thread): adds a char at the current cursor position abstraction to the curses win.addch() """ - try: self.win.addch(char,2) + try: self.win.addch(char) except: pass self.cholerab.send_char(self.x,self.y,chr(char)) def stop(self): -- cgit v1.2.3 From 55198bc5ecc2e46907ad67a62f5ccae5c662d0c7 Mon Sep 17 00:00:00 2001 From: makefu Date: Sun, 24 Jul 2011 05:06:59 +0200 Subject: fixed backspace bug on krebs... hopefully? --- cholerab/cholerab-live/view.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'cholerab/cholerab-live/view.py') diff --git a/cholerab/cholerab-live/view.py b/cholerab/cholerab-live/view.py index 01b53b8c..6a75f655 100644 --- a/cholerab/cholerab-live/view.py +++ b/cholerab/cholerab-live/view.py @@ -40,12 +40,13 @@ class CursesView(threading.Thread): while self.running: c = self.scr.getch() #get_char(self.scr) #TODO UTF8 here, get_wch not yet implemented + log.debug("Pressed : %d" % c) if c == KEY_LEFT : self.x -=1 elif c == KEY_RIGHT : self.x +=1 elif c == KEY_UP : self.y -=1 elif c == KEY_DOWN : self.y +=1 elif c == ord('q') : self.stop() - elif c == 127: + elif c == 127 or c == KEY_BACKSPACE: log.info('backspace pressed') self.x -=1; self.x,self.y = try_move(self.x,self.y) -- cgit v1.2.3