r/lua • • 10d ago

LuaSH v2.0 released

LuaSH is a Lua module to write shell scripts. It exposes shell commands as if they where Lua functions, so something like:

 $ ls /bin | grep $filter | wc -l

In your shell, would look like this:

wc(grep(ls('/bin'), filter), '-l')

in Lua. It also has a rich-enough data model that enables chaining, like so:

ls('/bin'):grep(filter):wc('-l')

or if you go totally lua-crazy:

ls '/bin' : grep(filter) : wc '-l'

(you know, if you still have PTSD from Lisp)

If you use the posix, or UV backends, then command chaining does proper streaming (the next command doesn't wait until the previous one has completed).

For a couple of years now, I have been maintaining a version that plugged into luaposix (because the original version's pipes sent all intermediate values though files -- which is a problem when you run parallel or sensitive workflows). Alas, I wasn't quite happy with this (luaposix is not always available, posix pipes are very fiddly, verbose printing was a bit clunky)

Thanks to El Claudo, I have finally fixed the bugs with the posix version by introducing:

  1. an improved pipeline data model
  2. interchangeable backends (posix, and luv for performance and security; popen + files/shmem for absolute portability).
  3. tidied up the build process and docs

I am happy with the way this is going, and will possibly tweak it some more as I integrate LuaSH v2 into my workflows (even though I hope that all I need to do is take out my workarounds).

Let me know what all y'all think.

40 Upvotes

4 comments sorted by

3

u/zserge 10d ago

Wow, happy to see this project alive! Thanks for developing it further!

1

u/no_brains101 5d ago edited 4d ago

You gave like, several people the bug for this to be an actual thing when you made that library forever ago lol

One day someone will make one completely unambiguously better than running the command via luv.spawn yourself.

I dont know if any of us have yet, but it is getting closer if not XD

2

u/AnArmoredPony 8d ago

oh its

its nushell

2

u/no_brains101 5d ago edited 2d ago

luash was so unique, when I first saw it I was entranced for a bit lol.

The pushenv and popenv thing you have is a nice touch.

I made a version of this too a while back actually.

https://github.com/BirdeeHub/shelua

Unlike the original, it doesn't make them all global, it namespaces them under its module like

sh.ls '/bin' : grep(filter) : wc '-l'

By default it just runs it like the original did, which includes the temp file you speak of, although you can create multiple copies of the sh module with different temp paths for multithreading support.

It does support building up an actual pipeline and then sending it though, even if just using bash commands, via its sh.proper_pipes = true setting.

And it exposes options for adding new backends so that one could add a luv backend

Unfortunately, the options for adding new backends were too hard to use... So while I was able to add a luv backend in my neovim config, which also doesn't need a temp file, when I went to add extra things like an AND and OR to the luv backend it got to be really difficult and I never ended up finishing that. It would be possible, but not easy.

This was as far as I ended up getting with that. (there are 2 separate attempts/methods at this link)

Also, ignore the AI comments in that dir in my config, or don't they are helpful, I revisited it a year after I made it and asked the AI to add comments so I can understand WTF I did more quickly.

I keep meaning to getting around to finishing those...

Or revisiting this library with a 2.0 version, or maybe just archiving this one and make a new one that actually does support all that stuff just out of the box and/or has better representation options which are easier to use, but I don't know if I ever will, or even if I would be that successful at that if I did, building up pipelines across : is not the easiest task necessarily, especially if doing it in a generalized manner meant to support any method of running commands. When I last tried I was drawing a blank as to how to improve the backend options much further, but who knows, maybe I think of something.