mirror of
https://github.com/paul-nameless/tg
synced 2025-02-16 18:48:24 +00:00
Merge pull request #8 from paul-nameless/add-extended-shortcuts-support
added gg, 4j, 3k keybinding support
This commit is contained in:
commit
6de0fcd204
4 changed files with 94 additions and 68 deletions
|
@ -35,49 +35,49 @@ class Controller:
|
|||
|
||||
while True:
|
||||
|
||||
key = self.view.get_key(self.view.chats.h, self.view.chats.w)
|
||||
log.info('Pressed key: %s', key)
|
||||
if key == 'q':
|
||||
repeat_factor, keys = self.view.get_keys(self.view.chats.h, self.view.chats.w)
|
||||
log.info('Pressed keys: %s', keys)
|
||||
if keys == 'q':
|
||||
return 'QUIT'
|
||||
elif key == ']':
|
||||
elif keys == ']':
|
||||
if self.model.next_chat():
|
||||
self.refresh_chats()
|
||||
elif key == '[':
|
||||
elif keys == '[':
|
||||
if self.model.prev_chat():
|
||||
self.refresh_chats()
|
||||
elif key == 'J':
|
||||
elif keys == 'J':
|
||||
if self.model.jump_next_msg():
|
||||
self.refresh_msgs()
|
||||
elif key == 'K':
|
||||
elif keys == 'K':
|
||||
if self.model.jump_prev_msg():
|
||||
self.refresh_msgs()
|
||||
elif key in ('j', '^B'):
|
||||
if self.model.next_msg():
|
||||
elif keys in ('j', '^B'):
|
||||
if self.model.next_msg(repeat_factor):
|
||||
self.refresh_msgs()
|
||||
elif key in ('k', '^C'):
|
||||
if self.model.prev_msg():
|
||||
elif keys in ('k', '^C'):
|
||||
if self.model.prev_msg(repeat_factor):
|
||||
self.refresh_msgs()
|
||||
elif key == 'G':
|
||||
elif keys == 'G':
|
||||
if self.model.jump_bottom():
|
||||
self.refresh_msgs()
|
||||
|
||||
elif key == '/':
|
||||
elif keys == '/':
|
||||
# search
|
||||
pass
|
||||
elif key == 'gg':
|
||||
elif keys == 'gg':
|
||||
# move to the top
|
||||
pass
|
||||
elif key == 'e':
|
||||
elif keys == 'e':
|
||||
# edit msg
|
||||
pass
|
||||
elif key == 'r':
|
||||
elif keys == 'r':
|
||||
# reply to this msg
|
||||
# print to status line
|
||||
pass
|
||||
elif key == 'I':
|
||||
elif keys == 'I':
|
||||
# open vim or emacs to write long messages
|
||||
pass
|
||||
elif key == 'i':
|
||||
elif keys == 'i':
|
||||
# write new message
|
||||
msg = self.view.get_input()
|
||||
if msg:
|
||||
|
@ -88,7 +88,7 @@ class Controller:
|
|||
)
|
||||
self.view.draw_status(f'Sent: {msg}')
|
||||
|
||||
elif key in ('h', '^D'):
|
||||
elif keys in ('h', '^D'):
|
||||
return 'BACK'
|
||||
|
||||
def handle_chats(self):
|
||||
|
@ -99,11 +99,11 @@ class Controller:
|
|||
self.refresh_chats()
|
||||
while True:
|
||||
|
||||
key = self.view.get_key(self.view.chats.h, self.view.chats.w)
|
||||
log.info('Pressed key: %s', key)
|
||||
if key == 'q':
|
||||
repeat_factor, keys = self.view.get_keys(self.view.chats.h, self.view.chats.w)
|
||||
log.info('Pressed keys: %s', keys)
|
||||
if keys == 'q':
|
||||
return
|
||||
elif key in ('l', '^E'):
|
||||
elif keys in ('l', '^E'):
|
||||
rc = self.handle_msgs()
|
||||
if rc == 'QUIT':
|
||||
return
|
||||
|
@ -111,13 +111,18 @@ class Controller:
|
|||
self.view.msgs.resize(0.5)
|
||||
self.refresh_chats()
|
||||
|
||||
elif key in ('j', '^B'):
|
||||
is_changed = self.model.next_chat()
|
||||
elif keys in ('j', '^B'):
|
||||
is_changed = self.model.next_chat(repeat_factor)
|
||||
if is_changed:
|
||||
self.refresh_chats()
|
||||
|
||||
elif key in ('k', '^C'):
|
||||
is_changed = self.model.prev_chat()
|
||||
elif keys in ('k', '^C'):
|
||||
is_changed = self.model.prev_chat(repeat_factor)
|
||||
if is_changed:
|
||||
self.refresh_chats()
|
||||
|
||||
elif keys == 'gg':
|
||||
is_changed = self.model.first_chat()
|
||||
if is_changed:
|
||||
self.refresh_chats()
|
||||
|
||||
|
|
|
@ -33,25 +33,32 @@ class Model:
|
|||
chat_id = self.chats.chat_ids[self.current_chat]
|
||||
return self.msgs.jump_bottom(chat_id)
|
||||
|
||||
def next_chat(self):
|
||||
if self.current_chat < len(self.chats.chats):
|
||||
self.current_chat += 1
|
||||
def next_chat(self, step=1):
|
||||
new_idx = self.current_chat + step
|
||||
if new_idx < len(self.chats.chats):
|
||||
self.current_chat = new_idx
|
||||
return True
|
||||
return False
|
||||
|
||||
def prev_chat(self):
|
||||
if self.current_chat > 0:
|
||||
self.current_chat -= 1
|
||||
def prev_chat(self, step=1):
|
||||
if self.current_chat == 0:
|
||||
return False
|
||||
self.current_chat = max(0, self.current_chat - step)
|
||||
return True
|
||||
|
||||
def first_chat(self):
|
||||
if self.current_chat != 0:
|
||||
self.current_chat = 0
|
||||
return True
|
||||
return False
|
||||
|
||||
def next_msg(self):
|
||||
def next_msg(self, step=1):
|
||||
chat_id = self.chats.chat_ids[self.current_chat]
|
||||
return self.msgs.next_msg(chat_id)
|
||||
return self.msgs.next_msg(chat_id, step)
|
||||
|
||||
def prev_msg(self):
|
||||
def prev_msg(self, step=1):
|
||||
chat_id = self.chats.chat_ids[self.current_chat]
|
||||
return self.msgs.prev_msg(chat_id)
|
||||
return self.msgs.prev_msg(chat_id, step)
|
||||
|
||||
def jump_next_msg(self):
|
||||
chat_id = self.chats.chat_ids[self.current_chat]
|
||||
|
@ -156,11 +163,12 @@ class MsgModel:
|
|||
self.msgs = defaultdict(list) # Dict[int, list]
|
||||
self.current_msgs = defaultdict(int)
|
||||
|
||||
def next_msg(self, chat_id):
|
||||
if self.current_msgs[chat_id] > 0:
|
||||
self.current_msgs[chat_id] -= 1
|
||||
return True
|
||||
return False
|
||||
def next_msg(self, chat_id, step=1):
|
||||
current_msgs = self.current_msgs[chat_id]
|
||||
if current_msgs == 0:
|
||||
return False
|
||||
self.current_msgs[chat_id] = max(0, current_msgs - step)
|
||||
return True
|
||||
|
||||
def jump_bottom(self, chat_id):
|
||||
if self.current_msgs[chat_id] == 0:
|
||||
|
@ -169,21 +177,15 @@ class MsgModel:
|
|||
return True
|
||||
|
||||
def jump_next_msg(self, chat_id):
|
||||
if self.current_msgs[chat_id] - 10 > 0:
|
||||
self.current_msgs[chat_id] -= 10
|
||||
else:
|
||||
self.current_msgs[chat_id] = 0
|
||||
return True
|
||||
return self.next_msg(chat_id, step=10)
|
||||
|
||||
def jump_prev_msg(self, chat_id):
|
||||
if self.current_msgs[chat_id] + 10 < len(self.msgs[chat_id]):
|
||||
self.current_msgs[chat_id] += 10
|
||||
return True
|
||||
return False
|
||||
return self.prev_msg(chat_id, step=10)
|
||||
|
||||
def prev_msg(self, chat_id):
|
||||
if self.current_msgs[chat_id] < len(self.msgs[chat_id]):
|
||||
self.current_msgs[chat_id] += 1
|
||||
def prev_msg(self, chat_id, step=1):
|
||||
new_idx = self.current_msgs[chat_id] + step
|
||||
if new_idx < len(self.msgs[chat_id]):
|
||||
self.current_msgs[chat_id] = new_idx
|
||||
return True
|
||||
return False
|
||||
|
||||
|
|
|
@ -4,6 +4,13 @@ import os
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def num(value, default=None):
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
|
||||
def notify(msg, subtitle='New message', title='Telegram'):
|
||||
msg = '-message {!r}'.format(msg)
|
||||
subtitle = '-subtitle {!r}'.format(subtitle)
|
||||
|
|
|
@ -4,8 +4,13 @@ import math
|
|||
import re
|
||||
from datetime import datetime
|
||||
|
||||
from utils import num
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
MAX_KEYBINDING_LENGTH = 5
|
||||
MULTICHAR_KEYBINDINGS = ("gg",)
|
||||
|
||||
|
||||
class View:
|
||||
|
||||
|
@ -48,22 +53,29 @@ class View:
|
|||
def draw_msgs(self, current, msgs):
|
||||
self.msgs.draw(current, msgs)
|
||||
|
||||
def get_key(self, y, x):
|
||||
# return self.stdscr.getkey()
|
||||
def get_keys(self, y, x):
|
||||
keys = repeat_factor = ''
|
||||
|
||||
ch = self.stdscr.getch(y, x)
|
||||
log.info('raw ch without unctrl: %s', ch)
|
||||
try:
|
||||
return curses.unctrl(ch).decode()
|
||||
except UnicodeDecodeError:
|
||||
log.warning('cant uncrtl: %s', ch)
|
||||
return 'UNKNOWN'
|
||||
for _ in range(MAX_KEYBINDING_LENGTH):
|
||||
ch = self.stdscr.getch(y, x)
|
||||
log.info('raw ch without unctrl: %s', ch)
|
||||
try:
|
||||
key = curses.unctrl(ch).decode()
|
||||
except UnicodeDecodeError:
|
||||
log.warning('cant uncrtl: %s', ch)
|
||||
break
|
||||
if key.isdigit():
|
||||
repeat_factor += key
|
||||
continue
|
||||
keys += key
|
||||
# if match found or there are not any shortcut matches at all
|
||||
if all(
|
||||
p == keys or not p.startswith(keys)
|
||||
for p in MULTICHAR_KEYBINDINGS
|
||||
):
|
||||
break
|
||||
|
||||
def get_key_input(self, y, x):
|
||||
ch = self.msgs.win.getch(y, x)
|
||||
log.info('raw ch without unctrl in msgs: %s', ch)
|
||||
return ch
|
||||
# return curses.unctrl(ch).decode()
|
||||
return num(repeat_factor, default=1), keys or 'UNKNOWN'
|
||||
|
||||
def get_input(self):
|
||||
return self.status.get_input()
|
||||
|
|
Loading…
Add table
Reference in a new issue