diff --git a/lua/rpm/autocomplete.lua b/lua/rpm/autocomplete.lua index 0ed540e..2850cf2 100644 --- a/lua/rpm/autocomplete.lua +++ b/lua/rpm/autocomplete.lua @@ -1,4 +1,6 @@ -local commands = { +local M = {} + +M.commands = { help = { desc = "Get help for a command", nargs = "?" @@ -45,20 +47,20 @@ local commands = { }, } -local command_names = {} -local name_padding = 0 +M.command_names = {} +M.name_padding = 0 -for command, _ in pairs(commands) do - table.insert(command_names, command) +for command, _ in pairs(M.commands) do + table.insert(M.command_names, command) - if #command > name_padding then - name_padding = #command + if #command > M.name_padding then + M.name_padding = #command end end -local indent = string.rep(" ", name_padding + 3) +local indent = string.rep(" ", M.name_padding + 3) -local function get_command_args_info(command) +function M.get_command_args_info(command) if command == nil then return { has_args = false, @@ -70,11 +72,11 @@ local function get_command_args_info(command) local has_args = type(command.nargs) == "string" or command.nargs > 0 local max_args = command.nargs == "?" and 1 - or (command.nargs == "+" or commands.nargs == "*") and math.huge - or command.nargs + or (command.nargs == "+" or M.commands.nargs == "*") and math.huge + or command.nargs local min_args = (command.nargs == "?" or command.nargs == "*") and 0 - or max_args + or max_args return { has_args = has_args, @@ -83,24 +85,23 @@ local function get_command_args_info(command) } end -local function get_command_help(name) - local command = commands[name] +function M.get_command_help(name) + local command = M.commands[name] if not command then return "Command " .. name .. " not found." end - local padded_name = name .. string.rep(" ", name_padding - #name) + local padded_name = name .. string.rep(" ", M.name_padding - #name) - local has_args = get_command_args_info(command).has_args + local has_args = M.get_command_args_info(command).has_args return padded_name .. " - " .. command.desc .. "\n" .. - indent .. "Required arguments: " .. command.nargs .. "\n" .. - indent .. "Usage: `:Rpm " .. name .. (has_args and " " or "") .. "`" + indent .. "Required arguments: " .. command.nargs .. "\n" .. + indent .. "Usage: `:Rpm " .. name .. (has_args and " " or "") .. "`" end - -local function narrow_options(options, arg_lead) +function M.narrow_options(options, arg_lead) local matches = {} local arg_lead_lower = arg_lead:lower() @@ -113,18 +114,18 @@ local function narrow_options(options, arg_lead) return matches end -local function create_autocomplete(plugin_list) +function M.create(plugin_list) local function autocomplete(arg_lead, cmd_line) local args = vim.split(cmd_line, " ") if #args == 1 then - return command_names + return M.command_names end local cmd = args[2]:lower() - local command = commands[cmd] + local command = M.commands[cmd] - local args_info = get_command_args_info(command) + local args_info = M.get_command_args_info(command) local has_args = args_info.has_args local max_args = args_info.max_args local is_over_max_args = #args - 2 > max_args @@ -138,20 +139,11 @@ local function create_autocomplete(plugin_list) local is_help = cmd == "help" return has_args and not is_help and - narrow_options(plugin_list, arg_lead) or - narrow_options(command_names, arg_lead) + M.narrow_options(plugin_list, arg_lead) or + M.narrow_options(M.command_names, arg_lead) end return autocomplete end -local Autocomplete = { - create = create_autocomplete, - commands = commands, - command_names = command_names, - get_command_help = get_command_help, - get_command_args_info = get_command_args_info, - narrow_options = narrow_options -} - -return Autocomplete +return M diff --git a/lua/rpm/core.lua b/lua/rpm/core.lua index 57d93ad..2f3b83b 100644 --- a/lua/rpm/core.lua +++ b/lua/rpm/core.lua @@ -1,4 +1,6 @@ -local function parse_input_answer(answer) +local M = {} + +function M.parse_input_answer(answer) if answer:lower():match("^y") then return true end @@ -6,12 +8,12 @@ local function parse_input_answer(answer) return false end -local function get_plugin_version(install_path) +function M.get_plugin_version(install_path) return vim.fn.isdirectory(install_path) == 0 and "Not installed" or - vim.fn.system({ "git", "-C", install_path, "describe", "--tags" }):gsub("\n", "") + vim.fn.system({ "git", "-C", install_path, "describe", "--tags" }):gsub("\n", "") end -local function coerce_to_table(value) +function M.coerce_to_table(value) if type(value) == "table" then return value end @@ -19,7 +21,7 @@ local function coerce_to_table(value) return { value } end -local silent_print = function(message, silent) +function M.silent_print(message, silent) silent = silent or false if not silent then @@ -27,8 +29,8 @@ local silent_print = function(message, silent) end end -local function get_plugin_info(path) - local paths = coerce_to_table(path) +function M.get_plugin_info(path) + local paths = M.coerce_to_table(path) -- Consider the last path as the main dependency local last_path = paths[#paths] @@ -36,7 +38,7 @@ local function get_plugin_info(path) local name = last_path:match("([^/]+)$"):gsub("%.[a-zA-Z0-9]+", "") local install_path = vim.fn.stdpath("config") .. "/pack/plugins/start/" .. name - local version = get_plugin_version(install_path) + local version = M.get_plugin_version(install_path) return { install_path = install_path, @@ -46,14 +48,14 @@ local function get_plugin_info(path) } end -local function is_plugin_installed(path, silent) - local paths = coerce_to_table(path) +function M.is_plugin_installed(path, silent) + local paths = M.coerce_to_table(path) for _, p in ipairs(paths) do - local info = get_plugin_info(p) + local info = M.get_plugin_info(p) if vim.fn.isdirectory(info.install_path) == 0 then - silent_print("Dependency " .. info.name .. " is not installed.", silent) + M.silent_print("Dependency " .. info.name .. " is not installed.", silent) return false end end @@ -61,48 +63,48 @@ local function is_plugin_installed(path, silent) return true end -local function generate_helptags(path, silent) - local info = get_plugin_info(path) +function M.generate_helptags(path, silent) + local info = M.get_plugin_info(path) local doc_dir = info.install_path .. "/doc" if vim.fn.isdirectory(doc_dir) == 1 then - silent_print("Generating help tags for " .. info.name .. "...", silent) + M.silent_print("Generating help tags for " .. info.name .. "...", silent) vim.cmd("helptags " .. doc_dir) - - silent_print("Help tags for " .. info.name .. " have been generated!", silent) + + M.silent_print("Help tags for " .. info.name .. " have been generated!", silent) end end -local function install_plugin(path, silent) - local paths = coerce_to_table(path) +function M.install_plugin(path, silent) + local paths = M.coerce_to_table(path) for _, p in ipairs(paths) do - if is_plugin_installed(p, true) then - silent_print("Dependency " .. p .. " is already installed.\n", silent) + if M.is_plugin_installed(p, true) then + M.silent_print("Dependency " .. p .. " is already installed.\n", silent) goto continue end local url = p:match("^http") and p or "https://github.com/" .. p - local info = get_plugin_info(p) + local info = M.get_plugin_info(p) - silent_print("Cloning " .. url .. " to " .. info.install_path .. "...", silent) + M.silent_print("Cloning " .. url .. " to " .. info.install_path .. "...", silent) vim.fn.system({ "git", "clone", url, info.install_path }) - generate_helptags(p, silent) + M.generate_helptags(p, silent) - silent_print("Dependency " .. info.name .. " has been installed!", silent) + M.silent_print("Dependency " .. info.name .. " has been installed!", silent) ::continue:: end -end +end -local function update_plugin(path, silent) - local paths = coerce_to_table(path) +function M.update_plugin(path, silent) + local paths = M.coerce_to_table(path) for _, p in ipairs(paths) do - if not is_plugin_installed(p) then - silent_print("Dependency " .. p .. " is not installed.", silent) + if not M.is_plugin_installed(p) then + M.silent_print("Dependency " .. p .. " is not installed.", silent) local answer = "n" @@ -110,7 +112,7 @@ local function update_plugin(path, silent) answer = vim.fn.input("Would you like to install it? (y/n): ") end - if not parse_input_answer(answer) then + if not M.parse_input_answer(answer) then goto continue end @@ -118,64 +120,56 @@ local function update_plugin(path, silent) print("\n") end - install_plugin(p, silent) + M.install_plugin(p, silent) goto continue end - local info = get_plugin_info(p) + local info = M.get_plugin_info(p) - silent_print("Updating dependency " .. info.name .. "...", silent) + M.silent_print("Updating dependency " .. info.name .. "...", silent) vim.fn.system({ "git", "-C", info.install_path, "pull" }) - local new_version = get_plugin_version(info.install_path) + local new_version = M.get_plugin_version(info.install_path) if info.version == new_version then - silent_print("Dependency " .. info.name .. " is already up to date.", silent) + M.silent_print("Dependency " .. info.name .. " is already up to date.", silent) else - silent_print("Dependency " .. info.name .. " has been updated to " .. new_version .. "!", silent) + M.silent_print("Dependency " .. info.name .. " has been updated to " .. new_version .. "!", silent) end ::continue:: end end -local function delete_plugin(path, silent) - local paths = coerce_to_table(path) +function M.delete_plugin(path, silent) + local paths = M.coerce_to_table(path) for _, p in ipairs(paths) do - local info = get_plugin_info(p) + local info = M.get_plugin_info(p) if vim.fn.isdirectory(info.install_path) == 0 then - silent_print("Dependency " .. info.name .. " is not installed.\n", silent) + M.silent_print("Dependency " .. info.name .. " is not installed.\n", silent) goto continue end if not silent then local answer = vim.fn.input("Are you sure you want to delete " .. info.name .. "? (y/n): ") - if not parse_input_answer(answer) then + if not M.parse_input_answer(answer) then goto continue end print("\n") end - silent_print("Deleting " .. info.name .. "...", silent) + M.silent_print("Deleting " .. info.name .. "...", silent) vim.fn.system({ "rm", "-rf", info.install_path }) - silent_print("Dependency " .. info.name .. " has been deleted.", silent) + M.silent_print("Dependency " .. info.name .. " has been deleted.", silent) ::continue:: end end -return { - delete_plugin = delete_plugin, - get_plugin_info = get_plugin_info, - install_plugin = install_plugin, - is_plugin_installed = is_plugin_installed, - parse_input_answer = parse_input_answer, - generate_helptags = generate_helptags, - update_plugin = update_plugin -} +return M diff --git a/lua/rpm/interface.lua b/lua/rpm/interface.lua index 399924d..778d193 100644 --- a/lua/rpm/interface.lua +++ b/lua/rpm/interface.lua @@ -1,15 +1,21 @@ +local M = {} + local core = require("rpm.core") local plugin_list = require("rpm.plugin_list") -local Autocomplete = require("rpm.autocomplete") +local autocomplete = require("rpm.autocomplete") -local plugin_names = {} +M.plugin_names = {} +M.commands = autocomplete.commands +M.command_names = autocomplete.command_names +M.get_command_args_info = autocomplete.get_command_args_info +M.autocomplete = autocomplete.create(M.plugin_names) for name in pairs(plugin_list) do - table.insert(plugin_names, name) + table.insert(M.plugin_names, name) end -local function get_plugin(plugin_name) - local plugin = plugin_list[plugin_name] +function M.get(plugin_name) + local plugin = plugin_list[plugin_name] if not plugin then print("Plugin " .. plugin_name .. " not found.\n") @@ -19,7 +25,7 @@ local function get_plugin(plugin_name) return plugin end -local function use_plugin_list_op_with_routines(fn, args) +function M.use_plugin_list_op_with_routines(fn, args) for name, plugin in pairs(plugin_list) do local routine = coroutine.create(function() fn(name, plugin, args) @@ -29,30 +35,21 @@ local function use_plugin_list_op_with_routines(fn, args) end end --- RPM - Rudimentary Plugin Manager -local Rpm = { - commands = Autocomplete.commands, - command_names = Autocomplete.command_names, - get_command_args_info = Autocomplete.get_command_args_info, - autocomplete = Autocomplete.create(plugin_names), - get = get_plugin -} - -Rpm.help = function(command_name) +function M.help(command_name) if command_name then - print(Autocomplete.get_command_help(command_name)) + print(autocomplete.get_command_help(command_name)) return end print("Available commands:\n") - for _, command in ipairs(Autocomplete.command_names) do - print("\n" .. Autocomplete.get_command_help(command)) + for _, command in ipairs(autocomplete.command_names) do + print("\n" .. autocomplete.get_command_help(command)) end end -Rpm.generate_helptags = function(plugin_name) - local plugin = get_plugin(plugin_name) +function M.generate_helptags(plugin_name) + local plugin = M.get(plugin_name) if not plugin then return @@ -61,8 +58,8 @@ Rpm.generate_helptags = function(plugin_name) core.generate_helptags(plugin.path) end -Rpm.info = function(plugin_name) - local plugin = get_plugin(plugin_name) +function M.info(plugin_name) + local plugin = M.get(plugin_name) if not plugin then return @@ -76,15 +73,15 @@ Rpm.info = function(plugin_name) print("Installation Path: " .. info.install_path) end -Rpm.list = function() - use_plugin_list_op_with_routines(function(name, plugin) +function M.list() + M.use_plugin_list_op_with_routines(function(_, plugin) local info = core.get_plugin_info(plugin.path) print(info.name .. " (" .. info.version .. ")") end) end -Rpm.install = function(plugin_name) - local plugin = get_plugin(plugin_name) +function M.install(plugin_name) + local plugin = M.get(plugin_name) if not plugin then return @@ -93,8 +90,8 @@ Rpm.install = function(plugin_name) core.install_plugin(plugin.path) end -Rpm.update = function(plugin_name) - local plugin = get_plugin(plugin_name) +function M.update(plugin_name) + local plugin = M.get(plugin_name) if not plugin then return @@ -103,10 +100,10 @@ Rpm.update = function(plugin_name) core.update_plugin(plugin.path) end -Rpm.delete = function(plugin_name, silent) +function M.delete(plugin_name, silent) silent = silent or false - local plugin = get_plugin(plugin_name) - + local plugin = M.get(plugin_name) + if not plugin then return end @@ -128,27 +125,27 @@ Rpm.delete = function(plugin_name, silent) core.delete_plugin(plugin.path, silent) end -Rpm.install_all = function() +function M.install_all() print("Installing all plugins...\n") - use_plugin_list_op_with_routines(function(name) - Rpm.install(name) + M.use_plugin_list_op_with_routines(function(name) + M.install(name) end) print("\nAll plugins have been installed.") end -Rpm.update_all = function() +function M.update_all() print("Updating all plugins...\n") - use_plugin_list_op_with_routines(function(name) - Rpm.update(name) + M.use_plugin_list_op_with_routines(function(name) + M.update(name) end) print("\nAll plugins have been updated.") end -Rpm.delete_all = function() +function M.delete_all() local answer = vim.fn.input("Are you sure you want to delete all plugins? (y/n): ") if not core.parse_input_answer(answer) then @@ -157,18 +154,18 @@ Rpm.delete_all = function() print("\n") - use_plugin_list_op_with_routines(function(name) + M.use_plugin_list_op_with_routines(function(name) if name == "rpm" then return end - Rpm.delete(name, true) + M.delete(name, true) end) print("All plugins (except for RPM) have been deleted.") end -Rpm.clean = function() +function M.clean() local installed_plugins = vim.fn.globpath( vim.fn.stdpath("config") .. "/pack/plugins/start", "*", @@ -180,7 +177,7 @@ Rpm.clean = function() for _, plugin in pairs(plugin_list) do local paths = type(plugin.path) == "table" and plugin.path or { plugin.path } - + for _, path in ipairs(paths) do local info = core.get_plugin_info(path) table.insert(all_plugin_paths, info.name) @@ -201,12 +198,12 @@ Rpm.clean = function() print("Deleted " .. delete_count .. " plugins.\n") end -Rpm.setup = function(opts) +function M.setup(opts) opts = opts or {} if opts.after_init and type(opts.after_init) == "function" then - Rpm.after_init = opts.after_init + M.after_init = opts.after_init end end -return Rpm +return M diff --git a/lua/rpm/plugin_list.lua b/lua/rpm/plugin_list.lua index 26a6dcb..e88df7f 100644 --- a/lua/rpm/plugin_list.lua +++ b/lua/rpm/plugin_list.lua @@ -1,28 +1,24 @@ -local function get_plugin_list() - local plugins = {} +local M = {} - local configs = vim.fn.globpath( - vim.fn.stdpath("config") .. "/lua/plugins", "*.lua", - false, - true - ) +local configs = vim.fn.globpath( + vim.fn.stdpath("config") .. "/lua/plugins", "*.lua", + false, + true +) - for _, file in ipairs(configs) do - local routine = coroutine.create(function() - local module_name = file:match("([^/]+)$"):gsub("%.lua$", "") +for _, file in ipairs(configs) do + local routine = coroutine.create(function() + local module_name = file:match("([^/]+)$"):gsub("%.lua$", "") - local plugin = require("plugins." .. module_name) + local plugin = require("plugins." .. module_name) - plugins[module_name] = { - path = plugin[1], - init_fn = plugin[2] or function() end - } - end) + M[module_name] = { + path = plugin[1], + init_fn = plugin[2] or function() end + } + end) - coroutine.resume(routine) - end - - return plugins + coroutine.resume(routine) end -return get_plugin_list() +return M diff --git a/lua/rpm/user_commands.lua b/lua/rpm/user_commands.lua index 405d8e3..d0d4e6c 100644 --- a/lua/rpm/user_commands.lua +++ b/lua/rpm/user_commands.lua @@ -4,15 +4,15 @@ vim.api.nvim_create_user_command( "Rpm", function(cmd) local command = cmd.fargs[1] - + if not command then print("No command provided.") return end - rpm_command = Rpm.commands[command] + local rpm_command = Rpm.commands[command] - if not rpm_command then + if not rpm_command then print("Invalid command. Run `:Rpm help` for a list of commands.") return end