irir
is a command line tool that provides a filter to add colors for text lines generically from a YAML configuration file easily.
This is a log file I want to add colors.
$ cat example.log
2023/05/19 23:56:54 [info] GET /some/resource 200 0.001
2023/05/19 23:56:55 [warn] GET /some/resource 200 0.001
2023/05/19 23:56:56 [error] GET /some/resource 200 0.001
2023/05/19 23:56:57 [info] GET /some/resource 200 0.001
then, below is a coloring rule file for log
in YAML.
$ cat ~/.config/irir/irir_rule.yaml
---
log:
- type: match
match: [info]
color: cyan
target: word
- type: match
match: [warn]
color: yellow
target: word
- type: match
match: [error]
color: bg_red
target: line
Then, you can filter the log file by irir
with log
rule.
$ cat example.log | irir log
Yas!
irir
loads rules from YAML file. The rule file locates on your config directory of XDG Base Directory. File name should be irir_rule.yaml
.
You can start editing irir_rule.yaml
by a command irir --edit-config
except on Windows.
You can see the location of irir_rule.yaml
by a command irir --dump-config-path
.
Here is the JSON Schema file to support writing irir_rule.yaml
.
First key log
is rule name that is specified in command line. You can name it as you like.
log:
- type: match
match: [info]
color: cyan
target: word
- type: match
match: [warn]
color: yellow
target: line
- type: match
match: [error]
color: bg_red
target: line
Above rules have 3 ways to color as list.
- type: match
match: [info]
color: cyan
target: word
type
: This specifies how to match. It should bematch
,prefix
,suffix
orregexp
. Iftarget
value isword
, then you can use onlymatch
orregexp
.match
: This is a string or a regexp string to match.color
: specific color name. See the palettetarget
: This specifies a scope of coloring. It should beword
orline
.
If type
is regexp
, then there are special way to set regexp
for coloring words.
The condition of regexp is Ba.
. It will match 2 places with the line Foo Bar Baz
.
$ cat example_file.txt
Foo Bar Baz
$ cat ~/.config/irir/irir_rule.yaml
re:
- type: regexp
match: Ba.
color: red
target: word
Filter above example_file.txt
like below:
$ cat example_file.txt | irir re
Output like this.
As for YAML spec, if you write backslash \
in string value, then you should enclose string value by single-quote like below:
- type: regexp
match: '\w+\.go'
If you enclose regexp with backslash by double-quote, then you should escape backslash by backslash:
- type: regexp
match: "\\w+\\.go"
This is bit confusing. Single-quoted regexp is easier.
To add colors for go test
result by gotest
rule.
gotest:
- type: prefix
match: "--- PASS"
color: green
target: line
- type: prefix
match: "ok"
color: green
target: line
- type: prefix
match: "PASS"
color: green
target: line
- type: prefix
match: "--- FAIL"
color: red
target: line
- type: prefix
match: "FAIL"
color: bg_red
target: line
- type: prefix
match: "--- SKIP"
color: dark_yellow
target: line
- type: match
match: "=== RUN"
color: gray
target: line
- target: line
type: match
match: "=== CONT"
color: gray
- type: match
match: "=== PAUSE"
color: gray
target: line
- type: match
match: "[no tests to run]"
color: yellow
target: word
- type: match
match: "[no test files]"
color: yellow
target: word
- type: regexp
match: '[^\/]+\.go:\d+'
color: cyan
target: word
This is also helpful on wrapped go test
through make
in a project.
$ make test | irir gotest
You can specify default rule by ENV:IRIR_DEFAULT_RULE
. Then you can omit rule argument in command line.
export IRIR_DEFAULT_RULE=gotest
go test -v ./... | irir
irir --help
Usage: cat example.log | irir RULE_ID
Options:
--dump-colors Dump color palette
--dump-config-path Dump config file path
--dump-rule Dump specified rule
--dump-rules Show rules from config file
--dump-schema Dump JSON Schema to validate the rule YAML config file
--edit-config Invoke $EDITOR (or vi) to edit config YAML file
-h, --help Show help (This message) and exit
-v, --version Show version and build info and exit
NOTE: Don't execute a command from outside you don't handle. Just invoke only your own commands.
Below command line will color an output from some-command
by irir rule
.
$ iriri rule -- some-command
It's as same as below.
$ some-command | irir rule
If you often use irir
, you can set alias with wrap command feature like below.
$ alias some-command="iriri rule -- some-command"
Then you can avoid writing | irir rule
on each time.
Github Actions doesn't have TTY. If you want to use irir
in Github Actions, Then you should add shell: 'script -q -e -c "bash {0}"'
line like below. This is an example how to color outputs of go test
in Github Actions.
- name: Install irir
run: |
go install github.com/bayashi/irir@latest
mkdir -p $HOME/.config/irir
curl -L https://raw.githubusercontent.com/bayashi/irir/main/configs/gotest.yaml > $HOME/.config/irir/irir_rule.yaml
- name: Run tests
shell: 'script -q -e -c "bash {0}"'
run: go test -v ./... | irir
If you are using Mac:
brew tap bayashi/tap
brew install bayashi/tap/irir
Download binary from here: https://github.com/bayashi/irir/releases
If you have golang environment:
go install github.com/bayashi/irir@latest
MIT License
Dai Okabayashi: https://github.com/bayashi