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 │ -- │ Rootiest Module │
-- ╰─────────────────────────────────────────────────────────╯ -- ╰─────────────────────────────────────────────────────────╯
local M = {} 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
---@return nil
-- Yank line without leading/trailing whitespace
function M.yank_line() function M.yank_line()
vim.api.nvim_feedkeys("_v$hy$", "n", true) vim.api.nvim_feedkeys("_v$hy$", "n", true)
end end
-- Check if the terminal is Kitty -- Variable to track if the precognition plugin has been called once
function M.using_kitty() local precog_first_time = true
local term = os.getenv("TERM") or ""
return term:find("kitty") ~= nil
end
-- Toggle the precognition plugin --- Toggle the precognition plugin
---@return nil
function M.toggle_precognition() function M.toggle_precognition()
if pcall(require, "precognition") then if pcall(require, "precognition") then
-- Initialize precognition plugin -- Initialize precognition plugin
@@ -32,79 +36,81 @@ function M.toggle_precognition()
precognition.toggle() -- Call toggle once precognition.toggle() -- Call toggle once
end end
else else
vim.api.nvim_err_writeln("precognition plugin is not installed") trigger_error("precognition plugin is not installed")
end end
end end
-- Toggle Hardtime and Precognition together --- Toggle Hardtime and Precognition together
---@return nil
function M.toggle_hardmode() function M.toggle_hardmode()
if pcall(require, "hardtime") then if pcall(require, "hardtime") then
local hardtime = require("hardtime") local hardtime = require("hardtime")
hardtime.toggle() hardtime.toggle()
M.toggle_precognition() M.toggle_precognition()
else else
vim.api.nvim_err_writeln("hardtime plugin is not installed") trigger_error("hardtime plugin is not installed")
end end
end end
-- Toggle LazyGit terminal --- Toggle LazyGit terminal split
function M.toggle_lazygit_term() ---@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 if pcall(require, "toggleterm.terminal") then
local Terminal = require("toggleterm.terminal").Terminal local Terminal = require("toggleterm.terminal").Terminal
local lazygit = Terminal:new({ local lazygit = Terminal:new({
cmd = "lazygit", cmd = "lazygit",
hidden = true, hidden = true,
direction = "horizontal", -- Set direction to horizontal split direction = direction or "horizontal", -- Set direction to horizontal split
size = 0.8, -- Set size to 80% of the screen size = size or 0.3, -- Set size to 30% of the screen
}) })
lazygit:toggle() lazygit:toggle()
return true
else else
vim.api.nvim_err_writeln("toggleterm plugin is not installed") trigger_error("toggleterm plugin is not installed")
return false
end end
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() function M.load_remote()
if pcall(require, "remote-nvim") then if pcall(require, "remote-nvim") then
---@diagnostic disable-next-line: missing-parameter ---@diagnostic disable-next-line: missing-parameter
require("remote-nvim").setup() require("remote-nvim").setup()
vim.cmd("RemoteStart") vim.cmd("RemoteStart")
return true
else else
vim.api.nvim_err_writeln("remote-nvim plugin is not installed") trigger_error("remote-nvim plugin is not installed")
end return false
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 = "" })
end end
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() function M.YankAndPasteInQuotes()
-- Yank text inside quotes on the current line -- Yank text inside quotes on the current line
vim.cmd("normal! yiq") vim.cmd("normal! yiq")
@@ -115,6 +121,9 @@ function M.YankAndPasteInQuotes()
-- List of marks to check ('a' to 'z') -- List of marks to check ('a' to 'z')
local marks = "abcdefghijklmnopqrstuvwxyz" local marks = "abcdefghijklmnopqrstuvwxyz"
-- Flag to track if any pasting was successful
local was_pasted = false
-- Loop through each mark -- Loop through each mark
for mark in marks:gmatch(".") do for mark in marks:gmatch(".") do
-- Check if the mark exists (returns the line number or nil) -- 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 -- Select text inside quotes and paste the yanked text
vim.cmd("normal! viq") vim.cmd("normal! viq")
vim.cmd('normal! "' .. yanked_text .. "P") vim.cmd('normal! "' .. yanked_text .. "P")
-- Set the flag to true since pasting was successful
was_pasted = true
end end
end end
-- Return whether any pasting was successful
return was_pasted
end 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() function M.eval_neovide()
if vim.g.neovide then if vim.g.neovide then
require("config.neovide") require("config.neovide")
return true
else
return false
end end
end end
-- Define user commands --- Define user commands
---@return nil
function M.define_commands() function M.define_commands()
-- Define Q abbreviation -- Define Q abbreviation
vim.cmd.cnoreabbrev("Q", "qall") vim.cmd.cnoreabbrev("Q", "qall")
@@ -154,24 +174,23 @@ function M.define_commands()
) )
end end
-- Set up module --- Set up rootiest module
function M.setup() function M.setup()
M.eval_neovide() M.eval_neovide()
M.define_commands() M.define_commands()
-- Setup types data -- Setup types data
require("data.types").setup() require("data.types").setup()
-- Setup indentor
require("utils.indentor")
-- ━━━━━━━━━━━━━━━━━━━━━━━━━ Rootiest cmd window ━━━━━━━━━━━━━━━━━━━━━━━━━ ---━━━━━━━━━━━━━━━━━━━━━━━━━━ Rootiest cmd window ━━━━━━━━━━━━━━━━━━━━━━━━━
-- --
-- Setup Rootiest cmd window utility -- Setup Rootiest cmd window utility
require("utils.cmd_window").setup() require("utils.cmd_window").setup()
-- Setup Rootiest cmd window (override default command-line behavior) -- Setup Rootiest cmd window (override default command-line behavior)
-- require("utils.cmd_window").setup({ override_cmdline = true }) -- require("utils.cmd_window").setup({ override_cmdline = true })
-- Setup indentor
require("utils.indentor")
end end
return M return M