Commit d6f2a9a5 authored by Jacek Sowiński's avatar Jacek Sowiński

Correctly recognize special keys in key-shortcuts

Special keys in config should follow similiar syntax to that used in
vim:

left arrow = `<Left>`
right arrow = `<Right>`
page down = `<Page down>`
home = `<Home>`
esc = `<Esc>`
F4 = `<F4>`
Ctrl+s = `<C-s>`
Meta+k = `<M-k>`

Examples:
=========

```ini
[column_keymap]
<Left> = prev_column
<Right> = next_column
<Esc>k = home
<C-s> = tag {} foo 1
<M-s> = tag {} foo
```
parent 52a04262
[column_keymap]
up = up
<Left> = prev_column
<Esc>d = delete_column
......@@ -159,5 +159,12 @@ class ConfigTest(TopydoTest):
self.assertEqual(keymap['up'], 'up')
self.assertIn('u', keystates)
self.assertEqual(keymap['<Left>'], 'prev_column')
self.assertNotIn('<Lef', keystates)
self.assertEqual(keymap['<Esc>d'], 'delete_column')
self.assertNotIn('<Esc', keystates)
self.assertIn('<Esc>', keystates)
if __name__ == '__main__':
unittest.main()
......@@ -88,3 +88,5 @@ D = delete_column
Y = copy_column
L = swap_left
R = swap_right
<Left> = prev_column
<Right> = next_column
......@@ -16,6 +16,7 @@
import configparser
import os
import re
import shlex
from itertools import accumulate
......@@ -136,6 +137,8 @@ class _Config:
'Y': 'copy_column',
'L': 'swap_left',
'R': 'swap_right',
'<Left>': 'prev_column',
'<Right>': 'next_column',
},
}
......@@ -350,8 +353,9 @@ class _Config:
for combo, action in shortcuts:
# add all possible prefixes to keystates
if len(combo) > 1:
keystates |= set(accumulate(combo[:-1]))
combo_as_list = re.split('(<[A-Z].+?>|.)', combo)[1::2]
if len(combo_as_list) > 1:
keystates |= set(accumulate(combo_as_list[:-1]))
if action in ['pri', 'postpone', 'postpone_s']:
keystates.add(combo)
......
......@@ -94,3 +94,18 @@ def get_terminal_size(p_getter=None):
get_terminal_size.getter = inner
return get_terminal_size.getter()
def translate_key_to_config(p_key):
"""
Translates urwid key event to form understandable by topydo config parser.
"""
if len(p_key) > 1:
key = p_key.capitalize()
if key.startswith('Ctrl') or key.startswith('Meta'):
key = key[0] + '-' + key[5:]
key = '<' + key + '>'
else:
key = p_key
return key
......@@ -232,7 +232,6 @@ class UIApplication(CLIApplicationBase):
self.column_mode = _COPY_COLUMN
self._viewwidget_visible = True
def _column_action_handler(self, p_action):
dispatch = {
'first_column': self._focus_first_column,
......@@ -252,8 +251,6 @@ class UIApplication(CLIApplicationBase):
def _handle_input(self, p_input):
dispatch = {
':': self._focus_commandline,
'left': self._focus_previous_column,
'right': self._focus_next_column,
}
try:
......
......@@ -17,6 +17,7 @@
import urwid
from topydo.ui.TodoWidget import TodoWidget
from topydo.lib.Utils import translate_key_to_config
class PendingAction(object):
......@@ -135,7 +136,7 @@ class TodoListWidget(urwid.LineBox):
keymap, keystates = self.keymap
shortcut = self.keystate or ''
shortcut += p_key
shortcut += translate_key_to_config(p_key)
try:
action = keymap[shortcut]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment