diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-05-22 11:37:50 +0200 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2024-09-13 06:03:59 -0600 |
| commit | c56b57fc58c73e96def91f4904d5b6e7c50e3de9 (patch) | |
| tree | b0918f3a6b234e4bd9ff5e5d86c2aa09ea622179 /vis-lua.c | |
| parent | d8276d916d875695a1000e14a6d1dfd03e689c81 (diff) | |
| download | vis-c56b57fc58c73e96def91f4904d5b6e7c50e3de9.tar.gz vis-c56b57fc58c73e96def91f4904d5b6e7c50e3de9.tar.xz | |
support piping a buffer to an external process
Currently only Text objects can be piped to external commands.
This is tedious if data not available in any file should be passed
to an external process (e.g. building options and passing them to
vis-menu).
This adds the option to pass a buffer to _vis_pipe and provides wrapper
functions for the original behavior and the new one.
Diffstat (limited to 'vis-lua.c')
| -rw-r--r-- | vis-lua.c | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -1234,14 +1234,34 @@ static int exit_func(lua_State *L) { * @treturn string stdout the data written to stdout * @treturn string stderr the data written to stderr */ +/*** + * Pipe a string to external process and collect output. + * + * The editor core will be blocked while the external process is running. + * + * @function pipe + * @tparam string text the text written to the external command + * @tparam string command the command to execute + * @tparam[opt] bool fullscreen whether command is a fullscreen program (e.g. curses based) + * @treturn int code the exit status of the executed command + * @treturn string stdout the data written to stdout + * @treturn string stderr the data written to stderr + */ static int pipe_func(lua_State *L) { Vis *vis = obj_ref_check(L, 1, "vis"); int cmd_idx = 4; char *out = NULL, *err = NULL; + const char *text = NULL; File *file = vis->win ? vis->win->file : NULL; Filerange range = text_range_new(0, 0); - if (lua_gettop(L) <= 3) { + if (lua_gettop(L) == 2) { cmd_idx = 2; + } else if (lua_gettop(L) == 3) { + text = luaL_checkstring(L, 2); + if (text != NULL) + cmd_idx = 3; + else + cmd_idx = 2; } else if (!(lua_isnil(L, 2) && lua_isnil(L, 3))) { file = obj_ref_check(L, 2, VIS_LUA_TYPE_FILE); range = getrange(L, 3); @@ -1249,10 +1269,14 @@ static int pipe_func(lua_State *L) { const char *cmd = luaL_checkstring(L, cmd_idx); bool fullscreen = lua_isboolean(L, cmd_idx + 1) && lua_toboolean(L, cmd_idx + 1); - if (!file) + if (!text && !file) return luaL_error(L, "vis:pipe(cmd = '%s'): win not open, file can't be nil", cmd); - int status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); + int status; + if (text) + status = vis_pipe_buf_collect(vis, text, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); + else + status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); lua_pushinteger(L, status); if (out) lua_pushstring(L, out); |
