Skip to content

chrsm/nu_plugin_lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nu_plugin_lua

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.

notes

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

mapping

how input is mapped to Lua:

  • there's always a global called nu
  • scalars are translated to nu.input (string|number|bool|nil)
  • for records and lists, nu is the converted rec/list converted to t atable
  • 1-indexed (still lua, yo)
  • w/tables, nu.input is a ref back to nu itself, which allows referring to the whole input - but it does conflict with any input-named field
  • lua's print prints to stderr
  • multiple returns become a list

I recognize this is different from the standard $in but in is a keyword in lua.

examples

'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' }
╭───┬─────────────────╮
│ 0Cargo.lock      │
│ 1Cargo.toml      │
│ 2README.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'
╭───┬───╮
│ 01 │
│ 12 │
╰───┴───╯


lua '{a=1}'
╭───┬───╮
│ a │ 1 │
╰───┴───╯

YueScript

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

About

Mirror. Not actively maintained here.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors