From b904c158ce1b74d3b0b33a9b48027d0b5416e282 Mon Sep 17 00:00:00 2001 From: Kim Gert Nielsen Date: Mon, 15 Jan 2024 13:35:22 +0100 Subject: [PATCH] feature: Add group_id/count for toggleterm When using toggleterm its now possible to group the terminals so we don't have to run all plugins in same terminal. --- README.md | 19 +++++++++ lua/greyjoy/_extensions/cargo.lua | 1 + lua/greyjoy/_extensions/docker_compose.lua | 5 +++ lua/greyjoy/_extensions/generic.lua | 1 + lua/greyjoy/_extensions/kitchen.lua | 1 + lua/greyjoy/_extensions/makefile.lua | 1 + lua/greyjoy/_extensions/project.lua | 48 ---------------------- lua/greyjoy/_extensions/vscode_tasks.lua | 1 + lua/greyjoy/config.lua | 7 ++++ lua/greyjoy/init.lua | 22 +++++++++- 10 files changed, 57 insertions(+), 49 deletions(-) delete mode 100644 lua/greyjoy/_extensions/project.lua diff --git a/README.md b/README.md index 7165b51..e0d3f48 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,13 @@ Neovim 0.9+ is required size = nil, } }, + toggleterm = { + -- default_group_id can be a number or a function that takes a string as parameter. + -- The string passed as parameter is the name of the plugin so its possible to do logic based + -- on plugin name and function should always return a number like: + -- default_group_id = function(plugin) return 1 end + default_group_id = 1, + }, enable = true, border = "rounded", -- style for vim.ui.selector style = "minimal", @@ -44,6 +51,18 @@ 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. + +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 + +``` + extensions = { + docker_compose = { group_id = 2 }, + }, +``` + +and now all docker compose's exec is running in a secondary terminal (group_id 2) and all the others in group_id 1 + ## Extensions Default `greyjoy` does not have any extensions enabled. diff --git a/lua/greyjoy/_extensions/cargo.lua b/lua/greyjoy/_extensions/cargo.lua index 0afa27a..eecb73d 100644 --- a/lua/greyjoy/_extensions/cargo.lua +++ b/lua/greyjoy/_extensions/cargo.lua @@ -35,6 +35,7 @@ M.parse = function(fileinfo) elem["name"] = name elem["command"] = cmd elem["path"] = filepath + elem["plugin"] = "cargo" table.insert(elements, elem) end diff --git a/lua/greyjoy/_extensions/docker_compose.lua b/lua/greyjoy/_extensions/docker_compose.lua index e14003d..01bfde7 100644 --- a/lua/greyjoy/_extensions/docker_compose.lua +++ b/lua/greyjoy/_extensions/docker_compose.lua @@ -54,6 +54,11 @@ M.parse = function(fileinfo) elem["name"] = "docker-compose exec " .. v elem["command"] = { M.config["cmd"], "exec", "-it", v, M.config["shell"] } elem["path"] = filepath + elem["plugin"] = "docker_compose" + + if M.config["group_id"] then + elem["group_id"] = M.config["group_id"] + end table.insert(elements, elem) end diff --git a/lua/greyjoy/_extensions/generic.lua b/lua/greyjoy/_extensions/generic.lua index 008c374..b353093 100644 --- a/lua/greyjoy/_extensions/generic.lua +++ b/lua/greyjoy/_extensions/generic.lua @@ -55,6 +55,7 @@ M.parse = function(fileobj) elem["name"] = k elem["command"] = command elem["path"] = filepath + elem["plugin"] = "generic" table.insert(globalcommands, elem) end diff --git a/lua/greyjoy/_extensions/kitchen.lua b/lua/greyjoy/_extensions/kitchen.lua index 369e423..e551465 100644 --- a/lua/greyjoy/_extensions/kitchen.lua +++ b/lua/greyjoy/_extensions/kitchen.lua @@ -63,6 +63,7 @@ M.parse = function(fileinfo) elem["name"] = "kitchen " .. target .. " " .. v elem["command"] = { "kitchen", target, v } elem["path"] = filepath + elem["plugin"] = "kitchen" table.insert(elements, elem) end end diff --git a/lua/greyjoy/_extensions/makefile.lua b/lua/greyjoy/_extensions/makefile.lua index e910519..56aac94 100644 --- a/lua/greyjoy/_extensions/makefile.lua +++ b/lua/greyjoy/_extensions/makefile.lua @@ -52,6 +52,7 @@ M.parse = function(fileinfo) elem["name"] = "make " .. v elem["command"] = { "make", v } elem["path"] = filepath + elem["plugin"] = "makefile" table.insert(elements, elem) end diff --git a/lua/greyjoy/_extensions/project.lua b/lua/greyjoy/_extensions/project.lua deleted file mode 100644 index 7c74194..0000000 --- a/lua/greyjoy/_extensions/project.lua +++ /dev/null @@ -1,48 +0,0 @@ -local utils = require("greyjoy.utils") -local ok, greyjoy = pcall(require, "greyjoy") -if not ok then - vim.notify( - "This plugin requires greyjoy.nvim (https://github.com/desdic/greyjoy.nvim)", - vim.lsp.log_levels.ERROR, - { title = "Plugin error" } - ) - return -end - -local M = {} - -M.readfile = function(filename) - if not utils.file_exists(filename) then - return nil - end - local content = vim.fn.readfile(filename) - local obj = vim.fn.json_decode(content) - return obj -end - -M.parse = function(fileobj) - local globalcommands = {} - -- Reuse the greyjoy patterns to find root of project - local rootdir = - vim.fs.dirname(vim.fs.find(greyjoy.patterns, { upward = true })[1]) - local filename = rootdir .. "/greyjoy.json" - - local obj = M.readfile(filename) - if obj then - for key, value in pairs(obj) do - print(vim.inspect(key, value)) - end - end - - return globalcommands -end - -return greyjoy.register_extension({ - setup = function(config) - M.config = config - end, - exports = { - type = "global", - parse = M.parse, - }, -}) diff --git a/lua/greyjoy/_extensions/vscode_tasks.lua b/lua/greyjoy/_extensions/vscode_tasks.lua index 9de2bcd..6b696fc 100644 --- a/lua/greyjoy/_extensions/vscode_tasks.lua +++ b/lua/greyjoy/_extensions/vscode_tasks.lua @@ -25,6 +25,7 @@ M.parse_v2 = function(content, filepath) elem["name"] = v["label"] elem["command"] = { v["command"] } elem["path"] = filepath + elem["plugin"] = "vscode_tasks" if v["args"] then for _, value in pairs(v["args"]) do diff --git a/lua/greyjoy/config.lua b/lua/greyjoy/config.lua index 1f69ae1..1db02c5 100644 --- a/lua/greyjoy/config.lua +++ b/lua/greyjoy/config.lua @@ -15,6 +15,13 @@ local defaults = { size = nil, }, }, + toggleterm = { + -- default_group_id can be a number or a function that takes a string as parameter. + -- The string passed as parameter is the name of the plugin so its possible to do logic based + -- on plugin name and function should always return a number like: + -- default_group_id = function(plugin) return 1 end + default_group_id = 1, + }, enable = true, -- enable/disable plugin border = "rounded", -- default borders style = "minimal", -- default style diff --git a/lua/greyjoy/init.lua b/lua/greyjoy/init.lua index 57212f4..603b14c 100644 --- a/lua/greyjoy/init.lua +++ b/lua/greyjoy/init.lua @@ -67,6 +67,8 @@ greyjoy.menu = function(rootdir, elements) commands[value["name"]] = { command = value["command"], path = value["path"], + group_id = value["group_id"], + plugin = value["plugin"], } end @@ -107,19 +109,37 @@ greyjoy.to_toggleterm = function(command) return end + local count = 1 -- keep old behaviour and have all run in same terminal window + local group_type = type(config.defaults["toggleterm"]["default_group_id"]) + if group_type == "number" then + count = config.defaults["toggleterm"]["default_group_id"] + elseif group_type == "function" then + count = config.defaults["toggleterm"]["default_group_id"](command.plugin) + end + + if command.group_id ~= nil then + group_type = type(command.group_id) + if group_type == "number" then + count = command.group_id + elseif group_type == "function" then + count = command.group_id(command.plugin) + end + end + local commandstr = table.concat(command.command, " ") local exec_command = "dir='" .. command.path .. "' cmd='" .. commandstr .. "'" + .. " name='" .. commandstr .. "'" if greyjoy.ui.toggleterm.size then exec_command = "size=" .. greyjoy.ui.toggleterm.size .. " " .. exec_command end - toggleterm.exec_command(exec_command) + toggleterm.exec_command(exec_command, count) end greyjoy.to_buffer = function(command)