diff options
author | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-01-27 19:46:08 +0000 |
---|---|---|
committer | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-01-27 19:46:08 +0000 |
commit | 9275ded3338b6f1fa356aaa4ee306dcb1e20b019 (patch) | |
tree | f1973ac2e25be977cbd964a10302d6481ca96860 | |
parent | 038038b728f7e6c4aaf5e24c85bf7047532a3aad (diff) | |
download | poezio-9275ded3338b6f1fa356aaa4ee306dcb1e20b019.tar.gz poezio-9275ded3338b6f1fa356aaa4ee306dcb1e20b019.tar.bz2 poezio-9275ded3338b6f1fa356aaa4ee306dcb1e20b019.tar.xz poezio-9275ded3338b6f1fa356aaa4ee306dcb1e20b019.zip |
historique, edition de ligne correcte, affichage du texte OK, gestion des caracteres unicodes (reception et envoie) et d'autres trucs
-rw-r--r-- | src/client.py | 2 | ||||
-rw-r--r-- | src/gui.py | 67 | ||||
-rw-r--r-- | src/window.py | 77 |
3 files changed, 103 insertions, 43 deletions
diff --git a/src/client.py b/src/client.py index ccc2b7b4..31792512 100644 --- a/src/client.py +++ b/src/client.py @@ -25,6 +25,8 @@ from handler import Handler from gui import Gui from curses import wrapper, initscr +sys.stderr = open('/dev/null', 'a') + class Client(object): """ Main class @@ -69,12 +69,7 @@ class Room(object): if not msg: logger.info('msg is None..., %s' % (nick)) return - # lines = msg.split('\n') - # first line has the nick and timestamp but others don't self.lines.append((datetime.now(), nick.encode('utf-8'), msg.encode('utf-8'))) - # if len(lines) > 0: - # for line in lines: - # self.lines.append((line.encode('utf-8'))) def add_info(self, info): """ info, like join/quit/status messages""" @@ -130,12 +125,40 @@ class Gui(object): 'prev': self.rotate_rooms_right, } + self.key_func = { + "KEY_LEFT": self.window.input.key_left, + "KEY_RIGHT": self.window.input.key_right, + "KEY_UP": self.window.input.key_up, + "KEY_END": self.window.input.key_end, + "KEY_HOME": self.window.input.key_home, + "KEY_DOWN": self.window.input.key_down, + "KEY_BACKSPACE": self.window.input.key_backspace + } + self.handler = Handler() self.handler.connect('on-connected', self.on_connected) self.handler.connect('join-room', self.join_room) self.handler.connect('room-presence', self.room_presence) self.handler.connect('room-message', self.room_message) + def main_loop(self, stdscr): + while 1: + curses.doupdate() + key = stdscr.getkey() + if key == curses.KEY_RESIZE: + self.window.resize(stdscr) + elif str(key) in self.key_func.keys(): + self.key_func[key]() + elif ord(key) == 10: + self.execute() + else: + if ord(key) > 190 and ord(key) < 225: + key = key+stdscr.getkey() + elif ord(key) == 226: + key = key+stdscr.getkey() + key = key+stdscr.getkey() + self.window.do_command(key) + def current_room(self): return self.rooms[0] @@ -148,6 +171,7 @@ class Gui(object): def init_curses(self, stdscr): curses.start_color() curses.noecho() + stdscr.keypad(True) curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) # Admin @@ -209,11 +233,11 @@ class Gui(object): if not room: return logger.warning("presence received for a non-existing room: %s" % (name)) msg = room.on_presence(stanza, from_nick) - self.window.text_win.add_line(room, (datetime.now(), msg)) if room == self.current_room(): - self.window.text_win.refresh(room.name) - self.window.user_win.refresh(room.users) - curses.doupdate() + self.window.text_win.add_line(room, (datetime.now(), msg)) + self.window.text_win.refresh(room.name) + self.window.user_win.refresh(room.users) + curses.doupdate() def execute(self): line = self.window.input.get_text() @@ -239,28 +263,3 @@ class Gui(object): def command_quit(self, args): self.reset_curses() sys.exit() - - def main_loop(self, stdscr): - while 1: - curses.doupdate() - key = stdscr.getkey() - if ord(key) == 195: - n = stdscr.getkey() - key = key+n - self.window.input.win.addstr(key) - self.window.input.add_char(key) - self.window.input.win.refresh() - elif ord(key) == 226: - n = stdscr.getkey() - m = stdscr.getkey() - key = key+n+m - self.window.input.win.addstr(key) - self.window.input.add_char(key) - self.window.input.win.refresh() - - elif key == curses.KEY_RESIZE: - self.window.resize(stdscr) - elif ord(key) == 10: - self.execute() - else: - self.window.do_command(ord(key)) diff --git a/src/window.py b/src/window.py index 5699151f..97c1f6d0 100644 --- a/src/window.py +++ b/src/window.py @@ -162,10 +162,12 @@ class Input(Win): """ def __init__(self, height, width, y, x, stdscr): Win.__init__(self, height, width, y, x, stdscr) - self.input = curses.textpad.Textbox(self.win) - self.input.insert_mode = True - self.win.keypad(True) - self.text = '' + # self.input = curses.textpad.Textbox(self.win) + # self.input.insert_mode = True + self.history = [] + self.text = u'' + self.pos = 0 + self.histo_pos = 0 def resize(self, height, width, y, x, stdscr): self._resize(height, width, y, x, stdscr) @@ -173,16 +175,73 @@ class Input(Win): self.input.insert_mode = True self.win.clear() - def add_char(self, char): - self.text += char + def key_up(self): + self.win.clear() + if self.histo_pos >= 0: + self.histo_pos -= 1 + self.win.addstr(self.history[self.histo_pos+1]) + self.text = self.history[self.histo_pos+1] + self.pos = len(self.text) + self.refresh() + + def key_down(self): + self.win.clear() + if self.histo_pos < len(self.history)-1: + self.histo_pos += 1 + self.win.addstr(self.history[self.histo_pos]) + self.text = self.history[self.histo_pos] + self.pos = len(self.text) + else: + self.histo_pos = len(self.history)-1 + self.text = u'' + self.pos = 0 + self.refresh() + + def key_home(self): + self.pos = 0 + self.win.move(0, 0) + self.refresh() + + def key_end(self): + self.pos = len(self.text) + self.win.move(0, len(self.text)) + self.refresh() + + def key_left(self): + (y, x) = self.win.getyx() + if self.pos > 0: + self.pos -= 1 + self.win.move(y, x-1) + self.refresh() + + def key_right(self): + (y, x) = self.win.getyx() + if self.pos < len(self.text): + self.pos += 1 + self.win.move(y, x+1) + self.refresh() + + def key_backspace(self): + (y, x) = self.win.getyx() + if len(self.text) > 0 and self.pos != 0: + self.text = self.text[:self.pos-1]+self.text[self.pos:] + self.pos -= 1 + self.win.delch(y, x-1) + self.refresh() def do_command(self, key): - self.text += chr(key) - self.input.do_command(key) + (y, x) = self.win.getyx() + self.text = self.text[:self.pos]+key.decode('utf-8')+self.text[self.pos:] + self.win.insstr(key) + self.win.move(y, x+1) + self.pos += 1 def get_text(self): txt = self.text - self.text = '' + self.text = u'' + self.pos = 0 + self.history.append(txt) + self.histo_pos = len(self.history)-1 return txt def save_text(self): |