A tool to help process text, JSON, YAML, TOML or plists on the command line Meant to be used with stdin being piped in from some other command
npm install -g jeval
This puts a jeval command on your PATH.
Run jeval with no arguments to print the full flag list. The last argument is always a
JavaScript expression evaluated with the input available as the variable l:
echo "some line" | jeval "l.replace(/e/, 'a')"
Input is read as one big string by default. Pass --line to evaluate each line on its own,
which is also what --match filters on.
For example, turning markdown headers into an indented list:
## Something
some text
### Another thing
more text
## Section
lots of text
### Sub section 1
### Sub section 2
## A header
### A final section
Can be done with npm run test:match
outputs:
- Something
- Another thing
- Section
- Sub section 1
- Sub section 2
- A header
- A final section
Or one that I have been using recently on a regular basis:
function alaOpacity() {
cat ~/.config/alacritty/alacritty.yml | jeval --yaml --yamlout "function r(l, o) { l.background_opacity=Number(o); return l; } r(l, \"$1\"); " > ~/.config/alacritty/alacritty.new
mv ~/.config/alacritty/alacritty.new ~/.config/alacritty/alacritty.yml
}
then:
alaOpacity 0.7
This shows it's ability to parse yaml as well.
It can also parse and print JSON of course and handle plists and TOML.
Input is parsed with --json, --yaml, --toml or --plist, and output can be dumped in any of
those formats with --jsonout, --yamlout, --tomlout or --plistout. That makes converting
between them a one liner:
cat config.yml | jeval --yaml --jsonout "l" # yaml -> json
cat config.json | jeval --json --yamlout "l" # json -> yaml
cat config.toml | jeval --toml --jsonout "l" # toml -> json
cat config.yml | jeval --yaml --tomlout "l" # yaml -> toml
Reading a value, checking it and writing the file back out. Given this alacritty.toml:
[scrolling]
history = 50000
multiplier = 15
[terminal.shell]
program = "/bin/zsh"
args = ["--login"]
[window]
decorations = "buttonless"
dynamic_padding = truedouble the scrollback history, but only if it is under 100000:
cat alacritty.toml | jeval --toml --tomlout "l.scrolling.history < 100000 ? (l.scrolling.history *= 2, l) : l"
[scrolling] history becomes 100000 and every other key, type and section is preserved. The
guard makes it safe to run repeatedly — a second run leaves the file alone. This is
npm run test:toml, which runs against test/alacritty.toml.
Note that comments are not preserved when writing TOML back out; the output is regenerated from the parsed data.
Use ncc:
npm i -g @vercel/ncc
ncc build dist/jeval.js -o jeval
chmod +x jeval/index.js
cp jeval/index.js /usr/local/bin/jeval # jeval/index.js is the file you could now use on any machine without deps other than node
rm -rf jeval
with a packaged NodeJS interpreter.
alias -g removeFirstLine='tail -n +2'
npm i -g @vercel/ncc pkg
ncc build dist/jeval.js -o jeval
cp jeval/index.js ./jeval-with-hashbang.js
rm -rf jeval
cat jeval-with-hashbang.js | removeFirstLine > jeval.js
pkg --target macos-x64 jeval.js
rm jeval-with-hashbang.js jeval.js
Here is an example bash function that will change your Alacritty opacity:
# To use run: alaFontSize 12
function alaFontSize() {
cat ~/.config/alacritty/alacritty.yml | jeval --yaml --yamlout "(l.font.size=Number(\"$1\")) && l; " > $HOME/.config/alacritty/alacritty.yml.tmp
mv $HOME/.config/alacritty/alacritty.yml.tmp $HOME/.config/alacritty/alacritty.yml
}
# To use run: alaOpacity 0.8
function alaOpacity() {
cat ~/.config/alacritty/alacritty.yml | jeval --yaml --yamlout "(l.background_opacity=Number(\"$1\")) && l; " > $HOME/.config/alacritty/alacritty.yml.tmp
mv $HOME/.config/alacritty/alacritty.yml.tmp $HOME/.config/alacritty/alacritty.yml
}
alaOpacity 0.3
This idea of evalutating JS against text/json/yaml input is actually based off of someone elses idea/tool but I don't know where that repo is and cannot link to it.