From e4ead029a0321a2321de955133d8b9aa7dfac4b9 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Mon, 18 Feb 2019 20:26:15 +0100 Subject: [PATCH] Add bility to jump to bottom in msgs Add support for arrow keys --- controller.py | 31 ++++++++++-------- model.py | 10 ++++++ view.py | 90 ++++++++++++++++++++++++++++----------------------- 3 files changed, 77 insertions(+), 54 deletions(-) diff --git a/controller.py b/controller.py index 59e5541..ddca813 100644 --- a/controller.py +++ b/controller.py @@ -51,14 +51,18 @@ class Controller: elif key == 'K': if self.model.jump_prev_msg(): self.refresh_msgs() - elif key == 'j': + elif key in ('j', '^B'): if self.model.next_msg(): self.refresh_msgs() - elif key == 'k': + elif key in ('k', '^C'): if self.model.prev_msg(): self.refresh_msgs() elif key == 'G': - # move to the bottom + if self.model.jump_bottom(): + self.refresh_msgs() + + elif key == '/': + # search pass elif key == 'gg': # move to the top @@ -82,8 +86,9 @@ class Controller: chat_id=chat_id, text=msg, ) + self.view.draw_status(f'Sent: {msg}') - elif key == 'h': + elif key in ('h', '^D'): return 'BACK' def handle_chats(self): @@ -98,7 +103,7 @@ class Controller: logger.info('Pressed key: %s', key) if key == 'q': return - elif key == 'l': + elif key in ('l', '^E'): rc = self.handle_msgs() if rc == 'QUIT': return @@ -106,12 +111,12 @@ class Controller: self.view.msgs.resize(0.5) self.refresh_chats() - elif key == 'j': + elif key in ('j', '^B'): is_changed = self.model.next_chat() if is_changed: self.refresh_chats() - elif key == 'k': + elif key in ('k', '^C'): is_changed = self.model.prev_chat() if is_changed: self.refresh_chats() @@ -136,13 +141,13 @@ class Controller: # with self.lock: chat_id = update['message']['chat_id'] self.model.msgs.msgs[chat_id].append(update['message']) - msgs = self.model.get_current_msgs() + # msgs = self.model.get_current_msgs() self.refresh_msgs() - try: - notify(update['message']['content']['text']['text']) - except Exception: - logger.exception( - 'Error happened on notify: %s', update['message']) + if not update['disable_notification']: + try: + notify(update['message']['content']['text']['text']) + except Exception: + logger.exception('Error happened on notify: %s', update) # message_content = update['message']['content'].get('text', {}) # we need this because of different message types: photos, files, etc. # message_text = message_content.get('text', '').lower() diff --git a/model.py b/model.py index aa8e510..ac1e805 100644 --- a/model.py +++ b/model.py @@ -18,6 +18,10 @@ class Model: def get_current_msg(self): return self.msgs.current_msgs[self.get_current_chat_id()] + def jump_bottom(self): + chat_id = self.chats.chat_ids[self.current_chat] + return self.msgs.jump_bottom(chat_id) + def next_chat(self): if self.current_chat < len(self.chats.chats): self.current_chat += 1 @@ -145,6 +149,12 @@ class MsgModel: return True return False + def jump_bottom(self, chat_id): + if self.current_msgs[chat_id] == 0: + return False + self.current_msgs[chat_id] = 0 + return True + def jump_next_msg(self, chat_id): if self.current_msgs[chat_id] - 10 > 0: self.current_msgs[chat_id] -= 10 diff --git a/view.py b/view.py index c2165b0..ef039fd 100644 --- a/view.py +++ b/view.py @@ -52,41 +52,7 @@ class View: # return curses.unctrl(ch).decode() def get_input(self): - curses.curs_set(1) - - buff = '' - while True: - key = self.msgs.win.get_wch( - self.msgs.h-1, min(len(buff), self.msgs.w-1)) - key = ord(key) - logger.info('Pressed in send msg: "%s"', key) - # try: - logger.info('Trying to chr: %s', chr(key)) - # except ValueError: - # logger.exception() - if key == 10: # return - logger.info('Sending msg: %s', buff) - break - elif key == 127: # del - if buff: - buff = buff[:-1] - elif key == 7: # ^G cancel - logger.info('Not Sending msg: %s', buff) - buff = None - break - elif chr(key).isprintable(): - buff += chr(key) - if len(buff) >= self.msgs.w: - start = len(buff) - self.msgs.w - buff_wrapped = buff[start+1:] - else: - buff_wrapped = (buff + ' ' * (self.msgs.w - - len(buff) - 1)) - self.msgs.win.addstr(self.msgs.h-1, 0, buff_wrapped) - self.msgs.win.move(self.msgs.h-1, min(len(buff), self.msgs.w-1)) - - curses.curs_set(0) - return buff + return self.status.get_input() class StatusView: @@ -105,8 +71,49 @@ class StatusView: self.win.wmove(self.y, self.x) def draw(self, msg): - msg = '-' * (self.w - 1) - self.win.addstr(0, 0, msg) + # msg = '-' * (self.w - 1) + # msg = '>' + if not msg: + msg = 'Status' + self.win.addstr(0, 0, msg[:self.w]) + self.win.refresh() + + def get_input(self): + curses.curs_set(1) + self.win.clear() + + buff = '' + while True: + key = self.win.get_wch(0, min(len(buff), self.w-1)) + key = ord(key) + logger.info('Pressed in send msg: "%s"', key) + # try: + logger.info('Trying to chr: %s', chr(key)) + # except ValueError: + # logger.exception() + if key == 10: # return + logger.info('Sending msg: %s', buff) + break + elif key == 127: # del + if buff: + buff = buff[:-1] + elif key == 7: # ^G cancel + logger.info('Not Sending msg: %s', buff) + buff = None + break + elif chr(key).isprintable(): + buff += chr(key) + if len(buff) >= self.w: + start = len(buff) - self.w + buff_wrapped = buff[start+1:] + else: + buff_wrapped = (buff + ' ' * (self.w - + len(buff) - 1)) + self.win.addstr(0, 0, buff_wrapped) + self.win.move(0, min(len(buff), self.w-1)) + + curses.curs_set(0) + return buff class ChatView: @@ -126,8 +133,9 @@ class ChatView: self.win.clear() # self.win.vline(0, self.w-1, curses.ACS_VLINE, self.h) for i, chat in enumerate(chats): - msg = f'{get_date(chat)} {chat["title"]} [{chat["unread_count"]}]: {get_last_msg(chat)}' - msg = emoji_pattern.sub(r'', msg)[:self.w-1] + msg = f' {get_date(chat)} {chat["title"]} [{chat["unread_count"]}]: {get_last_msg(chat)}' + # msg = emoji_pattern.sub(r'', msg)[:self.w-1] + msg = emoji_pattern.sub(r'', msg)[:self.w-2] + ' ' # msg = msg[:self.w-1] if len(msg) < self.w: msg += ' ' * (self.w - len(msg) - 1) @@ -194,13 +202,13 @@ class MsgView: msg['date']).strftime("%H:%M:%S") _type = msg['@type'] if _type == 'message': - return "{} {}: {}".format( + return " {} {}: {}".format( dt, msg['sender_user_id'], parse_content(msg['content']) ) logger.debug('Unknown message type: %s', msg) - return 'unknown msg type: ' + str(msg['content']) + return ' unknown msg type: ' + str(msg['content']) def get_last_msg(chat):