~callum/barszcz

e5b4bec6bbca61fe1bde6e32460188807ffeb2ed — Callum Brown 1 year, 10 months ago b253bbd
Make shortcuts configurable

Implements: https://todo.sr.ht/~callum/barszcz/3
2 files changed, 44 insertions(+), 24 deletions(-)

M README.md
M beetsplug/barszcz.py
M README.md => README.md +25 -11
@@ 49,25 49,20 @@ Required arguments are CAPITALISED.
| focus [INDEX] |            | Change focus to the thing at the given [INDEX]. A negative [INDEX] focuses the last thing. |
| up [n]        |            | Change focus up [n] things (wraps). Default: [n] = 1. |
| down [n]      |            | Change focus down [n] things (wraps). Default: [n] = 1. |
|               |            | Blank command clears input line. |


### Shortcuts

Single and multi-character shortcuts can be used to execute commands without
typing them in.
An `[n]` preceding a shortcut indicates an optional sequence of digits.
The available shortcuts are:
A sequence of digits preceding a shortcut can be passed as an argument to
commands, for example typing `3` followed by the down arrow key will move the
focus three things down (using the default configuration).

| shortcut   | command |
| ---        | --- |
| [n]KEY_UP     | up [n] |
| [n]KEY_DOWN   | down [n] |
| [n]f       | focus [n] |
| gg         | focus 1 |
| G          | focus -1 |
| KEY_ESCAPE | _blank, clears the input line_ |
For the default shortcuts and how to define shortcuts, see the Configuration
section.

Shortcuts will be configurable in the future.

## Configuration



@@ 85,6 80,16 @@ and if `left` or `right` is left blank, nothing will be displayed on that side.

- `gap` is the number of blank lines between each item or album.

- `shortcuts` is a collection of key-value pairs of the form
`shortcut sequence: command`.
A single pair of curly brackets `{}` in the command will be replaced by the
sequence of digits input immediately prior to the shortcut sequence.
The escape key is used to clear the input line and current shortcut sequence.
For a list of key constants that can be used for special keys
(e.g. `KEY_ENTER`), see the "Key constant" table on the
[constants section](https://docs.python.org/3/library/curses.html#constants)
of the Python curses documentation.


Default configuration:



@@ 104,4 109,13 @@ barszcz:
		right:
			- $albumtotal tracks
	gap: 1
	shortcuts:
		KEY_UP: up {}
		k: up {}
		KEY_DOWN: down {}
		j: down {}
		f: focus {}
		gg: focus 0
		G: focus -1
		KEY_ESCAPE: ""
```

M beetsplug/barszcz.py => beetsplug/barszcz.py +19 -13
@@ 15,6 15,7 @@
import curses
from curses.textpad import Textbox

import confuse
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, human_seconds_short
from beets import config


@@ 33,6 34,16 @@ config["barszcz"].add(
            "right": ["$albumtotal tracks"],
        },
        "gap": 1,
        "shortcuts": {
            "KEY_UP": "up {}",
            "k": "up {}",
            "KEY_DOWN": "down {}",
            "j": "down {}",
            "f": "focus {}",
            "gg": "focus 0",
            "G": "focus -1",
            "KEY_ESCAPE": "", # Clears input line
        },
    }
)



@@ 353,15 364,6 @@ class Group:

class Barszcz:
    running = True
    # Will be moved to configuration at some point
    shortcuts = {
        "KEY_UP": "up {}",
        "KEY_DOWN": "down {}",
        "f": "focus {}",
        "gg": "focus 1",
        "G": "focus -1",
        "KEY_ESCAPE": "", # Clears input line
    }

    def __init__(self, lib, opts, args):
        self.lib = lib


@@ 515,6 517,10 @@ class Barszcz:

        self.set_group_dimensions(height, width)

        # Get default + user defined shortcuts (which override defaults)
        shortcuts = config["barszcz"]["shortcuts"].get(
            confuse.MappingValues(str)
        )
        # Currently inputed shortcut character sequence.
        shortcut = ""
        # Currently inputed number of repetitions for the shortcut.


@@ 586,16 592,16 @@ class Barszcz:
                    # of times a shortcut input in future should be repeated.
                    # NOTE: This means shortcuts can not start with a digit.
                    shortcut_reps += key
                elif key in self.shortcuts:
                elif key in shortcuts:
                    # If the key just pressed is a shortcut on its own,
                    # just do that rather than add it to the existing sequence.
                    command = self.shortcuts[key]
                    command = shortcuts[key]
                else:
                    # Add to sequence of characters currently being input
                    shortcut += key
                    if shortcut in self.shortcuts:
                    if shortcut in shortcuts:
                        # Do the multi-character shortcut
                        command = self.shortcuts[shortcut]
                        command = shortcuts[shortcut]

                input_win.clear()
                # NOTE: just `if command:` would disallow the command being "",