Skip to content

Conversation

@yertto
Copy link

@yertto yertto commented Jun 1, 2025

Overview

This PR introduces a new @table string output formatter to jq.

Examples

(Nb. using -n / --null-input, -r / --raw-output)

  •  jq -nr '[["one","two","three"],[1,2,3]] | @table'
    
    ┌───┬───┬─────┐
    │one│two│three│
    │1  │2  │3    │
    └───┴───┴─────┘
    
  •  jq -nr '{"headings":["one","two","three"],"rows":[[1,2],["unicycle","","tricycle"]]} | @table'
    
    ┌────────┬───┬────────┐
    │one     │two│three   │
    ├────────┼───┼────────┤
    │1       │2  │        │
    │unicycle│   │tricycle│
    └────────┴───┴────────┘
    

@yertto yertto marked this pull request as draft June 1, 2025 09:17
@yertto yertto force-pushed the yertto/table branch 13 times, most recently from 8c1485a to 29b63d6 Compare June 5, 2025 12:12
yertto added 16 commits June 5, 2025 22:17
❯ echo '[["foo", "bar", "baz"], ["a", "b", "c"], [1, null, 3]]' | ./jq --raw-output '@table'
┌───┬────┬───┐
│foo│bar │baz│
├───┼────┼───┤
│a  │b   │c  │
│1  │null│3  │
└───┴────┴───┘
❯ echo '{"headings": ["foo", "bar", "baz"], "rows": [["a", "b", "c"], [1, null, 3]]}' | ./jq --raw-output '@table'
┌───┬────┬───┐
│foo│bar │baz│
├───┼────┼───┤
│a  │b   │c  │
│1  │null│3  │
└───┴────┴───┘

❯ echo '[["foo", "bar", "baz"], ["a", "b", "c"], [1, null, 3]]' | ./jq --raw-output '@table'
┌───┬────┬───┐
│foo│bar │baz│
│a  │b   │c  │
│1  │null│3  │
└───┴────┴───┘
❯ echo '{"rows": [["a", "b", "c"], [1, null, 3]]}' | ./jq --raw-output '@table'
┌─┬────┬─┐
│a│b   │c│
│1│null│3│
└─┴────┴─┘
❯ ./jq --raw-output '@table' <<-EOF
[
  {"false":"true", "bartrue":"false", "baz":"TRUE"},
  {"false":1, "bartrue":null, "baz":0},
  {"false":"true false", "bartrue":"FALSE TRUE", "baz":"falsetrue"}
]
EOF
┌──────────┬─────────┬──────────┐
│bartrue   │baz      │false     │
├──────────┼─────────┼──────────┤
│false     │TRUE     │true      │
│null      │0        │1         │
│FALSE TRUE│falsetrue│true false│
└──────────┴─────────┴──────────┘
❯ ./jq --raw-output '@table' <<-EOF
[
  {"false":"true", "bartrue":"false", "baz":"TRUE"},
  {"false":1, "bartrue":null, "baz":0},
  {"false":"true false", "bartrue":"FALSE TRUE", "baz":"falsetrue"}
]
EOF
┌──────────┬──────────┬─────────┐
│false     │bartrue   │baz      │
├──────────┼──────────┼─────────┤
│true      │false     │TRUE     │
│1         │null      │0        │
│true false│FALSE TRUE│falsetrue│
└──────────┴──────────┴─────────┘
❯ ./jq --raw-output '@table' <<-EOF
{
  "headings": ["baz", "bartrue"],
  "rows": [
    {"false":"true", "bartrue":"false", "baz":"TRUE"},
    {"false":1, "bartrue":null, "baz":0},
    {"false":"true false", "bartrue":"FALSE TRUE", "baz":"falsetrue"}
  ],
  "color_words": ["(no|NO|false|FALSE)", "(yes|YES|true|TRUE)", "null"]
}
EOF
┌─────────┬──────────┐
│baz      │bartrue   │
├─────────┼──────────┤
│TRUE     │false     │
│0        │null      │
│falsetrue│FALSE TRUE│
└─────────┴──────────┘
@yertto yertto force-pushed the yertto/table branch 5 times, most recently from b8429b3 to 3cc7244 Compare June 5, 2025 12:39
@yertto yertto changed the title Add a @table formatter with object/array row handling Add a @table formatter with optional headings Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant