Skip to content

Commit

Permalink
feat!: Add a edit option to modify commands
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Jul 29, 2024
1 parent 8e5ad44 commit 472f3e2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Integration with [toggleterm](https://github.com/akinsho/toggleterm.nvim) is als

## Requirements

Neovim 0.9+ is required
Neovim 0.10+ is required

## Default settings

Expand Down Expand Up @@ -51,7 +51,7 @@ Neovim 0.9+ is required
}
```

Per default all plugins use the same terminal but this behaviour (if you are using `toggleterm`) can be overridden by either grouping the plugins to a specific `group_id` or create a function to assign number based on plugin name.
Per default all plugins use the same terminal but this behaviour (if you are using `toggleterm`) can be overridden by either grouping the plugins to a specific `group_id` or create a function to assign number based on plugin name. Notice that the plugin/group name `edit` is reserved.

So if you want all plugins to run under id `id` (default) but the `docker_compose` you would like to have another group you can configure it via

Expand Down 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, `Greyjoy <pluginname or group name>` or `Greyjoy edit` to modify commands.

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
87 changes: 78 additions & 9 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,7 +104,52 @@ greyjoy.menu = function(rootdir, elements)
end
end

vim.ui.select(menuelem, { prompt = "Select a command" }, function(label, _)
return menuelem, commands
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 a command" }, function(label)
if label then
greyjoy.last_element[rootdir] = label
local command = commands[label]

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, " ")
if tmp == input then
greyjoy.overrides[tmp] = nil
else
greyjoy.overrides[tmp] = input
end
else
greyjoy.overrides[commandinput] = input
end
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]
Expand Down Expand Up @@ -209,7 +271,7 @@ local add_elements = function(elements, output)
end
end

greyjoy.run = function(arg)
greyjoy.run = function(args)
-- just return if disabled
if not greyjoy.enable then
return
Expand All @@ -219,7 +281,10 @@ greyjoy.run = function(arg)
local fullname = vim.api.nvim_buf_get_name(0)
local filename = vim.fs.basename(fullname)
local filepath = vim.fs.dirname(fullname)
local pluginname = arg or ""
local pluginname = args or ""
if pluginname == "edit" then
pluginname = ""
end
local uv = vim.uv

filepath = utils.if_nil(filepath, "")
Expand Down Expand Up @@ -270,7 +335,11 @@ greyjoy.run = function(arg)
end
end

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

vim.api.nvim_create_user_command("Greyjoy", function(args)
Expand Down
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("%w+") do
table.insert(words, word)
end
return words
end

return M

0 comments on commit 472f3e2

Please sign in to comment.