Add bility to jump to bottom in msgs

Add support for arrow keys
This commit is contained in:
Paul Nameless 2019-02-18 20:26:15 +01:00
parent 73065f5f45
commit e4ead029a0
3 changed files with 77 additions and 54 deletions

View file

@ -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()
if not update['disable_notification']:
try:
notify(update['message']['content']['text']['text'])
except Exception:
logger.exception(
'Error happened on notify: %s', update['message'])
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()

View file

@ -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

84
view.py
View file

@ -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:
@ -127,7 +134,8 @@ class ChatView:
# 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 = 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)