mirror of
https://github.com/paul-nameless/tg
synced 2024-11-22 03:43:19 +00:00
Contacts search (#144)
* wip * improve search displaying * add next chat support * navigate chats backwards * prettify * add optional param
This commit is contained in:
parent
c020ba1ea7
commit
16b6a5a4d9
4 changed files with 74 additions and 7 deletions
|
@ -495,6 +495,44 @@ class Controller:
|
|||
self.model.edit_message(text=text)
|
||||
self.present_info("Message edited")
|
||||
|
||||
@bind(chat_handler, ["n"])
|
||||
def next_found_chat(self) -> None:
|
||||
"""Go to next found chat"""
|
||||
if self.model.set_current_chat_by_id(
|
||||
self.model.chats.next_found_chat()
|
||||
):
|
||||
self.render()
|
||||
|
||||
@bind(chat_handler, ["N"])
|
||||
def prev_found_chat(self) -> None:
|
||||
"""Go to previous found chat"""
|
||||
if self.model.set_current_chat_by_id(
|
||||
self.model.chats.next_found_chat(True)
|
||||
):
|
||||
self.render()
|
||||
|
||||
@bind(chat_handler, ["/"])
|
||||
def search_contacts(self) -> None:
|
||||
"""Search contacts and set jumps to it if found"""
|
||||
msg = self.view.status.get_input(prefix="/")
|
||||
if not msg:
|
||||
return self.present_info("Search discarded")
|
||||
|
||||
rv = self.tg.search_contacts(msg)
|
||||
chat_ids = rv.update["chat_ids"]
|
||||
if not chat_ids:
|
||||
return self.present_info("Chat not found")
|
||||
|
||||
chat_id = chat_ids[0]
|
||||
if chat_id not in self.model.chats.chat_ids:
|
||||
self.present_info("Chat not loaded")
|
||||
return
|
||||
|
||||
self.model.chats.found_chats = chat_ids
|
||||
|
||||
if self.model.set_current_chat_by_id(chat_id):
|
||||
self.render()
|
||||
|
||||
@bind(chat_handler, ["c"])
|
||||
def view_contacts(self) -> None:
|
||||
contacts = self.model.users.get_contacts()
|
||||
|
|
26
tg/models.py
26
tg/models.py
|
@ -72,6 +72,22 @@ class Model:
|
|||
return self.msgs.jump_bottom(chat_id)
|
||||
return False
|
||||
|
||||
def set_current_chat_by_id(self, chat_id: int) -> bool:
|
||||
idx = next(
|
||||
iter(
|
||||
i
|
||||
for i, chat in enumerate(self.chats.chats)
|
||||
if chat["id"] == chat_id
|
||||
)
|
||||
)
|
||||
return self.set_current_chat(idx)
|
||||
|
||||
def set_current_chat(self, chat_idx: int) -> bool:
|
||||
if 0 < chat_idx < len(self.chats.chats):
|
||||
self.current_chat = chat_idx
|
||||
return True
|
||||
return False
|
||||
|
||||
def next_chat(self, step: int = 1) -> bool:
|
||||
new_idx = self.current_chat + step
|
||||
if new_idx < len(self.chats.chats):
|
||||
|
@ -210,6 +226,16 @@ class ChatModel:
|
|||
self.chat_ids: Set[int] = set()
|
||||
self.have_full_chat_list = False
|
||||
self.title: str = "Chats"
|
||||
self.found_chats: List[int] = []
|
||||
self.found_chat_idx: int = 0
|
||||
|
||||
def next_found_chat(self, backwards: bool = False) -> int:
|
||||
new_idx = self.found_chat_idx + (-1 if backwards else 1)
|
||||
new_idx %= len(self.found_chats)
|
||||
|
||||
self.found_chat_idx = new_idx
|
||||
|
||||
return self.found_chats[new_idx]
|
||||
|
||||
def id_by_index(self, index: int) -> Optional[int]:
|
||||
if index >= len(self.chats):
|
||||
|
|
|
@ -113,6 +113,10 @@ class Tdlib(Telegram):
|
|||
|
||||
return self._send_data(data)
|
||||
|
||||
def search_contacts(self, target: str, limit: int = 10) -> AsyncResult:
|
||||
data = {"@type": "searchChats", "query": target, "limit": limit}
|
||||
return self._send_data(data, block=True)
|
||||
|
||||
def send_doc(self, file_path: str, chat_id: int) -> AsyncResult:
|
||||
data = {
|
||||
"@type": "sendMessage",
|
||||
|
|
13
tg/views.py
13
tg/views.py
|
@ -95,13 +95,15 @@ class StatusView:
|
|||
self.win.addstr(0, 0, msg.replace("\n", " ")[: self.w])
|
||||
self._refresh()
|
||||
|
||||
def get_input(self, msg: str = "") -> str:
|
||||
self.draw(msg)
|
||||
def get_input(self, buff: str = "", prefix: str = "") -> str:
|
||||
curses.curs_set(1)
|
||||
|
||||
buff = ""
|
||||
while True:
|
||||
key = self.win.get_wch(0, min(len(buff) + len(msg), self.w - 1))
|
||||
self.win.erase()
|
||||
line = buff[-(self.w - 1) :]
|
||||
self.win.addstr(0, 0, f"{prefix}{line}")
|
||||
|
||||
key = self.win.get_wch(0, min(len(buff + prefix), self.w - 1))
|
||||
key = ord(key)
|
||||
if key == 10: # return
|
||||
break
|
||||
|
@ -113,9 +115,6 @@ class StatusView:
|
|||
break
|
||||
elif chr(key).isprintable():
|
||||
buff += chr(key)
|
||||
self.win.erase()
|
||||
line = (msg + buff)[-(self.w - 1) :]
|
||||
self.win.addstr(0, 0, line)
|
||||
|
||||
self.win.clear()
|
||||
curses.curs_set(0)
|
||||
|
|
Loading…
Reference in a new issue