mirror of
https://github.com/paul-nameless/tg
synced 2024-11-22 03:43:19 +00:00
Merge branch 'master' into tail-long-msgs
This commit is contained in:
commit
985da1d502
8 changed files with 64 additions and 49 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ __pycache__
|
|||
.env
|
||||
dist
|
||||
*.log*
|
||||
Makefile
|
||||
|
|
|
@ -3,7 +3,7 @@ module = "tg"
|
|||
dist-name = "tg"
|
||||
description-file = "README.md"
|
||||
author = "Paul Nameless"
|
||||
author-email = "paul.nameless@icloud.com"
|
||||
author-email = "reacsdas@gmail.com"
|
||||
home-page = 'https://github.com/paul-nameless/tg'
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
|
|
|
@ -3,4 +3,4 @@ Tg
|
|||
|
||||
TUI client for telegram
|
||||
"""
|
||||
__version__ = "0.0.1"
|
||||
__version__ = "0.0.2"
|
||||
|
|
|
@ -471,7 +471,7 @@ class Controller:
|
|||
)
|
||||
self.view.msgs.draw(current_msg_idx, msgs, MSGS_LEFT_SCROLL_THRESHOLD)
|
||||
|
||||
def _notify_for_message(self, chat_id: int, msg: MsgProxy):
|
||||
def notify_for_message(self, chat_id: int, msg: MsgProxy):
|
||||
# do not notify, if muted
|
||||
# TODO: optimize
|
||||
chat = None
|
||||
|
|
|
@ -29,7 +29,7 @@ class Model:
|
|||
return self.users.get_user(user_id)
|
||||
|
||||
@property
|
||||
def current_chat_id(self):
|
||||
def current_chat_id(self) -> Optional[int]:
|
||||
return self.chats.id_by_index(self.current_chat)
|
||||
|
||||
def get_current_chat_msg_idx(self) -> Optional[int]:
|
||||
|
|
|
@ -43,9 +43,8 @@ def update_msg_content(controller: Controller, update: Dict[str, Any]):
|
|||
controller.model.msgs.update_msg_content(
|
||||
chat_id, message_id, update["new_content"]
|
||||
)
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
if current_chat_id == chat_id:
|
||||
controller.render_msgs()
|
||||
|
||||
|
@ -54,23 +53,19 @@ def update_msg_content(controller: Controller, update: Dict[str, Any]):
|
|||
def update_new_msg(controller: Controller, update: Dict[str, Any]):
|
||||
msg = MsgProxy(update["message"])
|
||||
controller.model.msgs.add_message(msg.chat_id, msg.msg)
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
if current_chat_id == msg.chat_id:
|
||||
controller.render_msgs()
|
||||
if msg.file_id and msg.size <= max_download_size:
|
||||
controller.download(msg.file_id, msg.chat_id, msg["id"])
|
||||
|
||||
controller._notify_for_message(msg.chat_id, msg)
|
||||
controller.notify_for_message(msg.chat_id, msg)
|
||||
|
||||
|
||||
@update_handler("updateChatOrder")
|
||||
def update_chat_order(controller: Controller, update: Dict[str, Any]):
|
||||
log.info("Proccessing updateChatOrder")
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
chat_id = update["chat_id"]
|
||||
order = update["order"]
|
||||
|
||||
|
@ -83,9 +78,8 @@ def update_chat_title(controller: Controller, update: Dict[str, Any]):
|
|||
log.info("Proccessing updateChatTitle")
|
||||
chat_id = update["chat_id"]
|
||||
title = update["title"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(chat_id, title=title)
|
||||
controller._refresh_current_chat(current_chat_id)
|
||||
|
||||
|
@ -97,9 +91,8 @@ def update_chat_marked_as_unread(
|
|||
log.info("Proccessing updateChatIsMarkedAsUnread")
|
||||
chat_id = update["chat_id"]
|
||||
is_marked_as_unread = update["is_marked_as_unread"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(
|
||||
chat_id, is_marked_as_unread=is_marked_as_unread
|
||||
)
|
||||
|
@ -112,9 +105,8 @@ def update_chat_is_pinned(controller: Controller, update: Dict[str, Any]):
|
|||
chat_id = update["chat_id"]
|
||||
is_pinned = update["is_pinned"]
|
||||
order = update["order"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(
|
||||
chat_id, is_pinned=is_pinned, order=order
|
||||
)
|
||||
|
@ -126,9 +118,8 @@ def update_chat_read_outbox(controller: Controller, update: Dict[str, Any]):
|
|||
log.info("Proccessing updateChatReadOutbox")
|
||||
chat_id = update["chat_id"]
|
||||
last_read_outbox_message_id = update["last_read_outbox_message_id"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(
|
||||
chat_id, last_read_outbox_message_id=last_read_outbox_message_id,
|
||||
)
|
||||
|
@ -141,9 +132,8 @@ def update_chat_read_inbox(controller: Controller, update: Dict[str, Any]):
|
|||
chat_id = update["chat_id"]
|
||||
last_read_inbox_message_id = update["last_read_inbox_message_id"]
|
||||
unread_count = update["unread_count"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(
|
||||
chat_id,
|
||||
last_read_inbox_message_id=last_read_inbox_message_id,
|
||||
|
@ -159,9 +149,8 @@ def update_chat_draft_msg(controller: Controller, update: Dict[str, Any]):
|
|||
# FIXME: ignoring draft message itself for now because UI can't show it
|
||||
# draft_message = update["draft_message"]
|
||||
order = update["order"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(chat_id, order=order)
|
||||
controller._refresh_current_chat(current_chat_id)
|
||||
|
||||
|
@ -172,9 +161,8 @@ def update_chat_last_msg(controller: Controller, update: Dict[str, Any]):
|
|||
chat_id = update["chat_id"]
|
||||
message = update["last_message"]
|
||||
order = update["order"]
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
controller.model.chats.update_chat(
|
||||
chat_id, last_message=message, order=order
|
||||
)
|
||||
|
@ -198,9 +186,8 @@ def update_msg_send_succeeded(controller: Controller, update):
|
|||
msg_id = update["old_message_id"]
|
||||
controller.model.msgs.add_message(chat_id, update["message"])
|
||||
controller.model.msgs.remove_message(chat_id, msg_id)
|
||||
current_chat_id = controller.model.chats.id_by_index(
|
||||
controller.model.current_chat
|
||||
)
|
||||
|
||||
current_chat_id = controller.model.current_chat_id
|
||||
if current_chat_id == chat_id:
|
||||
controller.render_msgs()
|
||||
|
||||
|
|
|
@ -149,7 +149,9 @@ def truncate_to_len(s: str, target_len: int, encoding: str = "utf-8") -> str:
|
|||
|
||||
|
||||
def copy_to_clipboard(text):
|
||||
subprocess.run(config.COPY_CMD, universal_newlines=True, input=text)
|
||||
subprocess.run(
|
||||
config.COPY_CMD, universal_newlines=True, input=text, shell=True
|
||||
)
|
||||
|
||||
|
||||
class suspend:
|
||||
|
|
45
tg/views.py
45
tg/views.py
|
@ -287,23 +287,29 @@ class MsgView:
|
|||
return ""
|
||||
return " ".join(flags)
|
||||
|
||||
def _format_reply_msg(self, chat_id: int, msg: str, reply_to: int) -> str:
|
||||
def _format_reply_msg(
|
||||
self, chat_id: int, msg: str, reply_to: int, width_limit: int
|
||||
) -> str:
|
||||
reply_msg = MsgProxy(self.msg_model.get_message(chat_id, reply_to))
|
||||
if reply_msg_content := self._parse_msg(reply_msg):
|
||||
reply_msg_content = reply_msg_content.replace("\n", " ")
|
||||
if len(reply_msg_content) > 68:
|
||||
# trimming old reply messages as it done in tg web & desktop
|
||||
reply_msg_content = f"{reply_msg_content[:65]}..."
|
||||
reply_sender = self._get_user_by_id(reply_msg.sender_id)
|
||||
sender_name = f" {reply_sender}:" if reply_sender else ""
|
||||
msg = f">{sender_name} {reply_msg_content}\n{msg}"
|
||||
reply_line = f">{sender_name} {reply_msg_content}"
|
||||
if len(reply_line) >= width_limit:
|
||||
reply_line = f"{reply_line[:width_limit - 4]}..."
|
||||
msg = f"{reply_line}\n{msg}"
|
||||
return msg
|
||||
|
||||
def _format_msg(self, msg_proxy: MsgProxy, user_id_item: int) -> str:
|
||||
def _format_msg(
|
||||
self, msg_proxy: MsgProxy, user_id_item: int, width_limit: int
|
||||
) -> str:
|
||||
msg = self._parse_msg(msg_proxy)
|
||||
msg = msg.replace("\n", " ")
|
||||
if reply_to := msg_proxy.reply_msg_id:
|
||||
msg = self._format_reply_msg(msg_proxy.chat_id, msg, reply_to)
|
||||
msg = self._format_reply_msg(
|
||||
msg_proxy.chat_id, msg, reply_to, width_limit
|
||||
)
|
||||
return msg
|
||||
|
||||
def _collect_msgs_to_draw(
|
||||
|
@ -333,7 +339,6 @@ class MsgView:
|
|||
dt = msg_proxy.date.strftime("%H:%M:%S")
|
||||
user_id_item = msg_proxy.sender_id
|
||||
|
||||
msg = self._format_msg(msg_proxy, user_id_item)
|
||||
user_id = self._get_user_by_id(user_id_item)
|
||||
flags = self._get_flags(msg_proxy)
|
||||
if user_id and flags:
|
||||
|
@ -341,8 +346,11 @@ class MsgView:
|
|||
flags = " " + flags
|
||||
label_elements = f" {dt} ", user_id, flags
|
||||
label_len = sum(len(e) for e in label_elements)
|
||||
elements = *label_elements, f" {msg}"
|
||||
|
||||
msg = self._format_msg(
|
||||
msg_proxy, user_id_item, width_limit=self.w - label_len - 1
|
||||
)
|
||||
elements = *label_elements, f" {msg}"
|
||||
needed_lines = 0
|
||||
for i, msg_line in enumerate(msg.split("\n")):
|
||||
# count wide character utf-8 symbols that take > 1 bytes to
|
||||
|
@ -403,7 +411,24 @@ class MsgView:
|
|||
for attr, elem in zip(self._msg_attributes(selected), elements):
|
||||
if not elem:
|
||||
continue
|
||||
self.win.addstr(line_num, column, elem, attr)
|
||||
lines = (column + len(elem)) // self.w
|
||||
last_line = self.h == line_num + lines
|
||||
# work around agaist curses behaviour, when you cant write
|
||||
# char to the lower right coner of the window
|
||||
# see https://stackoverflow.com/questions/21594778/how-to-fill-to-lower-right-corner-in-python-curses/27517397#27517397
|
||||
if last_line:
|
||||
start, stop = 0, self.w - column
|
||||
for i in range(lines):
|
||||
# insstr does not wraps long strings
|
||||
self.win.insstr(
|
||||
line_num + i,
|
||||
column if not i else 0,
|
||||
elem[start:stop],
|
||||
attr,
|
||||
)
|
||||
start, stop = stop, stop + self.w
|
||||
else:
|
||||
self.win.addstr(line_num, column, elem, attr)
|
||||
column += len(elem)
|
||||
|
||||
self._refresh()
|
||||
|
|
Loading…
Reference in a new issue