[SąT dev] [en] keyboard shortcuts refactoring

Goffi goffi at goffi.org
Jeu 4 Sep 19:35:56 CEST 2014


G'day,

I have refactored keyboard shortcuts in Urwid SąText/Primitivus. Now 
everything is in a keys module, and actions have names (for example 
APP_QUIT for... quitting the application). See both keys modules to see 
the currently defined actions.

The system can seems complexe but it's not really, and it had a couple 
of check to avoid conflicts.

When you add a new action, you add it either it's name directly (then 
there will not be namespace check), or with one or several namespace.
You can't add an action which already exists.

examples:

### CODE ###
from urwid_satext.keys import action_key_map

action_key_map.update(
         {
         # Edit bar
         ("edit", "MODE_INSERTION"): "i",
         ("edit", "MODE_COMMAND"): ":",
         ("edit", "HISTORY_PREV"): "up",
         ("edit", "HISTORY_NEXT"): "down",
	})
### CODE ###

means
INSERTION, MODE_COMMAND, HISTORY_PREV and HISTORY_NEXT are in "edit" 
namespace. We could use two namespace:

action_key_map[(("edit", "modal"), "MODE_COMMAND")] = "i"

or no namespace at all:

action_key_map["MODE_COMMAND"] = "i"

Namespaces are used to check conflicts. If "i" key is used more than one 
time in "edit" namespace, then a ConflictError will be raised.

You can also specify namespace links. For example, if you know that 
"input" keys are often in dialogs ("dialog" namespace), you can specify 
it in a tuple:

action_key_map.set_close_namespaces((("input", "dialog"),))

note that this tuple is itself in a tuple.
That means that if a key is used both in "input" namespace and "dialog" 
namespace, a ConflictError will be raised.

A second argument can be used in set_close_namespaces, this one 
indicates namespaces that should always checked.

For example, in Primitivus, we have:

action_key_map.set_close_namespaces(tuple(), ('global', 'focus', 
'menu_global'))

That means that no key used in 'global', 'focus' or 'menu_global' 
namespaces can be used elsewhere. So if somebody want to use "ctrl x" in 
e.g. chat panel, it's not possible because it's already used in APP_QUIT 
which is in menu_global namespace.

If you want to replace a shortcut, you must use replace_shorcut (for 
one) or replace (for several at once). In this case the action must 
already exists, and you must not use namespace (which has no sense in 
this case).

Keys can now be changed in sat.conf, in the [primivitus] section.

to change a key, use its action name with "key_" prefix (case 
insensitive). In case of error (e.g. a conflict), config parsing will 
stop, and you'll have an error popup, with details available in logs 
(:messages command).

example: you want to use [shift tab] to switch focus:

you put this in sat.conf


### CONF ###
[primitivus]
key_focus_switch = shift tab
### CONF ###

But when you try it, you fin an error popup. You type in command mode 
[Esc + ":"] the :messages command, you find this:

ERROR: configuration error: shortcut [shift tab] is used both in 
namespaces "edit" and "focus"

Indeed, [shift tab] is used for completion, so you change your sat.conf 
as follow:

### CONF ###
[primitivus]
key_focus_switch = shift tab
key_edit_complete = tab
### CONF ###

And now that works ! You can change focus with [shift tab] and complete 
with [tab].


Enjoy
Goffi



Plus d'informations sur la liste de diffusion dev