Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes MessageBox colorful and grouped by date #51

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion telegramtui/src/MessageInfoForm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def create(self):
def update(self):
current_user = self.parentApp.MainForm.chatBoxObj.value
current_user_name = client.dialogs[current_user].name
current_message = self.parentApp.MainForm.messageBoxObj.value
val = self.parentApp.MainForm.messageBoxObj.value
current_message = self.parentApp.MainForm.messageBoxObj.msg_indexs[-val-1]
messages = self.parentApp.MainForm.messageBoxObj.get_messages_info(current_user)
current_id = messages[-current_message - 1].id

Expand Down
69 changes: 59 additions & 10 deletions telegramtui/src/messageBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os.path
from telegramtui.src import npyscreen
from telegramtui.src.telegramApi import client
from telegramtui.src import aalib
import aalib
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently aalib working only on Linux :(

from PIL import Image
import datetime


class MessageBox(npyscreen.BoxTitle):
Expand All @@ -14,6 +15,15 @@ def create(self, **kwargs):
self.aalib = kwargs['aalib'] if 'aalib' in kwargs else False

self.buff_messages = len(client.dialogs) * [None]
self.COLORS = [
'STANDOUT' ,
'LABEL' ,
'LABELBOLD' ,
'CONTROL' ,
'SAFE' ,
'WARNING' ,
'DANGER'
]

def when_value_edited(self):
if self.value is not None:
Expand All @@ -24,13 +34,32 @@ def when_cursor_moved(self):
self.parent.parentApp.queue_event(npyscreen.Event("event_messagebox_change_cursor"))

def update_messages(self, current_user):
self.msg_indexs = []
messages = self.get_messages_info(current_user)

color_data = []
data = []
last_date = datetime.datetime(1970, 1, 1)
for i in range(len(messages) - 1, -1, -1):
self.msg_indexs.append(i)
# replace empty char
cur_msg = messages[i]
messages[i].message = messages[i].message.replace(chr(8203), '')
cur_date = cur_msg.date
if cur_date.date() > last_date.date():
last_date = cur_date
self.add_datemark(data, color_data, cur_date)
else:
time_delta = cur_date - last_date
# delta_hours = int(time_delta.seconds / 3600)
# if bool(delta_hours):
# bar = '| ' * delta_hours
# data.append(bar)
# color_data.append(len(bar)*[0]) # white color
if time_delta>datetime.timedelta(0, 1800, 0):
self.add_datemark(data, color_data, cur_date, False)

last_date = cur_msg.date

data.append(messages[i].name + " " + messages[i].message)
color_data.append(messages[i].color)
Expand All @@ -39,18 +68,33 @@ def update_messages(self, current_user):

self.values = data

if len(messages) > self.height - 3:
self.entry_widget.start_display_at = len(messages) - self.height + 3
if len(data) > self.height - 3:
self.entry_widget.start_display_at = len(data) - self.height + 3
else:
self.entry_widget.start_display_at = 0

self.entry_widget.cursor_line = len(messages)
self.entry_widget.cursor_line = len(data)

self.name = client.dialogs[current_user].name
self.footer = client.online[current_user]

self.display()

def add_datemark(self, out, color_out, date, full=True):
if full:
today = datetime.date.today()
dd = (today - date.date()).days
if dd < 2:
mark=['Today ','Yesterday '][dd]+str(date.strftime('%I:%M:%S'))
else:
mark=str(date)
else:
mark = 11*' '+str(date.time())

self.msg_indexs.append(-1)
out.append(mark)
color_out.append(len(mark)*[0]) # white color

def get_messages_info(self, current_user):
messages = client.get_messages(current_user)

Expand Down Expand Up @@ -80,6 +124,10 @@ def get_messages_info(self, current_user):
# get name if message is forwarding
prepare_forward_message = self.prepare_forward_messages(messages[i])

media = messages[i].media if hasattr(messages[i], 'media') else None
mess = messages[i].message if hasattr(messages[i], 'message') \
and isinstance(messages[i].message, str) else None

# if chat or interlocutor
if dialog_type == 1 or dialog_type == 2:
user_name = users[messages[i].sender.id].name
Expand All @@ -90,7 +138,7 @@ def get_messages_info(self, current_user):
user_name = "Deleted Account"
offset = " " * (max_name_len - (len(user_name)))
name = read + user_name + ":" + offset
color = (len(read) + len(user_name)) * [users[messages[i].sender.id].color]
color = (len(read) + len(name) + (len(mess) if type(mess) is str else 0)) * [users[messages[i].sender.id].color]

# if channel
elif dialog_type == 3:
Expand All @@ -105,9 +153,6 @@ def get_messages_info(self, current_user):
name = ""
color = [0]

media = messages[i].media if hasattr(messages[i], 'media') else None
mess = messages[i].message if hasattr(messages[i], 'message') \
and isinstance(messages[i].message, str) else None

image_name = ""
if self.aalib and media is not None and hasattr(media, 'photo'):
Expand Down Expand Up @@ -146,6 +191,9 @@ def __init__(self, color, name):
client.dialogs[current_user].first_name
users[client.dialogs[current_user].dialog.peer.user_id] = user_info(
self.parent.theme_manager.findPair(self, 'WARNING'), name)
if name == 'Ninje Ac':
users[client.dialogs[current_user].dialog.peer.user_id] = user_info(
self.parent.theme_manager.findPair(self, 'STANDOUT'), name)

# set me
name = client.me.first_name if hasattr(client.me, 'first_name') else client.me.last_name
Expand All @@ -166,8 +214,9 @@ def __init__(self, color, name):
elif hasattr(messages[i].sender, 'last_name') and messages[i].sender.last_name is not None:
username = messages[i].sender.last_name

users[messages[i].sender.id] = user_info(
self.parent.theme_manager.findPair(self, 'WARNING'), username)
if messages[i].sender.id not in users:
users[messages[i].sender.id] = user_info(
self.parent.theme_manager.findPair(self, self.COLORS[i%len(self.COLORS)]), username)

max_name_len = max(max_name_len, len(username))

Expand Down
2 changes: 1 addition & 1 deletion telegramtui/src/npyscreen/eveventhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def handle_event(self, event):
for handler in self.event_handlers[event.name]:
try:
handler(event)
except weakref.ReferenceError:
except ReferenceError:
remove_list.append(handler)
for dead_handler in remove_list:
self.event_handlers[event.name].remove(handler)
Expand Down
8 changes: 4 additions & 4 deletions telegramtui/src/npyscreen/wgcheckbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def set_up_handlers(self):
ord('x'): self.h_toggle,
curses.ascii.NL: self.h_select_exit,
curses.ascii.CR: self.h_select_exit,
ord('j'): self.h_exit_down,
ord('k'): self.h_exit_up,
ord('h'): self.h_exit_left,
ord('l'): self.h_exit_right,
ord('k'): self.h_exit_down,
ord('l'): self.h_exit_up,
ord('j'): self.h_exit_left,
ord(';'): self.h_exit_right,
})

