From 4c8095e02d1a2fcb57d58be2ce9786e9fd7cf29b Mon Sep 17 00:00:00 2001 From: Nameless Date: Fri, 12 Jun 2020 01:40:47 +0800 Subject: [PATCH] When yank msgs, also copy to clipboard (#76) * When yank msgs, also copy to clipboard --- tg/controllers.py | 23 +++++------------------ tg/models.py | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/tg/controllers.py b/tg/controllers.py index 9dc4b06..2098263 100644 --- a/tg/controllers.py +++ b/tg/controllers.py @@ -13,7 +13,6 @@ from tg.models import Model from tg.msg import MsgProxy from tg.tdlib import Tdlib from tg.utils import ( - copy_to_clipboard, get_duration, get_video_resolution, get_waveform, @@ -107,7 +106,6 @@ class Controller: "A": lambda _: self.write_long_msg(), "p": lambda _: self.forward_msgs(), "y": lambda _: self.copy_msgs(), - "c": lambda _: self.copy_msg_text(), "r": lambda _: self.reply_message(), "R": lambda _: self.reply_with_long_message(), # message selection @@ -116,18 +114,6 @@ class Controller: "^[": lambda _: self.discard_selected_msgs(), # esc } - def copy_msg_text(self): - """Copies current msg text or path to file if it's file""" - msg = MsgProxy(self.model.current_msg) - if msg.file_id: - text = msg.local_path - elif msg.is_text: - text = msg.text_content - else: - return - copy_to_clipboard(text) - self.present_info("Copied msg") - def forward_msgs(self): if not self.model.forward_msgs(): self.present_error("Can't forward msg(s)") @@ -140,11 +126,12 @@ class Controller: return msg_ids = self.model.selected[chat_id] if not msg_ids: - self.present_error("No msgs selected") - return - self.model.yanked_msgs = (chat_id, msg_ids) + msg = self.model.current_msg + msg_ids = [msg["id"]] + self.model.copied_msgs = (chat_id, msg_ids) self.discard_selected_msgs() - self.present_info(f"Copied {len(msg_ids)} messages") + self.model.copy_msgs_text() + self.present_info(f"Copied {len(msg_ids)} msg(s)") def toggle_select_msg(self): chat_id = self.model.chats.id_by_index(self.model.current_chat) diff --git a/tg/models.py b/tg/models.py index afab525..12d94f3 100644 --- a/tg/models.py +++ b/tg/models.py @@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple from tg.msg import MsgProxy from tg.tdlib import Tdlib +from tg.utils import copy_to_clipboard log = logging.getLogger(__name__) @@ -18,7 +19,7 @@ class Model: self.current_chat = 0 self.downloads: Dict[int, Tuple[int, int]] = {} self.selected: Dict[int, List[int]] = defaultdict(list) - self.yanked_msgs: Tuple[int, List[int]] = (0, []) + self.copied_msgs: Tuple[int, List[int]] = (0, []) def get_me(self): return self.users.get_me() @@ -168,7 +169,7 @@ class Model: chat_id = self.chats.id_by_index(self.current_chat) if not chat_id: return False - from_chat_id, msg_ids = self.yanked_msgs + from_chat_id, msg_ids = self.copied_msgs if not msg_ids: return False for msg_id in msg_ids: @@ -177,9 +178,24 @@ class Model: return False self.tg.forward_messages(chat_id, from_chat_id, msg_ids) - self.yanked_msgs = (0, []) + self.copied_msgs = (0, []) return True + def copy_msgs_text(self): + """Copies current msg text or path to file if it's file""" + buffer = [] + + from_chat_id, msg_ids = self.copied_msgs + if not msg_ids: + return False + for msg_id in msg_ids: + msg = MsgProxy(self.msgs.get_message(from_chat_id, msg_id)) + if msg.file_id: + buffer.append(msg.local_path) + elif msg.is_text: + buffer.append(msg.text_content) + copy_to_clipboard("\n".join(buffer)) + class ChatModel: def __init__(self, tg: Tdlib) -> None: