Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**/result
**/repl-result-out
**/.nvim.lua
**/.cache
**/compile_commands.json
**/build
47 changes: 39 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,19 @@ sh.escape_args = false
sh.assert_zero = false
-- proper pipes at the cost of access to mid pipe values after further calls have been chained from it.
sh.proper_pipes = false
-- a list of functions to run in order on the command before running it.
-- each one recieves the final command and is to return a string representing the new one
sh.transforms = {}
-- because the actual metatable is inaccessible, you can add extra values here
-- will apply to the main sh variable, and all sub commands
sh.meta = {}
---Allows the definition of new shell backends.
---@type table<string, Shelua.Repr>
sh.repr = { posix = { --[[...]] } }
sh.shell = "posix"

-- because the actual metatable is inaccessible
-- this contains the metatable of this shelua instance
-- will apply to the main sh variable
---WARNING: DANGER YOU CAN BREAK A SHELUA INSTANCE THIS WAY
sh.meta_main = {}
-- applies to actual metatable of each command result created by this sh instance.
-- CANNOT set __metatable __index __tostring or __concat
sh.meta = {}
```

For info on `sh.repr`, see [Shell Representation docs](./REPR.md)
Expand All @@ -197,11 +200,11 @@ Or you can call it with a function that receives the old settings table and retu
local nsh = require('sh')()
nsh.assert_zero = true
-- or
local newersh = require('sh')({assert_zero = true})
-- or
local newsh = require('sh')()
getmetatable(newsh).assert_zero = true
-- or
local newersh = require('sh')({assert_zero = true})
-- or
local evennewersh = require('sh')(function(s) s.assert_zero = true return s end)

-- unaffected, prints 1
Expand All @@ -210,6 +213,29 @@ print(require('sh')["false"]().__exitcode)
print(nsh["false"]().__exitcode)
```

This function syntax used for cloning can also be used for modification of an existing instance.

```lua
require('sh')(function(opts)
opts.assert_zero = true
return opts
end, true) -- optional second argument meaning "do not clone"
require('sh')({escape_args = true}, true)
```

When setting settings via this method:
```lua
local sh = require('sh')
sh.repr = { newshell = { ... } }
```
This would overwrite the entire `repr` table, erasing any already there.

You may prefix the setting name with `_x_` to deep extend the tables together instead.
```lua
local sh = require('sh')
sh._x_repr = { newshell = { ... } }
```

## For nix users

```nix
Expand Down Expand Up @@ -265,6 +291,11 @@ You should provide the interpreter path via something like this to get the most

- `os.write_file(opts, filename, contents)` will be added where opts is `{ append = false, newline = true }` by default

- `os.env` will be added. Setting values in the table will set the environment variable
in the process environment, setting one to nil will unset it,
reading will return the environment variable's value.
Analogous to `vim.env` in neovim.

- The path to the shell hooks will be added to the shelua library's metatable (via `sh.stdenv_shell_hooks_path = shell_hooks`), for use in redefining the existing transform which adds them if desired.

## License
Expand Down
5 changes: 5 additions & 0 deletions REPR.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ Prior to 5.2 the io.popen command does not return exit code or signal. You can d
---each string in this table must begin with '__' or it will be ignored
---@field extra_cmd_results string[]|fun(opts: Shelua.Opts): string[]
extra_cmd_results = {},
---a list of functions to run in order on the command before running it.
---each one recieves the previous value and returns a new one.
---they are ran after concat_cmd or single_stdin and before the post_5_2_run and pre_5_2_run functions
---@field transforms? (fun(cmd: string|any): string|any)[]
transforms = {},
```

And the final method, `concat_cmd`. This is the counterpart to `single_stdin`.
Expand Down
13 changes: 7 additions & 6 deletions example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ print(sh.echo 'Hello World' :sed "s/Hello World/Goodbye Universe/g")
sh.escape_args = false

-- cloning sh with new settings, and "proper_pipes" setting (and others)
local nsh = sh({
proper_pipes = true,
escape_args = true,
assert_zero = true,
transforms = {
local nsh = sh(function (opts)
opts.proper_pipes = true
opts.escape_args = true
opts.assert_zero = true
opts.repr[opts.shell or "posix"].transforms = {
function(cmd)
print(cmd)
return cmd
end
}
})
return opts
end)
print(nsh.echo 'Hello world' :sed "s/Hello/Goodbye/g")
print(nsh.sed(nsh.echo 'Hello world', nsh.echo 'Hello world', "s/Hello/Goodbye/g"))
print(nsh.echo 'Hello World' :sed(nsh.echo 'Hello World', nsh.echo 'Hello World' :sed(nsh.echo 'Hello World', "s/Hello/Goodbye/g"), "s/World/Universe/g"))
25 changes: 23 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
forAllSys = nixpkgs.lib.genAttrs nixpkgs.lib.platforms.all;
overlay = final: prev: {
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
runLuaCommand = prev.callPackage ./runLuaCommand.nix { inherit n2l; };
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
};
overlay1 = final: prev: {
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
};
overlay2 = final: prev: {
runLuaCommand = prev.callPackage ./runLuaCommand.nix { inherit n2l; };
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
};
in {
overlays.default = overlay;
Expand All @@ -37,6 +37,24 @@
] // {
default = pkgs.shelua;
});
devShells = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
in {
default = pkgs.mkShell {
name = "testshell";
packages = with pkgs; [ bear ];
inputsFrom = [ ];
luaInterpreter = (pkgs.lua5_2.withPackages (ps: with ps; [inspect])).interpreter;
shellHook = ''
make_cc() {
pushd "$(git rev-parse --show-toplevel || echo ".")"
mkdir -p ./build
bear -- $CC -O2 -fPIC -shared -o ./build/env.so ./nix/env.c -I"$(dirname $luaInterpreter)/../include" "$@"
popd
}
'';
};
});
checks = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
mkBuildTest = lua: let
Expand All @@ -58,6 +76,9 @@
local outcat = outbin .. "/newcat"
local outecho = outbin .. "/newecho"
sh.mkdir("-p", outbin)
os.env.FRIEND = "everyone"
assert(os.getenv("FRIEND") == os.env.FRIEND, "os.env failed")
print(os.getenv("FRIEND"))
os.write_file({}, outfile, [[#!${pkgs.bash}/bin/bash]])
os.write_file({ append = true, }, outfile, [[echo "hello world!"]])
os.write_file({ append = true, }, outfile, [[cat ]] .. outdrv)
Expand Down
Loading
Loading