When yank msgs, also copy to clipboard (#76)

* When yank msgs, also copy to clipboard
This commit is contained in:
Nameless 2020-06-12 01:40:47 +08:00 committed by GitHub
parent 5a14a94802
commit 4c8095e02d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 21 deletions

View file

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

View file

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