Add colour package for JSON and YAML highlighting #4

Merged
benvin merged 3 commits from benvin/colour into main 2026-10-23 22:44:32 +11:00
Member

CLI tools such as vctx print vault JSON/YAML to terminals with no highlighting, and each would otherwise grow its own colouriser.

  • add colour.JSON with jq's default palette and jq 1.8.1 JQ_COLORS parsing
  • add colour.YAML with yq's palette and number rules for block maps/seqs, single- and multi-line scalars, |/> blocks, flow collections, anchors and comments
  • re-emit input byte for byte between escapes; invalid input passes through uncoloured
  • add colour.Enabled (Auto/Always/Never) honouring TTY, NO_COLOR and TERM=dumb
CLI tools such as vctx print vault JSON/YAML to terminals with no highlighting, and each would otherwise grow its own colouriser. - add `colour.JSON` with jq's default palette and jq 1.8.1 `JQ_COLORS` parsing - add `colour.YAML` with yq's palette and number rules for block maps/seqs, single- and multi-line scalars, `|`/`>` blocks, flow collections, anchors and comments - re-emit input byte for byte between escapes; invalid input passes through uncoloured - add `colour.Enabled` (Auto/Always/Never) honouring TTY, `NO_COLOR` and `TERM=dumb`
unkin-agent added 1 commit 2026-10-23 22:00:47 +11:00
Add colour package for jq/yq-style JSON and YAML highlighting
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2594d3d946
Author
Member
  • colour/yaml.go:56 — block = indent uses the line indent, so a block scalar opened on a dash line (- cert: |) swallows the item's sibling keys as string content; real vault kv get -format=yaml with a list of maps renders ttl: 768h after the PEM all green → set the threshold to the column of the key / innermost - and add a - a: |\n x\n b: 1\n case to TestYAMLEdges
  • nit: colour/yaml.go:55 — continuation lines of a wrapped quoted/plain scalar (vault wraps long strings at ~80 cols) are parsed as fresh lines, so folded: onto next line" gets key colour → track an open multi-line quoted scalar or leave continuation lines plain
  • nit: colour/yaml.go:24 — diverges from yq (goccy lexer): 1e3 is green in yq, 0o17/.inf/012 uncoloured, 1_000 magenta → drop the claim of matching yq for these or align the regex
  • nit: colour/colour.go:74 — jq stops parsing after the 8th field, so …:4;31x sets 4;31; this rejects the whole variable → validate only the digits/; prefix of the 8th field
  • nit: colour/colour.go:25 — jq does not check TERM=dumb → drop "follows jq" or say TERM=dumb is an addition
- colour/yaml.go:56 — `block = indent` uses the line indent, so a block scalar opened on a dash line (`- cert: |`) swallows the item's sibling keys as string content; real `vault kv get -format=yaml` with a list of maps renders `ttl: 768h` after the PEM all green → set the threshold to the column of the key / innermost `-` and add a `- a: |\n x\n b: 1\n` case to TestYAMLEdges - nit: colour/yaml.go:55 — continuation lines of a wrapped quoted/plain scalar (vault wraps long strings at ~80 cols) are parsed as fresh lines, so ` folded: onto next line"` gets key colour → track an open multi-line quoted scalar or leave continuation lines plain - nit: colour/yaml.go:24 — diverges from yq (goccy lexer): `1e3` is green in yq, `0o17`/`.inf`/`012` uncoloured, `1_000` magenta → drop the claim of matching yq for these or align the regex - nit: colour/colour.go:74 — jq stops parsing after the 8th field, so `…:4;31x` sets `4;31`; this rejects the whole variable → validate only the digits/`;` prefix of the 8th field - nit: colour/colour.go:25 — jq does not check `TERM=dumb` → drop "follows jq" or say TERM=dumb is an addition
unkin-agent added 1 commit 2026-10-23 22:16:46 +11:00
colour: fix block scalars under list items, multi-line scalars, yq numbers and JQ_COLORS parsing
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
37112ddf68
Author
Member
  • colour/yaml.go:135 — '?' rejects every key starting with ?, but yaml.v2/v3 emit plain keys like ?q: unquoted (indicator only when followed by space/EOL); the line is painted as an open plain scalar, so the key and its whole nested block (?q:\n nested: 1\n pem: |) come out green where yq colours key/number/string → treat ? as an indicator only when followed by a space or end of line, as for -, and add a case to TestYAMLEdges
- colour/yaml.go:135 — `'?'` rejects every key starting with `?`, but yaml.v2/v3 emit plain keys like `?q:` unquoted (indicator only when followed by space/EOL); the line is painted as an open plain scalar, so the key and its whole nested block (`?q:\n nested: 1\n pem: |`) come out green where yq colours key/number/string → treat `?` as an indicator only when followed by a space or end of line, as for `-`, and add a case to TestYAMLEdges
unkin-agent added 1 commit 2026-10-23 22:33:55 +11:00
colour: treat ? as a YAML key indicator only before space or EOL
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
bd61a52463
Author
Member

No findings.

No findings.
benvin merged commit 89a61da1fb into main 2026-10-23 22:44:32 +11:00
benvin deleted branch benvin/colour 2026-10-23 22:44:32 +11:00
Sign in to join this conversation.