Skip to content

Commit

Permalink
reapply #36 to refactored plugin (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdearaugustin authored Jun 3, 2024
1 parent bdb4661 commit 83e55f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Install the plugin using your preferred package manager (below is an example usi
return {
{
"h4ckm1n-dev/kube-utils-nvim",
dependencies = { "nvim-telescope/telescope.nvim" },
opts = {},
},
}
Expand Down
10 changes: 6 additions & 4 deletions lua/kube-utils-nvim/repository.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- kube-utils-nvim/repository.lua
--
local Utils = require("kube-utils-nvim.utils")

local Repository = {}

Expand All @@ -9,14 +11,14 @@ Repository.get_repository_info = function(chart_yaml_path)

-- Check if Chart.yaml exists
if vim.fn.filereadable(chart_yaml_path) ~= 1 then
print("Error: Chart.yaml not found at " .. chart_yaml_path)
Utils.log_error("Chart.yaml not found at " .. chart_yaml_path)
return nil, nil
end

-- Read the contents of Chart.yaml
local chart_yaml_contents = vim.fn.readfile(chart_yaml_path)
if not chart_yaml_contents then
print("Error: Failed to read Chart.yaml")
Utils.log_error("Failed to read Chart.yaml")
return nil, nil
end

Expand All @@ -31,10 +33,10 @@ Repository.get_repository_info = function(chart_yaml_path)
end

if repo_name == "" then
print("Error: Repository name not found in Chart.yaml")
Utils.log_error("Repository name not found in Chart.yaml")
end
if repo_url == "" then
print("Error: Repository URL not found in Chart.yaml")
Utils.log_error("Repository URL not found in Chart.yaml")
end

return repo_name, repo_url
Expand Down
38 changes: 28 additions & 10 deletions lua/kube-utils-nvim/telescope_picker.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
-- kube-utils-nvim/telescope_picker.lua
--
local Utils = require("kube-utils-nvim.utils")

local TelescopePicker = {}

local function invoke_callback(callback, value)
if type(callback) == "function" then
callback(value)
else
print("Error: Callback is not a function.")
Utils.log_error("Callback is not a function.")
end
end

local function check_telescope()
if not pcall(require, "telescope") then
error("Cannot find telescope!")
end
return true
end

function TelescopePicker.select_from_list(prompt_title, list, callback)
local pickers, finders, config, actions
if check_telescope() then
pickers = require("telescope.pickers")
finders = require("telescope.finders")
config = require("telescope.config")
actions = require("telescope.actions")
state = require("telescope.actions.state")
end

if type(list) ~= "table" or #list == 0 then
print("Error: List must be a non-empty table.")
Utils.log_error("List must be a non-empty table.")
return
end

if type(callback) ~= "function" then
print("Error: Callback must be a function.")
Utils.log_error("Callback must be a function.")
return
end

require("telescope.pickers")
pickers
.new({}, {
prompt_title = prompt_title,
finder = require("telescope.finders").new_table({ results = list }),
sorter = require("telescope.config").values.generic_sorter({}),
finder = finders.new_table({ results = list }),
sorter = config.values.generic_sorter({}),
attach_mappings = function(_, map)
map("i", "<CR>", function(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry(prompt_bufnr)
require("telescope.actions").close(prompt_bufnr)
local selection = state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
if selection then
invoke_callback(callback, selection.value)
end
Expand All @@ -42,15 +60,15 @@ end

TelescopePicker.input = function(prompt_title, callback)
if type(callback) ~= "function" then
print("Error: Callback must be a function.")
Utils.log_error("Callback must be a function.")
return
end

local input = vim.fn.input(prompt_title .. ": ")
if input ~= "" then
invoke_callback(callback, input)
else
print("Error: " .. prompt_title .. " cannot be empty.")
Utils.log_error("" .. prompt_title .. " cannot be empty.")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/kube-utils-nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Utils = {}

-- Function log_error
Utils.log_error = function(message)
print("Error: " .. message)
vim.notify("Error: " .. message, vim.log.levels.ERROR)
end

return Utils

0 comments on commit 83e55f6

Please sign in to comment.