refactor: modularize error triggering and notifications

This commit is contained in:
2024-08-31 03:03:05 -04:00
parent 6639a08fcd
commit b79f65f3a8
+77 -58
View File
@@ -1,24 +1,28 @@
---@module "config.rootiest"
--- This module contains the configuration for the rootiest distro.
-- ╭─────────────────────────────────────────────────────────╮
-- │ Rootiest Module │
-- ╰─────────────────────────────────────────────────────────╯
local M = {}
local utils = require("utils.rootiest")
--- Function to trigger an error message
---@param message string The error message to trigger
---@return boolean condition true if the error message was triggered, false otherwise
local function trigger_error(message)
return require("data").func.notify(message, "ERROR")
end
local precog_first_time = true
-- Yank line without leading/trailing whitespace
--- Yank line without leading/trailing whitespace
---@return nil
function M.yank_line()
vim.api.nvim_feedkeys("_v$hy$", "n", true)
end
-- Check if the terminal is Kitty
function M.using_kitty()
local term = os.getenv("TERM") or ""
return term:find("kitty") ~= nil
end
-- Variable to track if the precognition plugin has been called once
local precog_first_time = true
-- Toggle the precognition plugin
--- Toggle the precognition plugin
---@return nil
function M.toggle_precognition()
if pcall(require, "precognition") then
-- Initialize precognition plugin
@@ -32,79 +36,81 @@ function M.toggle_precognition()
precognition.toggle() -- Call toggle once
end
else
vim.api.nvim_err_writeln("precognition plugin is not installed")
trigger_error("precognition plugin is not installed")
end
end
-- Toggle Hardtime and Precognition together
--- Toggle Hardtime and Precognition together
---@return nil
function M.toggle_hardmode()
if pcall(require, "hardtime") then
local hardtime = require("hardtime")
hardtime.toggle()
M.toggle_precognition()
else
vim.api.nvim_err_writeln("hardtime plugin is not installed")
trigger_error("hardtime plugin is not installed")
end
end
-- Toggle LazyGit terminal
function M.toggle_lazygit_term()
--- Toggle LazyGit terminal split
---@param direction string|nil The direction of the terminal split (default: "horizontal")
---@param size number|nil The size of the terminal split (default: 0.3)
---@return boolean condition true if the terminal was toggled, false otherwise
function M.toggle_lazygit_term(direction, size)
if pcall(require, "toggleterm.terminal") then
local Terminal = require("toggleterm.terminal").Terminal
local lazygit = Terminal:new({
cmd = "lazygit",
hidden = true,
direction = "horizontal", -- Set direction to horizontal split
size = 0.8, -- Set size to 80% of the screen
direction = direction or "horizontal", -- Set direction to horizontal split
size = size or 0.3, -- Set size to 30% of the screen
})
lazygit:toggle()
return true
else
vim.api.nvim_err_writeln("toggleterm plugin is not installed")
trigger_error("toggleterm plugin is not installed")
return false
end
end
-- Load remote-nvim plugin
--- Toggle LazyGit floating terminal
---@param my_args string|nil The arguments to pass to the lazygit command (default: nil)
---@return boolean condition true if the terminal was toggled, false otherwise
function M.toggle_lazygit_float(my_args)
if pcall(require, "toggleterm.terminal") then
local Util = require("lazyvim.util")
my_args = my_args or nil
if my_args ~= "" or my_args ~= nil then
Util.terminal.open(
{ "lazygit", my_args },
{ cwd = Util.root(), esc_esc = false }
)
else
Util.terminal.open({ "lazygit" }, { cwd = Util.root(), esc_esc = false })
end
return true
else
trigger_error("toggleterm plugin is not installed")
return false
end
end
--- Load remote-nvim plugin
---@return boolean condition true if the plugin was loaded, false otherwise
function M.load_remote()
if pcall(require, "remote-nvim") then
---@diagnostic disable-next-line: missing-parameter
require("remote-nvim").setup()
vim.cmd("RemoteStart")
return true
else
vim.api.nvim_err_writeln("remote-nvim plugin is not installed")
end
end
-- Set cursor icons
function M.set_cursor_icons()
local current_mode = vim.fn.mode()
local bg_color -- Define bg_color variable
if current_mode == "n" then -- Normal
bg_color = utils.get_bg_color("MiniStatuslineModeNormal")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "v" then -- Visual
bg_color = utils.get_bg_color("MiniStatuslineModeVisual")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "V" then -- Visual line
bg_color = utils.get_bg_color("MiniStatuslineModeVisual")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "R" or current_mode == "r" then -- Replace
bg_color = utils.get_bg_color("MiniStatuslineModeReplace")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
elseif current_mode == "i" then -- Insert
bg_color = utils.get_bg_color("MiniStatuslineModeInsert")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
else -- Other\Unknown
bg_color = utils.get_bg_color("MiniStatuslineModeNormal")
vim.api.nvim_set_hl(0, "SmoothCursor", { fg = bg_color })
vim.fn.sign_define("smoothcursor", { text = "" })
trigger_error("remote-nvim plugin is not installed")
return false
end
end
--- Yank text inside quotes on the current line and paste it into text inside quotes at various marks.
--- @return boolean true if any text was pasted successfully, false otherwise
function M.YankAndPasteInQuotes()
-- Yank text inside quotes on the current line
vim.cmd("normal! yiq")
@@ -115,6 +121,9 @@ function M.YankAndPasteInQuotes()
-- List of marks to check ('a' to 'z')
local marks = "abcdefghijklmnopqrstuvwxyz"
-- Flag to track if any pasting was successful
local was_pasted = false
-- Loop through each mark
for mark in marks:gmatch(".") do
-- Check if the mark exists (returns the line number or nil)
@@ -127,18 +136,29 @@ function M.YankAndPasteInQuotes()
-- Select text inside quotes and paste the yanked text
vim.cmd("normal! viq")
vim.cmd('normal! "' .. yanked_text .. "P")
-- Set the flag to true since pasting was successful
was_pasted = true
end
end
-- Return whether any pasting was successful
return was_pasted
end
-- Evaluate Neovide settings
--- Evaluate Neovide state and activate settings if necessary
---@return boolean condition true if Neovide is active, false otherwise
function M.eval_neovide()
if vim.g.neovide then
require("config.neovide")
return true
else
return false
end
end
-- Define user commands
--- Define user commands
---@return nil
function M.define_commands()
-- Define Q abbreviation
vim.cmd.cnoreabbrev("Q", "qall")
@@ -154,24 +174,23 @@ function M.define_commands()
)
end
-- Set up module
--- Set up rootiest module
function M.setup()
M.eval_neovide()
M.define_commands()
-- Setup types data
require("data.types").setup()
-- Setup indentor
require("utils.indentor")
-- ━━━━━━━━━━━━━━━━━━━━━━━━━ Rootiest cmd window ━━━━━━━━━━━━━━━━━━━━━━━━━
---━━━━━━━━━━━━━━━━━━━━━━━━━━ Rootiest cmd window ━━━━━━━━━━━━━━━━━━━━━━━━━
--
-- Setup Rootiest cmd window utility
require("utils.cmd_window").setup()
-- Setup Rootiest cmd window (override default command-line behavior)
-- require("utils.cmd_window").setup({ override_cmdline = true })
-- Setup indentor
require("utils.indentor")
end
return M