Sort message newest at the bottom

Jump with H/J keys 10 msgs up/down
This commit is contained in:
Paul Nameless 2019-02-18 19:49:01 +01:00
parent 5aa9a73f16
commit 73065f5f45
3 changed files with 39 additions and 13 deletions

View file

@ -29,8 +29,8 @@ class Controller:
def handle_msgs(self):
# set width to 0.25, move window to left
# refresh everything
self.view.chats.resize(0.25)
self.view.msgs.resize(0.25)
self.view.chats.resize(0.2)
self.view.msgs.resize(0.2)
self.refresh_chats()
while True:
@ -45,6 +45,12 @@ class Controller:
elif key == '[':
if self.model.prev_chat():
self.refresh_chats()
elif key == 'J':
if self.model.jump_next_msg():
self.refresh_msgs()
elif key == 'K':
if self.model.jump_prev_msg():
self.refresh_msgs()
elif key == 'j':
if self.model.next_msg():
self.refresh_msgs()
@ -119,7 +125,7 @@ class Controller:
self.view.draw_status()
def refresh_msgs(self):
msgs = self.model.get_current_msgs()
msgs = self.model.get_current_msgs(limit=self.view.msgs.h)
self.view.draw_msgs(self.model.get_current_msg(), msgs)
def update_handler(self, update):

View file

@ -38,6 +38,14 @@ class Model:
chat_id = self.chats.chat_ids[self.current_chat]
return self.msgs.prev_msg(chat_id)
def jump_next_msg(self):
chat_id = self.chats.chat_ids[self.current_chat]
return self.msgs.jump_next_msg(chat_id)
def jump_prev_msg(self):
chat_id = self.chats.chat_ids[self.current_chat]
return self.msgs.jump_prev_msg(chat_id)
def get_chats(self, offset=0, limit=10):
return self.chats.get_chats(offset=offset, limit=limit)
@ -137,6 +145,19 @@ class MsgModel:
return True
return False
def jump_next_msg(self, chat_id):
if self.current_msgs[chat_id] - 10 > 0:
self.current_msgs[chat_id] -= 10
else:
self.current_msgs[chat_id] = 0
return True
def jump_prev_msg(self, chat_id):
if self.current_msgs[chat_id] + 10 < len(self.msgs[chat_id]):
self.current_msgs[chat_id] += 10
return True
return False
def prev_msg(self, chat_id):
if self.current_msgs[chat_id] < len(self.msgs[chat_id]):
self.current_msgs[chat_id] += 1
@ -145,7 +166,7 @@ class MsgModel:
def get_msgs(self, chat_id, offset=0, limit=10):
if offset + limit < len(self.msgs[chat_id]):
return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit][::-1]
return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit]
for i in range(3):
if len(self.msgs[chat_id]):
@ -167,7 +188,7 @@ class MsgModel:
if len(self.msgs[chat_id]) >= offset + limit:
break
return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit][::-1]
return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit]
class UserModel:

15
view.py
View file

@ -105,8 +105,8 @@ class StatusView:
self.win.wmove(self.y, self.x)
def draw(self, msg):
msg = ' ' * (self.w - 1)
self.win.addstr(0, 0, msg, curses.A_REVERSE)
msg = '-' * (self.w - 1)
self.win.addstr(0, 0, msg)
class ChatView:
@ -151,8 +151,6 @@ class MsgView:
self.x = 0
# self.win = stdscr.subwin(self.h, self.w, 0, self.x)
self.win = None
# self.win.scrollok(True)
# self.win.idlok(True)
self.lines = 0
def resize(self, p=0.5):
@ -162,6 +160,8 @@ class MsgView:
# if self.win is None:
self.win = self.stdscr.subwin(self.h, self.w, 0, self.x)
# self.win.scrollok(True)
# self.win.idlok(True)
# else:
# self.win.resize(self.h, self.w)
# self.win.mvwin(0, self.x)
@ -169,8 +169,7 @@ class MsgView:
def draw(self, current, msgs):
logger.info('Dwaring msgs')
self.win.clear()
count = 0
current = len(msgs) - current - 1
count = self.h
for i, msg in enumerate(msgs):
s = self._parse_msg(msg)
@ -178,14 +177,14 @@ class MsgView:
if len(s) < self.w:
s += ' ' * (self.w - len(s) - 1)
offset = math.ceil(len(s) / self.w)
if count + offset > self.h-1:
count -= offset
if count <= 0:
logger.warning('Reched end of lines')
break
if i == current:
self.win.addstr(count, 0, s, curses.A_REVERSE)
else:
self.win.addstr(count, 0, s)
count += offset
self.lines = count
self.win.refresh()