Merge pull request #187 from paul-nameless/show-reply-markup

Show reply markup
This commit is contained in:
Nameless 2020-10-16 11:21:35 +03:00 committed by GitHub
commit fabcafbb94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 16 deletions

View file

@ -26,7 +26,7 @@ jobs:
- name: Install tools
run: |
pip install black==19.10b0 mypy==0.770 isort==5.1.0 flake8==3.8.3
pip install black==20.8b1 mypy==0.790 isort==5.6.2 flake8==3.8.4
- name: Check formatting and run linters
run: |

View file

@ -424,7 +424,8 @@ class Controller:
self.tg.send_video(file_path, chat_id, width, height, duration)
def send_file(
self, send_file_fun: Callable[[str, int], AsyncResult],
self,
send_file_fun: Callable[[str, int], AsyncResult],
) -> None:
_input = self.view.status.get_input()
if _input is None:
@ -548,7 +549,10 @@ class Controller:
def _get_user_ids(self, is_multiple: bool = False) -> List[int]:
users = self.model.users.get_users()
_, cols = self.view.stdscr.getmaxyx()
limit = min(int(cols / 2), max(len(user.name) for user in users),)
limit = min(
int(cols / 2),
max(len(user.name) for user in users),
)
users_out = "\n".join(
f"{user.id}\t{user.name:<{limit}} | {user.status}"
for user in sorted(users, key=lambda user: user.order)

View file

@ -203,6 +203,15 @@ class MsgProxy:
def reply_msg_id(self) -> Optional[int]:
return self.msg.get("reply_to_message_id")
@property
def reply_markup(self) -> Optional[Dict[str, Any]]:
return self.msg.get("reply_markup")
@property
def reply_markup_rows(self) -> List[List[Dict[str, Any]]]:
assert self.reply_markup
return self.reply_markup.get("rows", [])
@property
def chat_id(self) -> int:
return self.msg["chat_id"]

View file

@ -294,35 +294,50 @@ class Tdlib(Telegram):
}
return self._send_data(data)
def get_basic_group(self, basic_group_id: int,) -> AsyncResult:
def get_basic_group(
self,
basic_group_id: int,
) -> AsyncResult:
data = {
"@type": "getBasicGroup",
"basic_group_id": basic_group_id,
}
return self._send_data(data)
def get_basic_group_full_info(self, basic_group_id: int,) -> AsyncResult:
def get_basic_group_full_info(
self,
basic_group_id: int,
) -> AsyncResult:
data = {
"@type": "getBasicGroupFullInfo",
"basic_group_id": basic_group_id,
}
return self._send_data(data)
def get_supergroup(self, supergroup_id: int,) -> AsyncResult:
def get_supergroup(
self,
supergroup_id: int,
) -> AsyncResult:
data = {
"@type": "getSupergroup",
"supergroup_id": supergroup_id,
}
return self._send_data(data)
def get_supergroup_full_info(self, supergroup_id: int,) -> AsyncResult:
def get_supergroup_full_info(
self,
supergroup_id: int,
) -> AsyncResult:
data = {
"@type": "getSupergroupFullInfo",
"supergroup_id": supergroup_id,
}
return self._send_data(data)
def get_secret_chat(self, secret_chat_id: int,) -> AsyncResult:
def get_secret_chat(
self,
secret_chat_id: int,
) -> AsyncResult:
data = {
"@type": "getSecretChat",
"secret_chat_id": secret_chat_id,

View file

@ -102,7 +102,16 @@ def parse_size(size: str) -> int:
def humanize_size(
num: int,
suffix: str = "B",
suffixes: Tuple[str, ...] = ("", "K", "M", "G", "T", "P", "E", "Z",),
suffixes: Tuple[str, ...] = (
"",
"K",
"M",
"G",
"T",
"P",
"E",
"Z",
),
) -> str:
magnitude = int(math.floor(math.log(num, 1024)))
val = num / math.pow(1024, magnitude)

View file

@ -280,7 +280,11 @@ class ChatView:
class MsgView:
def __init__(self, stdscr: window, model: Model,) -> None:
def __init__(
self,
stdscr: window,
model: Model,
) -> None:
self.model = model
self.stdscr = stdscr
self.h = 0
@ -353,7 +357,8 @@ class MsgView:
msg = f"{reply_line}\n{msg}"
return msg
def _format_url(self, msg_proxy: MsgProxy) -> str:
@staticmethod
def _format_url(msg_proxy: MsgProxy) -> str:
if not msg_proxy.is_text or "web_page" not in msg_proxy.msg["content"]:
return ""
web = msg_proxy.msg["content"]["web_page"]
@ -365,9 +370,7 @@ class MsgView:
url += f"\n | {description}"
return url
def _format_msg(
self, msg_proxy: MsgProxy, user_id_item: int, width_limit: int
) -> str:
def _format_msg(self, msg_proxy: MsgProxy, width_limit: int) -> str:
msg = self._parse_msg(msg_proxy)
if caption := msg_proxy.caption:
msg += "\n" + caption.replace("\n", " ")
@ -376,6 +379,29 @@ class MsgView:
msg = self._format_reply_msg(
msg_proxy.chat_id, msg, reply_to, width_limit
)
if reply_markup := self._format_reply_markup(msg_proxy):
msg += reply_markup
return msg
@staticmethod
def _format_reply_markup(msg_proxy: MsgProxy) -> str:
msg = ""
reply_markup = msg_proxy.reply_markup
if not reply_markup:
return msg
for row in msg_proxy.reply_markup_rows:
msg += "\n"
for item in row:
text = item.get("text")
if not text:
continue
_type = item.get("type", {})
if _type.get("@type") == "inlineKeyboardButtonTypeUrl":
if url := _type.get("url"):
text = f"{text} ({url})"
msg += f"| {text} "
msg += "|"
return msg
def _collect_msgs_to_draw(
@ -409,12 +435,12 @@ class MsgView:
flags = self._get_flags(msg_proxy)
if user_id and flags:
# if not channel add space between name and flags
flags = " " + flags
flags = f" {flags}"
label_elements = f" {dt} ", user_id, flags
label_len = sum(string_len_dwc(e) for e in label_elements)
msg = self._format_msg(
msg_proxy, user_id_item, width_limit=self.w - label_len - 1
msg_proxy, width_limit=self.w - label_len - 1
)
elements = *label_elements, f" {msg}"
needed_lines = 0