Skip to content

Commit

Permalink
Merge pull request #36 from desdic/20240822conditions
Browse files Browse the repository at this point in the history
feat(generic): added a condition function
  • Loading branch information
desdic authored Aug 22, 2024
2 parents 1505dde + 62307f4 commit 8724d97
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 45 deletions.
118 changes: 81 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,21 @@ generic = {
["run {filename}"] = {
command = {"go", "run", "{filename}"},
filetype = "go"
}
},
["cmake --build target"] = {
command = { "cmake", "--build", "target" },
condition = function(n)
return condition.file_exists("CMakeLists.txt", n)
and condition.directory_exists("target", n)
end,
},
["cmake -S . -B target"] = {
command = { "cmake", "-S", ".", "-B", "target" },
condition = function(n)
return condition.file_exists("CMakeLists.txt", n)
and not condition.directory_exists("target", n)
end,
},
...
}
},
Expand Down Expand Up @@ -158,45 +172,75 @@ docker_compose = {

## Installing

using packer
Using lazy

```
use({"desdic/greyjoy.nvim",
config = function()
local greyjoy = require("greyjoy")
greyjoy.setup({
output_results = "toggleterm",
last_first = true,
extensions = {
generic = {
commands = {
["run test.py"] = {
command = {"./test.py"},
filetype = "python"
{
"desdic/greyjoy.nvim",
keys = {
{ "<Leader>gr", "<cmd>Greyjoy<CR>", desc = "[G]reyjoy [r]un" },
{ "<Leader>gg", "<cmd>Greyjoy fast<CR>", desc = "[G]reyjoy fast [g]roup" },
{ "<Leader>ge", "<cmd>Greyedit<CR>", desc = "[G]reyjoy [r]un" },
},
dependencies = {
{ "akinsho/toggleterm.nvim" },
},
cmd = "Greyjoy",
config = function()
local greyjoy = require("greyjoy")
local condition = require("greyjoy.conditions")
greyjoy.setup({
output_results = "toggleterm",
last_first = true,
extensions = {
generic = {
commands = {
["run {filename}"] = { command = { "python3", "{filename}" }, filetype = "python" },
["run main.go"] = {
command = { "go", "run", "main.go" },
filetype = "go",
filename = "main.go",
},
["build main.go"] = {
command = { "go", "build", "main.go" },
filetype = "go",
filename = "main.go",
},
["zig build"] = {
command = { "zig", "build" },
filetype = "zig",
},
["cmake --build target"] = {
command = { "cmake", "--build", "target" },
condition = function(n)
return condition.file_exists("CMakeLists.txt", n)
and condition.directory_exists("target", n)
end,
},
["cmake -S . -B target"] = {
command = { "cmake", "-S", ".", "-B", "target" },
condition = function(n)
return condition.file_exists("CMakeLists.txt", n)
and not condition.directory_exists("target", n)
end,
},
},
},
kitchen = { group_id = 2, targets = { "converge", "verify", "destroy", "test" }, include_all = false },
docker_compose = { group_id = 3 },
cargo = { group_id = 4 },
},
["run {filename}"] = {
command = {"go", "run", "{filename}"},
filetype = "go"
}
}
},
kitchen = {
targets = {"converge", "verify"},
include_all = false,
}
},
run_groups = {
fast = {"generic", "makefile", "cargo"},
}
})
greyjoy.load_extension("generic")
greyjoy.load_extension("vscode_tasks")
greyjoy.load_extension("makefile")
greyjoy.load_extension("kitchen")
greyjoy.load_extension("cargo")
greyjoy.load_extension("docker_compose")
end
})
run_groups = { fast = { "generic", "makefile", "cargo", "docker_compose" } },
})
greyjoy.load_extension("cargo")
greyjoy.load_extension("docker_compose")
greyjoy.load_extension("generic")
greyjoy.load_extension("kitchen")
greyjoy.load_extension("makefile")
greyjoy.load_extension("vscode_tasks")
end,
}
```

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).
Expand Down
4 changes: 3 additions & 1 deletion lua/greyjoy/_extensions/generic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ M.parse = function(fileobj)
local filename = fileobj.filename
local filetype = fileobj.filetype
local filepath = fileobj.filepath
local rootdir = fileobj.rootdir

local globalcommands = {}
if M.config.commands then
for k, v in pairs(M.config.commands) do
local match = utils.is_match(v, filename, filetype, filepath)
local match =
utils.is_match(v, filename, filetype, filepath, rootdir)

if match then
local elem = {}
Expand Down
19 changes: 19 additions & 0 deletions lua/greyjoy/conditions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local M = {}

local greyutil = require("greyjoy.utils")

M.file_exists = function(filename, obj)
local fpath = vim.fs.joinpath(obj.rootdir, filename)
if greyutil.file_exists(fpath) then
return true
end
end

M.directory_exists = function(dirname, obj)
local fpath = vim.fs.joinpath(obj.rootdir, dirname)
if greyutil.directory_exists(fpath) then
return true
end
end

return M
1 change: 1 addition & 0 deletions lua/greyjoy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ greyjoy.run = function(arg, method)
fullname = fullname,
filename = filename,
filepath = filepath,
rootdir = rootdir,
}

local elements = {}
Expand Down
30 changes: 23 additions & 7 deletions lua/greyjoy/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@ M.file_exists = function(filename)
return vim.fn.filereadable(filename) ~= 0
end

M.is_match = function(v, filename, filetype, filepath)
if v.filename then
if v.filename ~= filename then
M.directory_exists = function(dirname)
return vim.fn.isdirectory(dirname) ~= 0
end

M.is_match = function(obj, filename, filetype, filepath, rootdir)
if obj.filename then
if obj.filename ~= filename then
return false
end
end

if v.filetype then
if v.filetype ~= filetype then
if obj.filetype then
if obj.filetype ~= filetype then
return false
end
end

if v.filepath then
if not string.find(filepath, v.filepath) then
if obj.filepath then
if not string.find(filepath, obj.filepath) then
return false
end
end

if obj.condition then
if type(obj.condition) == "function" then
return obj.condition({
obj = obj,
filename = filename,
filetype = filetype,
filepath = filepath,
rootdir = rootdir,
})
end
end

return true
end

Expand Down

0 comments on commit 8724d97

Please sign in to comment.