Demo: plain-text custom header colored by Kanta, rename migration, str extras
This commit is contained in:
+14
-19
@@ -212,33 +212,28 @@ def resolve_user_key(value: str) -> str | None:
|
||||
|
||||
- By default a transaction is logged with an `action by user` header followed
|
||||
by the diff lines. Added paths are colored green, deleted paths red.
|
||||
- `kanta.transaction(..., extra=...)` accepts display-only metadata that is
|
||||
used for logging and is never persisted in the `ChangeRecord`:
|
||||
- a string is appended literally after the action in the default header,
|
||||
- a dict is passed to a registered `@kanta.logheader` callback; if it has
|
||||
no `"target"` key, the database filename is inserted as the target.
|
||||
- Register a `@kanta.logheader` callback to replace the entire header line.
|
||||
It may declare `action: str`, `user: str | None` and `extra: dict | None`
|
||||
parameters, and can also have `DictPre`/`DictPost` state dicts and the
|
||||
`Kanta` instance injected. It must be synchronous and return `str | None`.
|
||||
- `kanta.transaction(..., extra="...")` accepts a display-only string that is
|
||||
appended after the action in the default header (colored by Kanta); it is
|
||||
never persisted in the `ChangeRecord`.
|
||||
- Register a `@kanta.logheader` callback to compose a custom header. Declare
|
||||
any of `action: str`, `user: str`, `extra: str`: Kanta passes the parts
|
||||
with its header colors already applied (missing `user`/`extra` as empty
|
||||
strings), so callbacks only arrange text — no color codes, fallbacks, or
|
||||
padding. `DictPre`/`DictPost` state dicts and the `Kanta` instance can
|
||||
also be injected. The callback must be synchronous and return `str | None`.
|
||||
- Multiple logheader callbacks are stacked in registration order; the first
|
||||
callback to return a non-`None` result wins. If all return `None`, Kanta
|
||||
falls back to the default header. The `user` value passed to the callback
|
||||
has already been through the `logfmt` formatters.
|
||||
falls back to the default header. The `user` part has already been through
|
||||
the `logfmt` formatters.
|
||||
- The header and diff parts can be toggled independently per transaction:
|
||||
`kanta.transaction(..., log={"header": True, "diff": False})`.
|
||||
|
||||
```python
|
||||
@kanta.logheader
|
||||
def format_header(action: str, user: str | None, extra: dict | None) -> str:
|
||||
session = extra.get("session_id", "-")
|
||||
return f"{user:<20} {session:>2} {action} {extra['target']}"
|
||||
def format_header(action: str, user: str, extra: str) -> str:
|
||||
return f"{user} {action} {extra}"
|
||||
|
||||
with kanta.transaction(
|
||||
action="update",
|
||||
user="alice",
|
||||
extra={"session_id": 3, "target": "Project Name (abcd1234)"},
|
||||
) as data:
|
||||
with kanta.transaction(action="update", user="alice", extra="Project X") as data:
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user