From e726550862e8a9e8089358c34cc51e926dbf1dac Mon Sep 17 00:00:00 2001 From: Erich L Foster Date: Fri, 3 May 2024 08:48:59 +0200 Subject: [PATCH] Add size to execute options --- README.md | 2 +- lua/devcontainer-cli/devcontainer_cli.lua | 6 ++-- lua/devcontainer-cli/devcontainer_utils.lua | 38 ++++++++++----------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 0a61852..19de96a 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ make assumptions about how you work. }, { "De", - ":DevcontainerExec direction='vertical'", + ":DevcontainerExec direction='vertical' size='40'", desc = "Execute a command in DevContainer", }, { diff --git a/lua/devcontainer-cli/devcontainer_cli.lua b/lua/devcontainer-cli/devcontainer_cli.lua index de5dbca..9e1f4b7 100644 --- a/lua/devcontainer-cli/devcontainer_cli.lua +++ b/lua/devcontainer-cli/devcontainer_cli.lua @@ -30,6 +30,7 @@ function M.exec(opts) local parsed = { cmd = nil, direction = nil, + size = nil, } if args ~= nil then @@ -38,13 +39,14 @@ function M.exec(opts) vim.validate({ cmd = { parsed.cmd, "string", true }, direction = { parsed.direction, "string", true }, + size = { parsed.size, "number", true }, }) - if parsed.cmd == nil and parsed.direction == nil then + if parsed.cmd == nil and parsed.direction == nil and parsed.size == nil then parsed.cmd = args end end - utils.exec(parsed.cmd, parsed.direction) + utils.exec(parsed.cmd, parsed.direction, parsed.size) end -- toggle the current devcontainer window diff --git a/lua/devcontainer-cli/devcontainer_utils.lua b/lua/devcontainer-cli/devcontainer_utils.lua index d102b93..13099ed 100644 --- a/lua/devcontainer-cli/devcontainer_utils.lua +++ b/lua/devcontainer-cli/devcontainer_utils.lua @@ -45,7 +45,7 @@ local _on_detach = function() end -- on_fail callback ----@param exit_code (integer) the exit code from the failed job +---@param exit_code (number) the exit code from the failed job local _on_fail = function(exit_code) vim.notify( "Devcontainer process has failed! exit_code: " .. exit_code, @@ -61,7 +61,7 @@ end -- on_exit callback function to delete the open buffer when devcontainer exits -- in a neovim terminal ----@param code (integer) the exit code +---@param code (number) the exit code local _on_exit = function(code) if code == 0 then _on_success() @@ -85,8 +85,8 @@ end -- create a new window and execute the given command ---@param cmd (string) the command to execute in the devcontainer terminal ---@param direction (string|nil) the placement of the window to be created (float, horizontal, vertical) ----@param close_on_exit (boolean|nil) whether to close terminal when process exits -local function _spawn_and_execute(cmd, direction, close_on_exit) +---@param size (number|nil) the size of the window to be created +local function _spawn_and_execute(cmd, direction, size) direction = vim.F.if_nil(direction, "float") if tableContains(directions, direction) == false then vim.notify("Invalid direction: " .. direction, vim.log.levels.ERROR) @@ -100,7 +100,8 @@ local function _spawn_and_execute(cmd, direction, close_on_exit) display_name = "devcontainer-cli", direction = vim.F.if_nil(direction, "float"), dir = folder_utils.get_root(config.toplevel), - close_on_exit = vim.F.if_nil(close_on_exit, false), + size = size + close_on_exit = false, on_open = function(term) -- ensure that we are not in insert mode vim.cmd("stopinsert") @@ -189,10 +190,11 @@ end ---@class ParsedArgs ---@field direction string? ---@field cmd string? +---@field size number? ----Take a users command arguments in the format "cmd='git commit' direction='float'" +---Take a users command arguments in the format "cmd='git commit' direction='float'" size='42' ---and parse this into a table of arguments ----{cmd = "git commit", direction = "float"} +---{cmd = "git commit", direction = "float", size = "42"} ---@param args string ---@return ParsedArgs function M.parse(args) @@ -219,6 +221,9 @@ function M.parse(args) if #part > 1 then local arg = vim.split(part, "=") local key, value = arg[1], arg[2] + if key == "size" then + value = tonumber(value) + end result[key] = value end end @@ -257,33 +262,26 @@ function M.bringup() _spawn_and_execute(command) end ---- execute command ----@param cmd (string) the command to execute in the devcontainer terminal ----@param direction (string|nil) the placement of the window to be created (left, right, bottom, float) ----@param close_on_exit (boolean|nil) whether to close terminal when process exits -function M._exec_command(cmd, direction, close_on_exit) - _spawn_and_execute(cmd, direction, close_on_exit) -end - -- execute the given cmd within the given devcontainer_parent ---@param cmd (string) the command to issue in the devcontainer terminal ---@param direction (string|nil) the placement of the window to be created -- (left, right, bottom, float) -function M._exec_cmd(cmd, direction) +function M._exec_cmd(cmd, direction, size) local command = _devcontainer_command("exec") if command == nil then return end command = command .. " " .. config.shell .. " -c '" .. cmd .. "'" - M._exec_command(command, direction, false) + _spawn_and_execute(command, direction, size) end -- execute a given cmd within the given devcontainer_parent ---@param cmd (string|nil) the command to issue in the devcontainer terminal ---@param direction (string|nil) the placement of the window to be created -- (left, right, bottom, float) -function M.exec(cmd, direction) +---@param size (number|nil) size of the window to create +function M.exec(cmd, direction, size) if _terminal ~= nil then vim.notify("There is already a devcontainer process running.", vim.log.levels.WARN) return @@ -294,14 +292,14 @@ function M.exec(cmd, direction) { prompt = "Enter command:" }, function(input) if input ~= nil then - M._exec_cmd(input, direction) + M._exec_cmd(input, direction, size) else vim.notify("No command received, ignoring.", vim.log.levels.WARN) end end ) else - M._exec_cmd(cmd, direction) + M._exec_cmd(cmd, direction, size) end end