Metadata-Version: 2.4
Name: rich-metadata
Version: 0.2.0
Summary: A Rich-based TUI toolkit for building metadata browsers.
Keywords: rich,tui,metadata,terminal,console
Author: Gabriel Jung
Author-email: Gabriel Jung <gabriel.jung@protonmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Sound/Audio
Classifier: Topic :: Software Development :: Libraries
Requires-Dist: rich>=13.0.0
Requires-Dist: loguru>=0.7.3
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/gabriel-jung/rich-metadata
Project-URL: Repository, https://github.com/gabriel-jung/rich-metadata
Project-URL: Bug Tracker, https://github.com/gabriel-jung/rich-metadata/issues
Description-Content-Type: text/markdown

# rich-metadata

A Rich-based TUI toolkit for building interactive metadata browsers.

Define your entities declaratively, wire up your API, and get an interactive terminal browser with pagination, navigation, lazy loading, and image support.

## Install

```bash
pip install rich-metadata
```

## Quick start

```python
from rich_metadata import (
    BaseNavigator,
    DisplayEngine,
    EntityDef,
    HeaderField,
    HeaderLink,
    SectionDef,
    SummaryField,
    TableColumn,
)

# 1. Define your entities
book_def = EntityDef(
    type_name="book",
    summary=[
        SummaryField(key="title", style="bold"),
        SummaryField(prefix="by ", key="author"),
        SummaryField(key="year", style="dim"),
    ],
    header_fields=[
        HeaderField("Author", key="author"),
        HeaderField("Year", key="year"),
        HeaderField("Genre", key="genre"),
        HeaderField("Pages", key="pages"),
        HeaderField("Publisher", key="publisher"),
    ],
    sections=[
        SectionDef(
            "chapters",
            navigable=True,
            columns=[
                TableColumn("#", "number", width=4),
                TableColumn("Title", "title", style="bold"),
                TableColumn("Pages", "pages"),
            ],
        ),
        SectionDef("description", lazy=True),
    ],
    header_links=[
        HeaderLink("Author: {author}", "author", ref_key="author_url"),
    ],
    footer=["url"],
)

# 2. Create a display engine and register definitions
engine = DisplayEngine()
engine.register(book_def)

# 3. Use the engine directly
entity = {"_type": "book", "title": "Dune", "author": "Frank Herbert", "year": "1965"}
engine.details(entity)    # render header + all sections
engine.summary(entity)    # one-line summary
engine.header(entity)     # header panel with image

# 4. Or wire up a navigator for interactive browsing
class BookAPI:
    def get(self, ref: str, *, full: bool = False, **kwargs) -> dict | None:
        ...  # fetch entity by URL/ID, return dict with "_type" key
        # `full=True` is passed when the caller wants every section eagerly
        # populated (e.g. `--json` / `--full` flows). Extra kwargs are forwarded.

    def search(self, query: str, exact_match: bool = True) -> list[dict]:
        ...  # return list of entity dicts (each with "_type" and "name")

navigator = BaseNavigator(
    engine,
    apis={"book": BookAPI(), "author": AuthorAPI()},
    entity_ref_key="url",
    lazy_fetchers={
        ("book", "description"): lambda api, entity: api.fetch_description(entity["id"]),
    },
)
navigator.navigate(entity)  # interactive section menu
navigator.browse(fetch_page=my_search_fn)  # paginated results
```

## Core concepts

### Entity dicts

Entities are plain Python dicts with a `_type` key for routing:

```python
{"_type": "book", "title": "Dune", "author": "Frank Herbert", "year": "1965"}
```

### EntityDef

Declares how an entity type is displayed. Each `EntityDef` configures:

