Skip to content

Commit 9c338e8

Browse files
committed
feat(repr.overrides): allow reprs to choose if they support certain behaviors
allow representations to choose to select a value for proper_pipes or escape_args, to allow the user to swap between representations that may or may not support or need these features easier.
1 parent 1c09e03 commit 9c338e8

4 files changed

Lines changed: 36 additions & 20 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ sh.meta_main = {}
174174
sh.meta = {}
175175
```
176176

177-
For info on `sh.repr`, see [Shell Representation docs](./REPR.md)
177+
Each `repr` may also set `proper_pipes` and `escape_args` directly to override the top-level setting.
178+
Set with `true` or `false`, or use `nil` to fall back to top-level setting.
179+
180+
For more info on `sh.repr`, see [Shell Representation docs](./REPR.md)
178181

179182
If you need to change a nested value, you should instead get the table itself and change that.
180183

REPR.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ local utils_json = tostring(sh.nixdoc {
3434
- If you wish to make your `add_args` return something that is not a string, your other representation functions and any defined `transforms` must be able to handle that.
3535
- In addition, if you wish to make your `add_args` return something that is not a string, you should define `__tostring` in its metatable to preserve useful error messages.
3636

37-
Our first 3 `Shelua.Repr` methods for `posix`: `arg_tbl`, `escape`, and `add_args`
37+
Our first 3 `Shelua.Repr` methods for `posix`: `arg_tbl`, `escape`, and `add_args`, plus 2 extra override properties `proper_pipes` and `escape_args`
3838

3939
```lua
4040
---@type Shelua.Repr
4141
local posix = {
42+
---Override top-level proper_pipes setting for this representation.
43+
---Set with true or false, or use nil to fall back to top-level setting.
44+
---@field proper_pipes? boolean
45+
proper_pipes = nil,
46+
---Override top-level escape_args setting for this representation.
47+
---Set with true or false, or use nil to fall back to top-level setting.
48+
---@field escape_args? boolean
49+
escape_args = nil,
4250
-- converts key and it's argument to "-k" or "-k=v" or "--key=v" or nil to ignore
4351
-- turns table form args from table keys and values into flags
4452
-- if returning a list, items will be added to args list in order

flake.nix

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@
8080
LUA_INCDIR = "${lua}/include";
8181
LUA = lua.interpreter;
8282
BEAR = "${pkgs.bear}/bin/bear";
83-
shellHook = ''
84-
ogdir=$(pwd)
85-
gitdir="$(git rev-parse --show-toplevel)"
86-
if [ -n "$gitdir" ]; then
87-
export PREFIX="$gitdir/build/test"
88-
cd "$gitdir"
89-
make bear
90-
cd "$ogdir"
91-
fi
92-
unset gitdir ogdir
93-
'';
9483
};
9584
});
9685
};

lua/sh.lua

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
---proper_pipes will need to know that accessing them should be a trigger to resolve the pipe.
4343
---each string in this table must begin with '__' or it will be ignored
4444
---@field extra_cmd_results string[]|fun(opts: Shelua.Opts): string[]
45+
---override top-level proper_pipes setting for this representation
46+
---@field proper_pipes? boolean
47+
---override top-level escape_args setting for this representation
48+
---@field escape_args? boolean
4549

4650
---@class Shelua.OptsClass
4751
---proper pipes at the cost of access to mid pipe values after further calls have been chained from it.
@@ -185,6 +189,18 @@ local get_repr_fn = function(opts, attr)
185189
end, "repr", "posix", attr), "repr", opts.shell or "posix", attr)
186190
end
187191

192+
local function repr_proper_pipes(opts)
193+
local val = tbl_get(opts, nil, "repr", opts.shell or "posix", "proper_pipes")
194+
if val ~= nil then return val end
195+
return opts.proper_pipes
196+
end
197+
198+
local function repr_escape_args(opts)
199+
local val = tbl_get(opts, nil, "repr", opts.shell or "posix", "escape_args")
200+
if val ~= nil then return val end
201+
return opts.escape_args
202+
end
203+
188204
local function cmd_result_names(opts)
189205
local names = { "__input", "__exitcode", "__signal" }
190206
local xtra = tbl_get(opts, {}, "repr", opts.shell or "posix", "extra_cmd_results")
@@ -316,15 +332,15 @@ local function flatten(input, opts)
316332
keys[k] = true
317333
local v = t[k]
318334
if type(v) == 'table' then
319-
if unresolved[v] and opts.proper_pipes then
335+
if unresolved[v] and repr_proper_pipes(opts) then
320336
table.insert(result.unres, v)
321337
table.insert(result.codes, false)
322338
table.insert(result.res, false)
323339
else
324340
f(v)
325341
end
326342
else
327-
table.insert(result.args, opts.escape_args and get_repr_fn(opts, "escape")(v, opts) or v)
343+
table.insert(result.args, repr_escape_args(opts) and get_repr_fn(opts, "escape")(v, opts) or v)
328344
end
329345
end
330346
local codes = {}
@@ -336,7 +352,7 @@ local function flatten(input, opts)
336352
if v ~= nil then
337353
if k == '__input' then
338354
table.insert(result.res, v)
339-
if opts.proper_pipes then
355+
if repr_proper_pipes(opts) then
340356
table.insert(result.unres, false)
341357
end
342358
to_add = true
@@ -399,7 +415,7 @@ local command
399415
local cmd_mt = {
400416
__index = function(self, c)
401417
local opts = getmetatable(self)
402-
if not opts.proper_pipes then
418+
if not repr_proper_pipes(opts) then
403419
return command(self, c)
404420
end
405421
if check_if_cmd_result(opts, c) then
@@ -528,19 +544,19 @@ command = function(self, cmdstr, ...)
528544
for k, v in ipairs(preargs.res) do
529545
table.insert(input, v)
530546
table.insert(codes, preargs.codes[k])
531-
if shmt.proper_pipes then
547+
if repr_proper_pipes(shmt) then
532548
table.insert(unres, preargs.unres[k])
533549
end
534550
end
535551
for k, v in ipairs(args.res) do
536552
table.insert(input, v)
537553
table.insert(codes, args.codes[k])
538-
if shmt.proper_pipes then
554+
if repr_proper_pipes(shmt) then
539555
table.insert(unres, args.unres[k])
540556
end
541557
end
542558
local t = {}
543-
if shmt.proper_pipes then
559+
if repr_proper_pipes(shmt) then
544560
unresolved[t] = { cmd = cmd, unres = unres, input = input, codes = codes }
545561
else
546562
local apply = function(com)

0 commit comments

Comments
 (0)