a nushell plugin that evaluates Lua against piped-in values.
uses mlua (vendored LuaJIT, so ~5.1).
this is just an experiment, learning the nushell ecosystem, so you probably shouldn't use this.
build: cargo build --release
copy to plugin dir: cp target/release/nu_plugin_lua ~/your-nushell-plugin-dir
edit config.nu
plugin add /path/to/plugins/nu_plugin_lua
plugin use lua
how input is mapped to Lua:
- there's always a global called
nu - scalars are translated to
nu.input(string|number|bool|nil) - for
recordsandlists,nuis the converted rec/list converted to t atable - 1-indexed (still lua, yo)
- w/tables,
nu.inputis a ref back tonuitself, which allows referring to the whole input - but it does conflict with anyinput-named field - lua's
printprints to stderr - multiple returns become a
list
I recognize this is different from the standard $in but in is a keyword in lua.
'abcd123' | lua 'string.upper(nu.input)'
ABCD123
{name: "x", n: 3} | lua 'nu.name .. tostring(nu.n)'
x3
[10 20 30] | lua 'nu[2]'
20
$ : ls | each {|e| lua 'nu.name' }
╭───┬─────────────────╮
│ 0 │ Cargo.lock │
│ 1 │ Cargo.toml │
│ 2 │ README.md │
│ 3 │ src │
│ 4 │ target │
╰───┴─────────────────╯
ls | each {|e| lua 'print(nu.name,nu.size)'}
Cargo.lock 62210
Cargo.toml 438
README.md 1836
src 4096
target 4096
╭────────────╮
│ empty list │
╰────────────╯
ls | lua 'for i,v in ipairs(nu) do print(i,v.name) end'
1 Cargo.lock
2 Cargo.toml
3 README.md
4 src
5 target
{a:1,b:[1,2]} | lua 'i=require("inspect");print(i(nu))'
<1>{
a = 1,
b = { 1, 2 },
input = <table 1>
}
lua '1,2'
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
╰───┴───╯
lua '{a=1}'
╭───┬───╮
│ a │ 1 │
╰───┴───╯requires c++20
build: cargo build --release --features yue
'abcd123' | yue 'string.upper nu.input'
ABCD123
{name: "x", n: 3} | yue '"#{nu.name}#{nu.n}"'
x3
[10 20 30] | yue 'sum = 0
for v in *nu
sum += v
sum'
60