| Field | Purpose |
|-------|---------|
| `type_name` | Entity type identifier (matches `_type` in dicts) |
| `summary` | One-line display fields (`SummaryField` list) |
| `header_fields` | Key-value pairs in the detail panel (`HeaderField` list) |
| `header_image_key` | Dict key holding image bytes for the header panel |
| `header_title` | Custom title callable `(dict) -> str` |
| `panel_border_style` | Rich style for the header panel border |
| `sections` | Expandable content sections (`SectionDef` list) |
| `header_links` | Navigable links shown in the section menu (`HeaderLink` list) |
| `footer` | Keys (strings) or callables `(dict) -> str \| None` for lines below the panel |
| `auto_full` | If True, render every section inline (no menu) and offer prev/next sibling navigation |

### SectionDef

Defines a content section. The rendering mode is auto-detected:

- Has `columns` -> **table** (if data is a `list`, rows are flat; if `dict[str, list]`, rows are grouped with headers)
- Has `custom_render` -> **custom** rendering function
- Neither -> **text** panel

Key options: `navigable` (items can be drilled into), `lazy` (fetched on demand), `duration_key` (sums and shows total duration).

### SummaryField

One segment of a one-line entity summary. Supports `style`, `prefix` (text before the value), `fallback` (shown when key is missing), and `transform` (receives value if `key` is set, or the whole entity dict if not).

### HeaderField

A labeled row in the detail panel. Either reads from `key` directly, or computes via `transform` (receives value if `key` is set, or the whole entity dict if not).

### TableColumn

A column in a table section. Supports `style`, `justify`, `width`, and `transform`.

### HeaderLink

A navigable link shown in the section menu (e.g., "Author: Frank Herbert ->"). Uses `ref_key` to read a URL/ID from the entity dict, or `ref_fn(entity) -> str | None` for computed refs.

## DisplayEngine

The rendering engine. Key methods:

```python
engine = DisplayEngine()              # uses default Rich console
engine = DisplayEngine(my_console)    # custom console

engine.register(entity_def)           # register an EntityDef
engine.summary(entity)                # one-line summary
engine.header(entity)                 # detail panel with optional image
engine.section(entity, "chapters")    # render a single section
engine.details(entity)                # header + all non-lazy sections
engine.select_from_list(items)        # numbered selection prompt
```

## BaseNavigator

Interactive browser with pagination, back-navigation, and lazy fetching.

```python
navigator = BaseNavigator(
    engine,
    apis={"book": book_api, "author": author_api},
    entity_ref_key="url",
    lazy_fetchers={
        ("book", "description"): lambda api, entity: api.fetch_desc(entity["id"]),
    },
)
```

| Parameter | Purpose |
|-----------|---------|
| `apis` | `{type: api}`. Each API needs `.get(ref, *, full=False, **kwargs)` and (for `search_and_navigate`) `.search(query, exact_match=True)` |
| `entity_ref_key` | Key to extract the navigable ref from item dicts (default: `"url"`) |
| `lazy_fetchers` | `{(type, section): callable(api, entity) -> data}` for lazy sections |

Key methods:

- **`navigate(entity)`** -- Interactive loop: header, section menu, lazy fetching, header link navigation, back-navigation, and prev/next sibling navigation when `siblings=` is provided.
- **`display_or_navigate(entity, *, json_output=False, full=False)`** -- Dispatch one entity: JSON dump (internal keys stripped), full inline render, or interactive `navigate()`. Used by CLIs that share one code path for `--json` / `--full` / interactive modes.
- **`search_and_navigate(query, types, *, exact_first=True, json_output=False, full=False)`** -- Search across `types` (each API's `.search()` is called), prefer exact name matches when `exact_first`, then select and navigate. Passes `full=` to `.get()` so backends can fetch eagerly when needed.
- **`browse(fetch_page=..., *, render_page=None, title=None, page_size=25, full=False, loop=False)`** -- Paginated results with selection. `fetch_page(start, count) -> (results, total)`. `loop=True` re-displays the page after a child view returns (works with both `full` and interactive modes).
- **`browse_sources(sources, *, full=False, loop=True)`** -- Pick from named browsable sources (`[(label, fetch_page), ...]`), then browse the selected one. Skips the menu when only one source is supplied. `loop` is passed to `browse()`, so by default going back from an item returns to its list.

Items with `_type` but no ref (no `url` or whatever `entity_ref_key` is) are treated as **inline entities** -- navigated directly without fetching.

Inside the interactive views, if an API's `.get`, a lazy fetcher or a later page raises, the error is logged, the user sees "Could not fetch ..." and stays where they were, so one failed request does not end the session. `fetch_entity` itself, the first search or page, and the fetch in `search_and_navigate`'s `json_output` / `full` modes let errors propagate to your code. `search_and_navigate` returns False when nothing was found or the pick could not be shown, so a CLI can set a non-zero exit status.

### `QuitSignal`

The navigator raises `QuitSignal` (an `Exception` subclass) on Ctrl+C / EOF at its prompts or during a fetch, so the interactive loops can unwind cleanly. (The search result list from `DisplayEngine.select_from_list` treats Ctrl+C as cancel and returns.) Catch it at your CLI entry point to exit quietly:

```python
from rich_metadata import QuitSignal

try:
    navigator.navigate(entity)
except QuitSignal:
    pass
```

## CLI helpers

Shared CLI utilities:

```python
from rich_metadata import (
    configure_logging,     # loguru setup (debug if verbose, else warnings)
    resolve_entity_type,   # extract (type, query) from parsed args
    strip_internal_keys,   # remove _prefixed keys for JSON output
    list_fetcher,          # wrap a list into a fetch_page callable for browse()
    page_fetcher,          # adapt page-number APIs to browse()'s offset interface
    parse_date,            # parse 'YYYY-MM-DD' string to date
    parse_date_args,       # parse --from/--to argparse flags into dates
    months_in_range,       # list of 'YYYY-MM' strings covering a date range
)
```

`list_fetcher` and `page_fetcher` are adapters for `BaseNavigator.browse()`:

```python
# Wrap a pre-fetched list
navigator.browse(fetch_page=list_fetcher(my_items))

# Adapt a page-number API (fetch(page) -> (results, has_more))
navigator.browse(fetch_page=page_fetcher(api.search, first_page=initial_results))
```

## Image support

Terminal image rendering for iTerm2 and Kitty:

```python
from rich_metadata import get_image_escape, show_image_beside

# Get raw escape sequence for image bytes
escape = get_image_escape(image_bytes, width=20, height=10)

# Show an image beside a Rich renderable
show_image_beside(console, image_bytes, my_panel)
```

Kitty-protocol terminals (Kitty, Ghostty, recent VS Code) are sent PNG data. Other formats go through the iTerm2 protocol on VS Code, which speaks both, and are skipped on Kitty and Ghostty rather than sent as an image the terminal cannot show. iTerm2-protocol terminals take any format they can decode.

## Escaping data

Values the engine reads by `key` (summary and header fields, table cells, text sections, footer keys, group names) are escaped before they reach Rich markup, so `"Live [bonus]"` prints as written and a stray `"[/]"` cannot crash a render. Control characters are dropped too. Output of your own `transform` callables, `header_title` and footer callables is treated as markup, so escape any data you interpolate there:

```python
from rich_metadata import escape

HeaderField("Title", transform=lambda d: f"[bold]{escape(d['title'])}[/bold] ({d['year']})")
```

## Hyperlinks

```python
from rich_metadata import link, links_line, supports_hyperlinks

link("Homepage", url)                          # clickable label, or the raw URL
links_line(("site", url1), ("shop", url2))     # one "URL: site | shop" line, or one labelled URL per line
```

Detection follows the npm `supports-hyperlinks` rules; `FORCE_HYPERLINK=1` / `NO_HYPERLINKS=1` override it.

## Duration helpers

```python
from rich_metadata import parse_duration, format_duration

parse_duration("3:45")      # 225 (seconds)
parse_duration("1:02:30")   # 3750
format_duration(225)         # "3:45"
format_duration(3750)        # "1:02:30"
```

## License

MIT
