Skip to content

Commit

Permalink
feat: Add a edit option to modify command before running
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Jul 29, 2024
1 parent 8e5ad44 commit 096b45a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ use({"desdic/greyjoy.nvim",
})
```

Once installed and reloaded you can use `:Greyjoy` to run it or `Greyjoy <pluginname or group name>`.
Once installed and reloaded you can use `:Greyjoy` to run it or `Greyjoy <pluginname or group name>`. If you need to edit a command (like adding a variable or option) you can use `:Greyedit` (Works with group and plugins as parameter too).

So in the above example its possible to run the generic and makefile plugin by running `:Greyjoy fast` or if you only wanted to run the makefile plugin you could do `:Greyjoy makefile`

Expand Down
99 changes: 86 additions & 13 deletions lua/greyjoy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ greyjoy.setup = function(options)
_extensions.set_config(config.defaults["extensions"] or {})

greyjoy.last_element = {}
greyjoy.overrides = {}

-- easy index to do lookup later
greyjoy.run_group_map = {}
Expand Down Expand Up @@ -52,15 +53,30 @@ end

greyjoy.extensions = require("greyjoy._extensions").manager

greyjoy.menu = function(rootdir, elements)
if next(elements) == nil then
return
end

local function generate_list(rootdir, elements, overrides)
local menuelem = {}
local menulookup = {}
local commands = {}

local newelements = {}
for _, value in ipairs(elements) do
local commandinput = table.concat(value.command, " ")
if overrides[commandinput] then
local override = overrides[commandinput]

table.insert(newelements, {
name = override,
command = utils.str_to_array(override),
orig_command = utils.str_to_array(commandinput),
path = value.path,
plugin = value.plugin,
})
else
table.insert(newelements, value)
end
end

for _, value in ipairs(newelements) do
-- keep track of what elements we have
menulookup[value["name"]] = true
table.insert(menuelem, value["name"])
Expand All @@ -69,6 +85,7 @@ greyjoy.menu = function(rootdir, elements)
path = value["path"],
group_id = value["group_id"],
plugin = value["plugin"],
orig_command = value["orig_command"] or nil,
}
end

Expand All @@ -87,16 +104,64 @@ greyjoy.menu = function(rootdir, elements)
end
end

vim.ui.select(menuelem, { prompt = "Select a command" }, function(label, _)
return menuelem, commands
end

greyjoy.execute = function(command)
if greyjoy.output_results == "toggleterm" then
greyjoy.to_toggleterm(command)
else
greyjoy.to_buffer(command)
end
end

greyjoy.edit = function(rootdir, elements)
if next(elements) == nil then
return
end

local menuelem, commands =
generate_list(rootdir, elements, greyjoy.overrides)
vim.ui.select(menuelem, { prompt = "Edit before run" }, function(label)
if label then
greyjoy.last_element[rootdir] = label
local command = commands[label]

if greyjoy.output_results == "toggleterm" then
greyjoy.to_toggleterm(command)
else
greyjoy.to_buffer(command)
end
local commandinput = table.concat(command.command, " ")

vim.ui.input(
{ prompt = "Edit", default = commandinput },
function(input)
if input then
if command.orig_command then
local tmp = table.concat(command.orig_command, " ")
greyjoy.overrides[tmp] = input
else
greyjoy.overrides[commandinput] = input
end

command.command = utils.str_to_array(input)
greyjoy.execute(command)
end
end
)
end
end)
end

greyjoy.menu = function(rootdir, elements)
if next(elements) == nil then
return
end

local menuelem, commands =
generate_list(rootdir, elements, greyjoy.overrides)
vim.ui.select(menuelem, { prompt = "Select a command" }, function(label)
if label then
greyjoy.last_element[rootdir] = label
local command = commands[label]

greyjoy.execute(command)
end
end)
end
Expand Down Expand Up @@ -209,7 +274,7 @@ local add_elements = function(elements, output)
end
end

greyjoy.run = function(arg)
greyjoy.run = function(arg, method)
-- just return if disabled
if not greyjoy.enable then
return
Expand Down Expand Up @@ -270,11 +335,19 @@ greyjoy.run = function(arg)
end
end

greyjoy.menu(rootdir, elements)
if method == "edit" then
greyjoy.edit(rootdir, elements)
else
greyjoy.menu(rootdir, elements)
end
end

vim.api.nvim_create_user_command("Greyjoy", function(args)
greyjoy.run(args.args)
end, { nargs = "*", desc = "Run greyjoy" })

vim.api.nvim_create_user_command("Greyedit", function(args)
greyjoy.run(args.args, "edit")
end, { nargs = "*", desc = "Edit greyjoy" })

return greyjoy
8 changes: 8 additions & 0 deletions lua/greyjoy/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ M.if_nil = function(x, y)
return x
end

M.str_to_array = function(str)
local words = {}
for word in str:gmatch("%S+") do
table.insert(words, word)
end
return words
end

return M

0 comments on commit 096b45a

Please sign in to comment.