Use go templates to customize output
You can use go templates to create a new field (called _0 per default). Per default, the templates are applied at the input level, when rows are actually still full blown objects (if reading in from JSON for example).
❯ glaze json misc/test-data/[123].json --template '{{.a}}-{{.b}}: {{.d.f}}'
+---------------------+
| _0 |
+---------------------+
| 1-2: 7 |
| 10-20: 70 |
| 100-200: <no value> |
+---------------------+
You can also apply templates at the row level, once the input has been flattened.
In this case, because flattened columns contain the symbol ., fields get renamed
to use the symbol _ as a separator.
❯ glaze json misc/test-data/[123].json --template '{{.a}}-{{.b}}: {{.d_f}}' \
--use-row-templates --fields a,_0 \
--output csv
a,_0
1,1-2: 7
10,10-20: 70
100,100-200: <no value>
Instead of just adding / replacing everything with a single field _0, you
can also specify multiple templates using the --template-field argument, which has
the form COLNAME:TEMPLATE.
❯ glaze json misc/test-data/[123].json \
--template-field 'foo:{{.a}}-{{.b}},bar:{{.d_f}}' \
--use-row-templates --fields a,foo,bar
+-----+---------+------------+
| a | foo | bar |
+-----+---------+------------+
| 1 | 1-2 | 7 |
| 10 | 10-20 | 70 |
| 100 | 100-200 | <no value> |
+-----+---------+------------+
Please note that the --template-field flag is parsed by cobra as a slice value,
which uses the golang csv parser. If you want to use the " character in your field, you need to
jump through a few hoops. See the end of this file for a list of functions that can be used in
templates.
❯ glaze json misc/test-data/book.json \
--template-field '"french_author:{{ replace .author ""Poe"" ""Pouet"" }}"' \
--use-row-templates
+-----------------+-----------+-------------------+
| author | title | french_author |
+-----------------+-----------+-------------------+
| Edgar Allan Poe | The Raven | Edgar Allan Pouet |
+-----------------+-----------+-------------------+
To make things a bit more readable, especially when doing a lot of template transformations,
you can also load field templates from a yaml file using the @ symbol.
❯ glaze json misc/test-data/[123].json \
--template-field '@misc/template-field-object.yaml' \
--output json
[
{
"barbaz": "6 - 7",
"foobar": "1"
},
{
"barbaz": "60 - 70",
"foobar": "10"
},
{
"barbaz": "\u003cno value\u003e - \u003cno value\u003e",
"foobar": "100"
}
]
You can also use a template output formatter that takes a --template-file argument.
The template is rendered with an object that has a rows field.
❯ cat misc/template-file-example.tmpl.md
# Counts Rows {{ len .rows }}
{{ range $row := .rows }}
## Row {{.b}}
- c: {{.c}}
- a: {{.a}}
{{ end }}%
❯ glaze json misc/test-data/[123].json \
--output template \
--template-file misc/template-file-example.tmpl.md
# Counts Rows 3
## Row 2
- c: [3 4 5]
- a: 1
## Row 20
- c: [30 40 50]
- a: 10
## Row 200
- c: [300]
- a: 100
Additional data can be provided using the --template-data argument
which should point to a JSON/CSV/YAML file containing the data to be
loaded.
❯ glaze json misc/test-data/[123].json \
--template-data misc/test-data/book.json \
--template-file misc/template-file-example2.tmpl.md \
--output template
# The Raven
Author: Edgar Allan Poe
## Row 2
- c: [3 4 5]
- a: 1
## Row 20
- c: [30 40 50]
- a: 10
## Row 200
- c: [300]
- a: 100
Glazed uses the sprig templating library to provide many useful functions.
Furthermore, there is support for a variety of additional templating functions.
The following functions are available for computing inside templates:
add(a, b interface{}) - Add two numbers (supports int, uint, float)sub(a, b interface{}) - Subtract b from a (supports int, uint, float)mul(a, b interface{}) - Multiply two numbers (supports int, uint, float)div(a, b interface{}) - Divide a by b (supports int, uint, float)parseFloat(s string) float64 - Parse string to float64parseInt(s string) int64 - Parse string to int64currency(n interface{}) string - Format number as currency with 2 decimal places❯ glaze json misc/test-data/[123].json \
--template-field 'foo:{{.a}} + {{.b}} = {{add .a .b}},bar:{{.a}} * {{ .d_f }} = {{ mul .a .d_f}}' \
--use-row-templates --fields a,foo,bar
+-----+-----------------+-------------------------------+
| a | foo | bar |
+-----+-----------------+-------------------------------+
| 1 | 1 + 2 = 3 | 1 * 7 = 7 |
| 10 | 10 + 20 = 30 | 10 * 70 = 700 |
| 100 | 100 + 200 = 300 | 100 * <no value> = 0 |
+-----+-----------------+-------------------------------+
The following functions are available to manipulate strings:
trim(s string) string - Remove spaces from both endstrimRightSpace(s string) string - Remove spaces from the righttrimTrailingWhitespaces(s string) string - Remove trailing whitespacesrpad(s string, n int) string - Right pad a string with spacesquote(s string) string - Quote a string with backticksstripNewlines(s string) string - Remove newlinesquoteNewlines(s string) string - Replace newlines with \ntoUpper(s string) string - Convert to uppercasetoLower(s string) string - Convert to lowercasereplaceRegexp(s, old, new string) string - Replace using regexppadLeft(s string, n int) string - Pad with spaces on the leftpadRight(s string, n int) string - Pad with spaces on the rightpadCenter(s string, n int) string - Center text with spacestoUrlField(v interface{}) string - Convert value to URL field formattoYaml(v interface{}) string - Convert value to YAML formatindentBlock(indent int, value string) string - Indent text block❯ glaze json misc/test-data/book.json \
--template-field '"robot_author:{{ replaceRegexp .author ""([A-Za-z])"" ""."" | toUpper }}"' \
--use-row-templates
+-----------------+-----------+------------------------------+
| author | title | robot_author |
+-----------------+-----------+------------------------------+
| Edgar Allan Poe | The Raven | E.D.G.A.R. A.L.L.A.N. P.O.E. |
+-----------------+-----------+------------------------------+
The following templates make generating markdown easier:
bold(s string) string - Make text bold with **italic(s string) string - Make text italic with *underline(s string) string - Make text underlined with __strikethrough(s string) string - Make text strikethrough with ~~code(s string) string - Make text code with backtickscodeBlock(s, lang string) string - Make text a code block with languagetoDate(v interface{}) string - Convert value to YYYY-MM-DD format (supports string and time.Time)The following functions are available for randomization:
randomChoice(list interface{}) interface{} - Return random element from listrandomSubset(list interface{}, n int) interface{} - Return n random elements from listrandomPermute(list interface{}) interface{} - Return random permutation of listrandomInt(min, max interface{}) int - Return random integer between min and max (inclusive)randomFloat(min, max interface{}) float64 - Return random float between min and maxrandomBool() bool - Return random booleanrandomString(length int) string - Return random alphanumeric string of given lengthrandomStringList(count, minLength, maxLength int) []string - Return list of random strings❯ glaze json misc/test-data/[123].json \
--template-field 'random:{{ randomChoice .c }},subset:{{ randomSubset .c 2 }}' \
--use-row-templates
+-----+--------+-------------+
| a | random | subset |
+-----+--------+-------------+
| 1 | 4 | [3 5] |
| 10 | 30 | [40 50] |
| 100 | 300 | [300] |
+-----+--------+-------------+
styleBold(s string) string - Make text bold with ANSI escape codesIn addition to the functions above, all functions from the sprig library are available. These include many useful functions for string manipulation, math, lists, dictionaries, and more.