~callum/barszcz

fb244ef0323ed7b25c2dde9823b62eae7d42b89d — Callum Brown 1 year, 8 months ago ba0f227
Subsets of a LibModelThing's attributes with more
2 files changed, 91 insertions(+), 12 deletions(-)

M README.md
M beetsplug/barszcz.py
M README.md => README.md +34 -4
@@ 89,10 89,18 @@ Default: [n] = 1.
Change focus down [n] things (wraps).
Default: [n] = 1.

#### `more`
#### `more [subset]`
Show more information about the focused thing.
If an album or item is focused, its attributes will be listed.
If an attribute is focused, albums and items with that attribute will be listed.

If an album or item is focused, its attributes will be listed, and the optional
[subset] argument can be used to specify which subset of attributes will be
listed: if [subset] is `all`, all attributes will be listed; if it is `set`,
only attributes with a set (non-blank) value will be listed; if it is `config`,
only attributes specified in the configuration will be listed.
Default: [subset] = `config`.

If an attribute is focused, albums and items with that attribute will be listed
using the beets query `key:value`, and the [subset] argument has no effect.

#### _blank_
A blank command clears the input line and shortcut input.


@@ 133,6 141,11 @@ and if `left` or `right` is left blank, nothing will be displayed on that side.
`small` is used for the number of blank lines between attributes when viewing
more detail about an item or album.

- `item_attributes` determines which attributes should be displayed
(and in what order) when using the `more config` command on an item.

- `album_attributes` is as above but for albums.

### Shortcuts

- `shortcuts` is a collection of key-value pairs of the form


@@ 172,6 185,21 @@ barszcz:
	gap:
		regular: 1
		small: 0
	item_attributes:
		- album
		- artist
		- albumartist
		- composer
		- year
		- original_year
		- genre
		- format
	album_attributes:
		- album
		- albumartist
		- year
		- original_year
		- genre
	shortcuts:
		KEY_UP: up {}
		k: up {}


@@ 180,6 208,8 @@ barszcz:
		f: focus {}
		gg: focus 0
		G: focus -1
		KEY_ENTER: more
		KEY_ENTER: more config
		ma: more all
		ms: more set
		KEY_ESCAPE: ""
```

M beetsplug/barszcz.py => beetsplug/barszcz.py +57 -8
@@ 37,6 37,23 @@ config["barszcz"].add(
            "regular": 1,
            "small": 0,
        },
        "item_attributes": [
            "album",
            "artist",
            "albumartist",
            "composer",
            "year",
            "original_year",
            "genre",
            "format",
        ],
        "album_attributes": [
            "album",
            "albumartist",
            "year",
            "original_year",
            "genre",
        ],
        "shortcuts": {
            "KEY_UP": "up {}",
            "k": "up {}",


@@ 45,7 62,9 @@ config["barszcz"].add(
            "f": "focus {}",
            "gg": "focus 0",
            "G": "focus -1",
            "KEY_ENTER": "more",
            "KEY_ENTER": "more config",
            "ma": "more all",
            "ms": "more set",
            "KEY_ESCAPE": "", # Clears input line
        },
    }


@@ 255,6 274,8 @@ class ItemThing(LibModelThing):
    format_right = config["barszcz"]["format_item"]["right"].get() or []
    # The number of lines the Thing takes up (minimum 1)
    height = max(len(format_left), len(format_right), 1)
    # Limited set of attributes to display normally
    display_attributes = config["barszcz"]["item_attributes"].get() or []


class AlbumThing(LibModelThing):


@@ 266,6 287,8 @@ class AlbumThing(LibModelThing):
    format_right = config["barszcz"]["format_album"]["right"].get() or []
    # The number of lines the Thing takes up (minimum 1)
    height = max(len(format_left), len(format_right), 1)
    # Limited set of attributes to display normally
    display_attributes = config["barszcz"]["album_attributes"].get() or []


class Group:


@@ 470,22 493,40 @@ class Barszcz:
        current_focus = self.current_group.focus
        self.focus(current_focus + n)

    def more(self):
    def more(self, subset="config"):
        """Display more information about the focused `Thing`.

        If the focused `Thing` is a `LibModelThing`, display its attributes.
        If it is an `AttributeThing`, list `Item`s and `Album`s with that
        attribute
        attribute.

        Arguments:
            subset: string, one of "all", "set", or "config".
              Determines which subset of a `LibModelThing`'s attributes
              should be displayed.
        """
        thing = self.current_group.focused_thing()

        if isinstance(thing, LibModelThing):
            lm = thing.libmodel
            # Get all fields, including computed ones
            attributes = [AttributeThing(k, str(lm.get(k))) for k in lm.keys()]
            attribute_things = []
            if subset == "config":
                # Display only configured attributes
                keys = thing.display_attributes
            else:
                keys = thing.libmodel.keys()
            for k in keys:
                v = thing.libmodel.get(k)
                # If `subset` is "set" values (nonblank), and the value for the
                # current key is set, then display the attribute.
                # If `subset` is not "set" (config or all), display the
                # attribute even if the value is unset/blank.
                if subset != "set" or (subset == "set" and v):
                    attribute_things.append(AttributeThing(k, str(v)))
            self.current_group = Group(
                [thing] + attributes,
                [thing] + attribute_things,
                *self.group_dimensions,
            )

        elif isinstance(thing, AttributeThing):
            # Use a list query to ensure the value is associated with the key
            # (not any other fields), and to avoid problems with escaping of


@@ 547,7 588,15 @@ class Barszcz:
            return self.ls(" ".join(split[1:]), albums=False)

        elif split[0] == "more":
            return self.more()
            if len(split) == 1:
                return self.more()
            elif len(split) == 2 and split[1] in ("all", "set", "config"):
                return self.more(subset=split[1])
            else:
                return (
                    "ERROR: 'more' takes one optional argument, which can be "
                    "either 'all', 'set', or 'config'"
                )

        else:
            return f"Unrecognised command '{split[0]}'"