diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-03-11 16:08:22 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-03-11 16:08:22 +0100 |
| commit | abaa7713a73af3afb94c249c652082d2a9d3b097 (patch) | |
| tree | 5e2307a96933f9c4f21540b0934c5f3d10460487 | |
| parent | 1bb97e0d50c9e542233304a6f3fbd4ed11b3c999 (diff) | |
| download | tg-abaa7713a73af3afb94c249c652082d2a9d3b097.tar.gz | |
show wip typing, read backspace and fix åäö
| -rw-r--r-- | TODO | 2 | ||||
| -rw-r--r-- | cursed.py | 37 | ||||
| -rw-r--r-- | main.py | 6 | ||||
| -rw-r--r-- | tele.py | 5 |
4 files changed, 30 insertions, 20 deletions
@@ -9,7 +9,6 @@ daily driver: - tg chat - tg list - show chat history -- show current wip message - notifications - libnotify - silent messages @@ -31,3 +30,4 @@ later: - save drafts - login - show when someone is typing +- mark messages as read @@ -3,8 +3,10 @@ import asyncio import sys class Console(): - def __init__(self): + def __init__(self, messages): self.stdscr = curses.initscr() + self.messages = messages + self.typed_message = "" def __enter__(self): curses.noecho() @@ -18,24 +20,29 @@ class Console(): curses.echo() curses.endwin() - def redraw(self, messages): + def redraw(self): self.stdscr.clear() - for msg in messages: + for msg in self.messages: self.stdscr.addstr(msg + '\n') + if self.typed_message != "": + self.stdscr.addstr(self.typed_message) self.stdscr.refresh() -async def start(c, on_str, on_tab, messages): - string = "" - c.redraw(messages) +async def start(c, on_str, on_tab): while True: - char = await asyncio.to_thread(c.stdscr.getkey) - if char == '\n' and string != "": - await on_str(string) - messages.append(string) - c.redraw(messages) - string = "" - elif char == '\t': - await on_tab() + c.redraw() + char = await asyncio.to_thread(c.stdscr.get_wch) + if isinstance(char, str): + if char == '\n': + if c.typed_message != "": + await on_str(c.typed_message) + c.messages.append(c.typed_message) + c.typed_message = "" + elif char == '\t': + await on_tab() + else: + c.typed_message += char else: - string += char + if char == 263: + c.typed_message = c.typed_message[:-1] @@ -12,12 +12,12 @@ async def on_tab(): def main(): messages = [] - with cursed.Console() as c: + with cursed.Console(messages) as c: tele.messages = messages - tele.redraw = lambda message: c.redraw(message) + tele.redraw = lambda: c.redraw() with tele.client: tele.client.start() - tele.client.loop.run_until_complete(cursed.start(c, on_str, on_tab, messages)) + tele.client.loop.run_until_complete(cursed.start(c, on_str, on_tab)) if __name__=="__main__": @@ -17,4 +17,7 @@ client = telethon.TelegramClient( @client.on(telethon.events.NewMessage) async def new_message(event): messages.append(event.raw_text) - redraw(messages) + redraw() + +if __name__ == "__main__": + client.start() |
