Skip to content

Commit

Permalink
Merge pull request #35 from desdic/20240729edit
Browse files Browse the repository at this point in the history
feat!: Add a edit option to modify commands
  • Loading branch information
desdic committed Jul 31, 2024
2 parents 8e5ad44 + 3479269 commit 732cab6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Neovim 0.9+ is required
extensions = {}, -- no extensions are loaded per default
last_first = false, -- make sure last option is first on next run, not persistant
run_groups = {}, -- no groups configured per default
overrides = {}, -- make global overrides
}
```

Expand Down Expand Up @@ -198,7 +199,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
1 change: 1 addition & 0 deletions lua/greyjoy/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ local defaults = {
default_shell = vim.o.shell, -- default shell to run tasks in
extensions = {},
last_first = false, -- make sure last option is first on next run, not persistant
overrides = {}, -- make global overrides
}

-- Set/Change options
Expand Down
105 changes: 85 additions & 20 deletions lua/greyjoy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,32 @@ 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 = {}

for _, value in ipairs(elements) do
local commandinput = table.concat(value.command, " ")
local name = value.name
local command = value.command
local orig_command = nil
if overrides[commandinput] then
name = overrides[commandinput]
-- Store original command before overwriting it
orig_command = command
command = utils.str_to_array(overrides[commandinput])
end

-- keep track of what elements we have
menulookup[value["name"]] = true
table.insert(menuelem, value["name"])
commands[value["name"]] = {
command = value["command"],
path = value["path"],
group_id = value["group_id"],
plugin = value["plugin"],
menulookup[name] = true
table.insert(menuelem, name)
commands[name] = {
command = command,
path = value.path,
group_id = value.group_id,
plugin = value.plugin,
orig_command = orig_command,
}
end

Expand All @@ -87,16 +96,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 +266,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 +327,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 732cab6

Please sign in to comment.