def h_toggle(self, ch):
Expand Down
4 changes: 2 additions & 2 deletions telegramtui/src/npyscreen/wgdatecombo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def set_up_handlers(self):
curses.ascii.CR: self.h_change_value,
curses.ascii.NL: self.h_change_value,
ord('x'): self.h_change_value,
ord('j'): self.h_exit_down,
ord('k'): self.h_exit_up,
ord('k'): self.h_exit_down,
ord('l'): self.h_exit_up,
})

class TitleDateCombo(titlefield.TitleText):
Expand Down
6 changes: 3 additions & 3 deletions telegramtui/src/npyscreen/wgmultiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ def set_up_handlers(self):
super(MultiLine, self).set_up_handlers()
self.handlers.update({
curses.KEY_UP: self.h_cursor_line_up,
ord('k'): self.h_cursor_line_up,
ord('l'): self.h_cursor_line_up,
curses.KEY_LEFT: self.h_cursor_line_up,
curses.KEY_DOWN: self.h_cursor_line_down,
ord('j'): self.h_cursor_line_down,
ord('k'): self.h_cursor_line_down,
curses.KEY_RIGHT: self.h_cursor_line_down,
curses.KEY_NPAGE: self.h_cursor_page_down,
curses.KEY_PPAGE: self.h_cursor_page_up,
Expand All @@ -457,7 +457,7 @@ def set_up_handlers(self):

if self.allow_filtering:
self.handlers.update({
ord('l'): self.h_set_filter,
ord('/'): self.h_set_filter,
ord('L'): self.h_clear_filter,
ord('n'): self.move_next_filtered,
ord('N'): self.move_previous_filtered,
Expand Down
8 changes: 4 additions & 4 deletions telegramtui/src/npyscreen/wgslider.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def set_up_handlers(self):
curses.KEY_RIGHT: self.h_increase,
ord('+'): self.h_increase,
ord('-'): self.h_decrease,
ord('h'): self.h_decrease,
ord('l'): self.h_increase,
ord('j'): self.h_exit_down,
ord('k'): self.h_exit_up,
ord('j'): self.h_decrease,
ord(';'): self.h_increase,
ord('k'): self.h_exit_down,
ord('l'): self.h_exit_up,
})

def h_increase(self, ch):
Expand Down
4 changes: 2 additions & 2 deletions telegramtui/src/npyscreen/wgtextbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ def set_up_handlers(self):
super(FixedText, self).set_up_handlers()
self.handlers.update({curses.KEY_LEFT: self.h_cursor_left,
curses.KEY_RIGHT: self.h_cursor_right,
ord('k'): self.h_exit_up,
ord('j'): self.h_exit_down,
ord('l'): self.h_exit_up,
ord('k'): self.h_exit_down,
})


Expand Down