Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/async loop #29

Merged
merged 20 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,26 @@ return {
### Setup
```lua
{
auto_refresh = {
enabled = false,
interval = 3000, -- milliseconds
},
namespace = "All",
hints = true,
context = true,
float_size = {
-- Almost fullscreen:
-- width = 1.0,
-- height = 0.95, -- Setting it to 1 will cause bottom to be cutoff by statuscolumn

-- For more context aware size:
width = 0.9,
height = 0.8,

-- Might need to tweak these to get it centered when float is smaller
col = 10,
row = 5,
}
}
```

Expand Down
6 changes: 5 additions & 1 deletion ftplugin/k8s_deployments.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- k8s_deployments.lua in ~/.config/nvim/ftplugin
local api = vim.api
local deployment_view = require("kubectl.views.deployments")
local hl = require("kubectl.actions.highlight")
local loop = require("kubectl.utils.loop")
local pod_view = require("kubectl.views.pods")
local root_view = require("kubectl.views.root")
local tables = require("kubectl.utils.tables")
Expand Down Expand Up @@ -64,3 +64,7 @@ api.nvim_buf_set_keymap(0, "n", "R", "", {
deployment_view.Deployments()
end,
})

if not loop.is_running() then
loop.start_loop(deployment_view.Deployments)
end
5 changes: 5 additions & 0 deletions ftplugin/k8s_events.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local event_view = require("kubectl.views.events")
local loop = require("kubectl.utils.loop")
local root_view = require("kubectl.views.root")
local tables = require("kubectl.utils.tables")
local api = vim.api
Expand Down Expand Up @@ -31,3 +32,7 @@ api.nvim_buf_set_keymap(0, "n", "<bs>", "", {
root_view.Root()
end,
})

if not loop.is_running() then
loop.start_loop(event_view.Events)
end
5 changes: 5 additions & 0 deletions ftplugin/k8s_nodes.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local loop = require("kubectl.utils.loop")
local node_view = require("kubectl.views.nodes")
local root_view = require("kubectl.views.root")
local tables = require("kubectl.utils.tables")
Expand Down Expand Up @@ -31,3 +32,7 @@ api.nvim_buf_set_keymap(0, "n", "d", "", {
end
end,
})

if not loop.is_running() then
loop.start_loop(node_view.Nodes)
end
9 changes: 7 additions & 2 deletions ftplugin/k8s_pods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local api = vim.api
local container_view = require("kubectl.views.containers")
local deployment_view = require("kubectl.views.deployments")
local hl = require("kubectl.actions.highlight")
local loop = require("kubectl.utils.loop")
local pod_view = require("kubectl.views.pods")
local tables = require("kubectl.utils.tables")
local view = require("kubectl.views")
Expand Down Expand Up @@ -67,7 +68,7 @@ api.nvim_buf_set_keymap(0, "n", "l", "", {
pod_view.selectPod(pod_name, namespace)
pod_view.PodLogs()
else
print("Failed to extract pod name or namespace.")
api.nvim_err_writeln("Failed to extract pod name or namespace.")
end
end,
})
Expand All @@ -81,7 +82,7 @@ api.nvim_buf_set_keymap(0, "n", "<CR>", "", {
pod_view.selectPod(pod_name, namespace)
container_view.containers(pod_view.selection.pod, pod_view.selection.ns)
else
print("Failed to extract containers.")
api.nvim_err_writeln("Failed to select pod.")
end
end,
})
Expand All @@ -93,3 +94,7 @@ api.nvim_buf_set_keymap(0, "n", "R", "", {
pod_view.Pods()
end,
})

if not loop.is_running() then
loop.start_loop(pod_view.Pods)
end
5 changes: 5 additions & 0 deletions ftplugin/k8s_secrets.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local loop = require("kubectl.utils.loop")
local root_view = require("kubectl.views.root")
local secret_view = require("kubectl.views.secrets")
local tables = require("kubectl.utils.tables")
Expand Down Expand Up @@ -31,3 +32,7 @@ api.nvim_buf_set_keymap(0, "n", "d", "", {
end
end,
})

if not loop.is_running() then
loop.start_loop(secret_view.Secrets)
end
5 changes: 5 additions & 0 deletions ftplugin/k8s_services.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local loop = require("kubectl.utils.loop")
local root_view = require("kubectl.views.root")
local service_view = require("kubectl.views.services")
local tables = require("kubectl.utils.tables")
Expand Down Expand Up @@ -31,3 +32,7 @@ api.nvim_buf_set_keymap(0, "n", "d", "", {
end
end,
})

if not loop.is_running() then
loop.start_loop(service_view.Services)
end
10 changes: 6 additions & 4 deletions lua/kubectl/actions/commands.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
local M = {}

function M.shell_command(cmd, args, callback)
function M.shell_command_async(cmd, args, callback)
local loaded, Job = pcall(require, "plenary.job")
if not loaded then
vim.notify("plenary.nvim is not installed. Please install it to use this feature.", vim.log.levels.ERROR)
return
end

local result = {}
Job:new({
command = cmd,
args = args,
on_stdout = function(_, data)
callback(data)
table.insert(result, data)
end,
on_stderr = function(_, data)
callback(data)
end,
on_exit = function(_, _)
callback(table.concat(result, "\n"))
end,
}):start()
end

-- Function to execute a shell command and return the output as a table of strings
function M.execute_shell_command(cmd, args)
local full_command = cmd .. " " .. table.concat(args, " ")
local handle = io.popen(full_command, "r")
Expand Down
10 changes: 6 additions & 4 deletions lua/kubectl/actions/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ local tag_patterns = {
{ pattern = M.symbols.note .. "[^" .. M.symbols.note .. M.symbols.clear .. "]*", group = "KubectlNote" }, -- Note
}

function M.register()
for _, tag in ipairs(tag_patterns) do
vim.fn.matchadd(tag.group, tag.pattern, 100, -1, { conceal = "" })
end
end

function M.set_highlighting(buf)
for _, symbol in pairs(M.symbols) do
vim.cmd("syntax match Conceal" .. ' "' .. symbol .. '" conceal')
end

for _, tag in ipairs(tag_patterns) do
vim.fn.matchadd(tag.group, tag.pattern, 100, -1, { conceal = "" })
end
api.nvim_buf_set_option(buf, "conceallevel", 3)
api.nvim_buf_set_option(buf, "concealcursor", "nc")
end
Expand Down
4 changes: 4 additions & 0 deletions lua/kubectl/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local M = {}
local defaults = {
auto_refresh = {
enabled = false,
interval = 3000, -- milliseconds
},
namespace = "All",
hints = true,
context = true,
Expand Down
2 changes: 2 additions & 0 deletions lua/kubectl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local commands = require("kubectl.actions.commands")
local completion = require("kubectl.completion")
local config = require("kubectl.config")
local filter_view = require("kubectl.views.filter")
local hl = require("kubectl.actions.highlight")
local namespace_view = require("kubectl.views.namespace")
local pod_view = require("kubectl.views.pods")
local view = require("kubectl.views")
Expand All @@ -21,6 +22,7 @@ FILTER = ""
SORTBY = ""

function M.open()
hl.register()
pod_view.Pods()
end

Expand Down
15 changes: 14 additions & 1 deletion lua/kubectl/resourcebuilder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@ function ResourceBuilder:new(resource, args)
self.args = args
self.hints = {}
self.filter = ""
self.data = {}
return self
end

function ResourceBuilder:fetch()
function ResourceBuilder:NamespaceOrAll()
if NAMESPACE ~= "All" then
for i, v in ipairs(self.args) do
if v == "-A" then
self.args[i] = "-n=" .. NAMESPACE
end
end
end
end

function ResourceBuilder:fetch()
self:NamespaceOrAll()
self.data = commands.execute_shell_command("kubectl", self.args)
return self
end

function ResourceBuilder:fetchAsync(callback)
self:NamespaceOrAll()
commands.shell_command_async("kubectl", self.args, function(data)
self.data = data
callback(self)
end)
return self
end

function ResourceBuilder:decodeJson()
local success, decodedData = pcall(vim.json.decode, self.data)
if success then
Expand Down
76 changes: 76 additions & 0 deletions lua/kubectl/utils/loop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
local config = require("kubectl.config")
local M = {}

local timers = {}

function M.start_loop_for_buffer(buf, callback)
if timers[buf] then
return
end

local running = false
local timer = vim.uv.new_timer()

timer:start(0, config.options.auto_refresh.interval, function()
if running then
return
end
running = true
vim.schedule(function()
if vim.api.nvim_get_current_buf() ~= buf then
return
end

callback()
running = false
end)
end)

timers[buf] = timer

vim.api.nvim_create_autocmd({ "BufEnter" }, {
buffer = buf,
callback = function()
M.start_loop_for_buffer(buf, callback)
end,
})

vim.api.nvim_create_autocmd({ "BufLeave", "BufDelete" }, {
buffer = buf,
callback = function()
M.stop_loop_for_buffer(buf)
end,
})
end

function M.stop_loop_for_buffer(buf)
local timer = timers[buf]
if timer then
timer:stop()
timer:close()
timers[buf] = nil
end
end

function M.start_loop(callback)
if config.options.auto_refresh.enabled then
local current_buf = vim.api.nvim_get_current_buf()
M.start_loop_for_buffer(current_buf, callback)
end
end

function M.stop_loop()
local current_buf = vim.api.nvim_get_current_buf()
M.stop_loop_for_buffer(current_buf)
end

function M.is_running_for_buffer(buf)
return timers[buf] ~= nil
end

function M.is_running()
local current_buf = vim.api.nvim_get_current_buf()
return M.is_running_for_buffer(current_buf)
end

return M
4 changes: 3 additions & 1 deletion lua/kubectl/utils/string.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local M = {}
function M.trim(s)
return s:match("^%s*(.-)%s*$")
if s then
return s:match("^%s*(.-)%s*$")
end
end

return M
Loading