diff --git a/lua/volt/color.lua b/lua/volt/color.lua index 3015a6d..7f03b6f 100644 --- a/lua/volt/color.lua +++ b/lua/volt/color.lua @@ -26,14 +26,14 @@ local M = {} -- Convert a hex color value to RGB --- @param hex: The hex color value --- @return r: Red (0-255) --- @return g: Green (0-255) --- @return b: Blue (0-255) -M.hex2rgb = function(hex) +--- @param hex string: The hex color value +--- @return integer|nil r: Red (0-255) +--- @return integer|nil g: Green (0-255) +--- @return integer|nil b: Blue (0-255) +function M.hex2rgb(hex) local hash = string.sub(hex, 1, 1) == "#" if string.len(hex) ~= (7 - (hash and 0 or 1)) then - return nil + return end local r = tonumber(hex:sub(2 - (hash and 0 or 1), 3 - (hash and 0 or 1)), 16) @@ -43,27 +43,31 @@ M.hex2rgb = function(hex) end -- Convert a hex color value to RGB ratio --- @param hex: The hex color value --- @return r: Red (0-100) --- @return g: Green (0-100) --- @return b: Blue (0-100) -M.hex2rgb_ratio = function(hex) +--- @param hex string: The hex color value +--- @return integer r: Red (0-100) +--- @return integer g: Green (0-100) +--- @return integer b: Blue (0-100) +function M.hex2rgb_ratio(hex) local r, g, b = M.hex2rgb(hex) return math.floor(r / 255 * 100), math.floor(g / 255 * 100), math.floor(b / 255 * 100) end -- Convert an RGB color value to hex --- @param r: Red (0-255) --- @param g: Green (0-255) --- @param b: Blue (0-255) --- @return The hexadecimal string representation of the color -M.rgb2hex = function(r, g, b) +--- @param r number: Red (0-255) +--- @param g number: Green (0-255) +--- @param b number: Blue (0-255) +--- @return string hex: The hexadecimal string representation of the color +function M.rgb2hex(r, g, b) return string.format("#%02x%02x%02x", math.floor(r), math.floor(g), math.floor(b)) end -- Helper function to convert a HSL color value to RGB -- Not to be used directly, use M.hsl2rgb instead -M.hsl2rgb_helper = function(p, q, a) +--- @param p number +--- @param q number +--- @param a number +--- @return number +function M.hsl2rgb_helper(p, q, a) if a < 0 then a = a + 6 end @@ -72,48 +76,50 @@ M.hsl2rgb_helper = function(p, q, a) end if a < 1 then return (q - p) * a + p - elseif a < 3 then + end + if a < 3 then return q - elseif a < 4 then + end + if a < 4 then return (q - p) * (4 - a) + p - else - return p end + + return p end -- Convert a HSL color value to RGB --- @param h: Hue (0-360) --- @param s: Saturation (0-1) --- @param l: Lightness (0-1) --- @return r: Red (0-255) --- @return g: Green (0-255) --- @return b: Blue (0-255) -M.hsl2rgb = function(h, s, l) +--- @param h number: Hue (0-360) +--- @param s number: Saturation (0-1) +--- @param l number: Lightness (0-1) +--- @return integer r: Red (0-255) +--- @return integer g: Green (0-255) +--- @return integer b: Blue (0-255) +function M.hsl2rgb(h, s, l) local t1, t2, r, g, b - h = h / 60 if l <= 0.5 then t2 = l * (s + 1) else t2 = l + s - (l * s) end + h = h / 60 t1 = l * 2 - t2 r = M.hsl2rgb_helper(t1, t2, h + 2) * 255 g = M.hsl2rgb_helper(t1, t2, h) * 255 b = M.hsl2rgb_helper(t1, t2, h - 2) * 255 - return r, g, b + return math.floor(r), math.floor(g), math.floor(b) end -- Convert an RGB color value to HSL --- @param r Red (0-255) --- @param g Green (0-255) --- @param b Blue (0-255) --- @return h Hue (0-360) --- @return s Saturation (0-1) --- @return l Lightness (0-1) -M.rgb2hsl = function(r, g, b) +--- @param r number: Red (0-255) +--- @param g number: Green (0-255) +--- @param b number: Blue (0-255) +--- @return number h: Hue (0-360) +--- @return number s: Saturation (0-1) +--- @return number l: Lightness (0-1) +function M.rgb2hsl(r, g, b) local min, max, l, s, maxcolor, h r, g, b = r / 255, g / 255, b / 255 @@ -155,31 +161,30 @@ M.rgb2hsl = function(r, g, b) end -- Convert a hex color value to HSL --- @param hex: The hex color value --- @param h: Hue (0-360) --- @param s: Saturation (0-1) --- @param l: Lightness (0-1) -M.hex2hsl = function(hex) +--- @param hex string: The hex color value +--- @return number h: Hue (0-360) +--- @return number s: Saturation (0-1) +--- @return number l: Lightness (0-1) +function M.hex2hsl(hex) local r, g, b = M.hex2rgb(hex) return M.rgb2hsl(r, g, b) end -- Convert a HSL color value to hex --- @param h: Hue (0-360) --- @param s: Saturation (0-1) --- @param l: Lightness (0-1) --- @returns hex color value -M.hsl2hex = function(h, s, l) +--- @param h number: Hue (0-360) +--- @param s number: Saturation (0-1) +--- @param l number: Lightness (0-1) +--- @return string hex: The hex color value +function M.hsl2hex(h, s, l) local r, g, b = M.hsl2rgb(h, s, l) return M.rgb2hex(r, g, b) end -- Change the hue of a color by a given amount --- @param hex The hex color value --- @param amount The amount to change the hue. --- Negative values decrease the hue, positive values increase it. --- @return The hex color value -M.change_hex_hue = function(hex, percent) +--- @param hex string: The hex color value +--- @param percent integer: The amount to change the hue +--- @return string hex: The hex color value +function M.change_hex_hue(hex, percent) local h, s, l = M.hex2hsl(hex) -- Convert percentage to a degree shift local shift = (percent / 100) * 360 @@ -191,11 +196,10 @@ M.change_hex_hue = function(hex, percent) end -- Desaturate or saturate a color by a given percentage --- @param hex The hex color value --- @param percent The percentage to desaturate or saturate the color. --- Negative values desaturate the color, positive values saturate it --- @return The hex color value -M.change_hex_saturation = function(hex, percent) +--- @param hex string: The hex color value +--- @param percent number: The percentage to desaturate or saturate the color. +--- @return string hex: The hex color value +function M.change_hex_saturation(hex, percent) local h, s, l = M.hex2hsl(hex) s = s + (percent / 100) if s > 1 then @@ -208,11 +212,10 @@ M.change_hex_saturation = function(hex, percent) end -- Lighten or darken a color by a given percentage --- @param hex The hex color value --- @param percent The percentage to lighten or darken the color. --- Negative values darken the color, positive values lighten it --- @return The hex color value -M.change_hex_lightness = function(hex, percent) +--- @param hex string: The hex color value +--- @param percent number: The percentage to lighten or darken the color. +--- @return string hex: The hex color value +function M.change_hex_lightness(hex, percent) local h, s, l = M.hex2hsl(hex) l = l + (percent / 100) if l > 1 then @@ -225,11 +228,11 @@ M.change_hex_lightness = function(hex, percent) end -- Compute a gradient between two colors --- @param hex1 The first hex color value --- @param hex2 The second hex color value --- @param steps The number of steps to compute --- @return A table of hex color values -M.compute_gradient = function(hex1, hex2, steps) +--- @param hex1 string: The first hex color value +--- @param hex2 string: The second hex color value +--- @param steps integer: The number of steps to compute +--- @return string[] gradient: A table of hex color values +function M.compute_gradient(hex1, hex2, steps) local h1, s1, l1 = M.hex2hsl(hex1) local h2, s2, l2 = M.hex2hsl(hex2) local h, s, l @@ -249,10 +252,10 @@ M.compute_gradient = function(hex1, hex2, steps) end -- Generate complementary colors --- @param hex The hex color value (string) --- @param count The number of complementary colors to generate --- @return A table containing the complementary colors in hex format -M.hex2complementary = function(hex, count) +--- @param hex string: The hex color value (string) +--- @param count integer: The number of complementary colors to generate +--- @return string[] complementary_colors: A table containing the complementary colors in hex format +function M.hex2complementary(hex, count) local h, s, l = M.hex2hsl(hex) local complementary_colors = {} @@ -271,12 +274,11 @@ M.hex2complementary = function(hex, count) end -- Mix two colors with a given percentage. --- @param first The primary hex color. --- @param second The hex color you want to mix into the first color. --- @param strength The percentage of second color in the output. --- This needs to be a number between 0 - 100. --- @return The mixed color as a hex value -M.mix = function(first, second, strength) +--- @param first string: The primary hex color. +--- @param second string: The hex color you want to mix into the first color. +--- @param strength number: The percentage of second color in the output (0-100). +--- @return string mixed: The mixed color as a hex value +function M.mix(first, second, strength) if strength == nil then strength = 0.5 end @@ -291,7 +293,8 @@ M.mix = function(first, second, strength) if s == 0 then return first - elseif s == 1 then + end + if s == 1 then return second end diff --git a/lua/volt/draw.lua b/lua/volt/draw.lua index 6f4fc94..f36460d 100644 --- a/lua/volt/draw.lua +++ b/lua/volt/draw.lua @@ -2,6 +2,7 @@ local api = vim.api local set_extmark = api.nvim_buf_set_extmark local state = require "volt.state" +--- @param buf integer return function(buf, section) local v = state[buf] local section_lines = section.lines(buf) diff --git a/lua/volt/events.lua b/lua/volt/events.lua index be035c1..725d5c4 100644 --- a/lua/volt/events.lua +++ b/lua/volt/events.lua @@ -7,7 +7,7 @@ local MouseMove = vim.keycode "" local LeftMouse = vim.keycode "" local map = vim.keymap.set -local get_item_from_col = function(tb, n) +local function get_item_from_col(tb, n) for _, val in ipairs(tb) do if val.col_start <= n and val.col_end >= n then return val @@ -15,9 +15,12 @@ local get_item_from_col = function(tb, n) end end -local run_func = function(foo) +--- @param foo function|string +local function run_func(foo) + ---@cast foo function if type(foo) == "function" then foo() + ---@cast foo string elseif type(foo) == "string" then vim.cmd(foo) end @@ -56,6 +59,9 @@ local function set_cursormoved_autocmd(buf) }) end +--- @param buf integer +--- @param row integer +--- @param col integer local function handle_hover(buf_state, buf, row, col) -- clear old hovers! if buf_state.hovered_extmarks then @@ -81,7 +87,8 @@ local function handle_hover(buf_state, buf, row, col) end end -local buf_mappings = function(buf) +--- @param buf integer +local function buf_mappings(buf) set_cursormoved_autocmd(buf) map("n", "", function() @@ -101,7 +108,7 @@ local M = {} M.bufs = {} -M.add = function(val) +function M.add(val) if type(val) == "table" then for _, buf in ipairs(val) do table.insert(M.bufs, buf) @@ -113,7 +120,7 @@ M.add = function(val) end end -M.enable = function() +function M.enable() vim.g.extmarks_events = true vim.o.mousemev = true diff --git a/lua/volt/highlights.lua b/lua/volt/highlights.lua index a6f75e0..370b83a 100644 --- a/lua/volt/highlights.lua +++ b/lua/volt/highlights.lua @@ -2,9 +2,11 @@ local api = vim.api local lighten = require("volt.color").change_hex_lightness local bg = vim.o.bg +---@type table local highlights = {} -local hexadecimal_to_hex = function(hex) +--- @param hex? integer +local function hexadecimal_to_hex(hex) return "#" .. ("%06x"):format((hex == nil and 0 or hex)) end @@ -29,7 +31,9 @@ if vim.g.base46_cache then CommentFg = { fg = colors.light_grey }, } else + ---@type string|integer|nil local normal_bg = api.nvim_get_hl(0, { name = "Normal" }).bg + ---@type string|integer|nil local comment_fg = api.nvim_get_hl(0, { name = "comment" }).fg normal_bg = hexadecimal_to_hex(normal_bg) @@ -39,7 +43,8 @@ else local lighter_bg = lighten(normal_bg, 5) local black3_bg = lighten(normal_bg, 10) - local get_hl = function(name) + --- @param name string + local function get_hl(name) local data = api.nvim_get_hl(0, { name = name }) return hexadecimal_to_hex(data.fg) end diff --git a/lua/volt/init.lua b/lua/volt/init.lua index 2f75ea6..2f1e146 100644 --- a/lua/volt/init.lua +++ b/lua/volt/init.lua @@ -5,7 +5,9 @@ local draw = require "volt.draw" local state = require "volt.state" local utils = require "volt.utils" -local get_section = function(tb, name) +--- @param tb table +--- @param name string +local function get_section(tb, name) for _, value in ipairs(tb) do if value.name == name then return value @@ -13,7 +15,7 @@ local get_section = function(tb, name) end end -M.gen_data = function(data) +function M.gen_data(data) for _, info in ipairs(data) do state[info.buf] = {} @@ -39,7 +41,9 @@ M.gen_data = function(data) end end -M.redraw = function(buf, names) +--- @param buf integer +--- @param names "all"|string|string[] +function M.redraw(buf, names) local v = state[buf] if names == "all" then @@ -49,17 +53,21 @@ M.redraw = function(buf, names) return end + ---@cast names string if type(names) == "string" then draw(buf, get_section(v.layout, names)) return end + ---@cast names string[] for _, name in ipairs(names) do draw(buf, get_section(v.layout, name)) end end -M.set_empty_lines = function(buf, n, w) +--- @param buf integer +--- @param w integer +function M.set_empty_lines(buf, n, w) local empty_lines = {} for _ = 1, n, 1 do @@ -69,7 +77,7 @@ M.set_empty_lines = function(buf, n, w) api.nvim_buf_set_lines(buf, 0, -1, true, empty_lines) end -M.mappings = function(val) +function M.mappings(val) for _, buf in ipairs(val.bufs) do -- cycle bufs map("n", "", function() @@ -100,7 +108,8 @@ M.mappings = function(val) end end -M.run = function(buf, opts) +--- @param buf integer +function M.run(buf, opts) vim.bo[buf].filetype = "VoltWindow" if opts.custom_empty_lines then @@ -120,15 +129,16 @@ M.run = function(buf, opts) end end -M.toggle_func = function(open_func, ui_state) +function M.toggle_func(open_func, ui_state) if ui_state then open_func() - else - api.nvim_feedkeys("q", "x", false) + return end + + api.nvim_feedkeys("q", "x", false) end -M.close = function(buf) +function M.close(buf) if not buf then api.nvim_feedkeys("q", "x", false) return diff --git a/lua/volt/ui/components.lua b/lua/volt/ui/components.lua index 8f0bcca..a6ed846 100644 --- a/lua/volt/ui/components.lua +++ b/lua/volt/ui/components.lua @@ -11,7 +11,7 @@ local M = {} --- @param o CheckboxOptions The options for the checkbox. --- @return string[] A table containing the checkbox representation. -M.checkbox = function(o) +function M.checkbox(o) return { (o.active and (o.check or "") or (o.uncheck or "")) .. " " .. o.txt, o.active and (o.hlon or "String") or (o.hloff or "ExInactive"), @@ -27,7 +27,7 @@ end --- @param o ProgressOptions The options for the progress bar. --- @return table[] A table containing two elements: the active and inactive parts of the progress bar. -M.progressbar = function(o) +function M.progressbar(o) local opts = { icon = { on = "-", off = "-" }, hl = { on = "exred", off = "linenr" }, @@ -44,11 +44,14 @@ M.progressbar = function(o) } end -M.separator = function(char, w, hl) +--- @param char string +--- @param w integer +--- @param hl string|nil +function M.separator(char, w, hl) return { { string.rep(char or "─", w), hl or "linenr" } } end -M.grid_row = function(tb) +function M.grid_row(tb) local result = {} for _, lines in ipairs(tb) do for _, line in ipairs(lines) do @@ -59,7 +62,7 @@ M.grid_row = function(tb) return result end -M.line_w = function(line) +function M.line_w(line) local w = 0 for _, cell in ipairs(line) do @@ -71,7 +74,8 @@ M.line_w = function(line) return w end -M.border = function(lines, hl) +--- @param hl string|nil +function M.border(lines, hl) hl = hl or "linenr" local maxw = 0 @@ -101,7 +105,8 @@ M.border = function(lines, hl) table.insert(lines, { { "└" .. horiz_chars .. "┘", hl } }) end -M.hpad = function(line, w) +--- @param w integer +function M.hpad(line, w) local pad_w = w - M.line_w(line) for i, v in ipairs(line) do diff --git a/lua/volt/ui/graphs/bar.lua b/lua/volt/ui/graphs/bar.lua index 45fca95..3e5c417 100644 --- a/lua/volt/ui/graphs/bar.lua +++ b/lua/volt/ui/graphs/bar.lua @@ -1,6 +1,6 @@ local utils = require "volt.ui.graphs.utils" -local gen_graph = function(lines, val, opts) +local function gen_graph(lines, val, opts) local barchar = string.rep(opts.icon or "█", opts.w) local emptychar = string.rep(" ", opts.w) local gap = string.rep(" ", opts.gap) diff --git a/lua/volt/ui/graphs/dot.lua b/lua/volt/ui/graphs/dot.lua index d4d43e2..89a100e 100644 --- a/lua/volt/ui/graphs/dot.lua +++ b/lua/volt/ui/graphs/dot.lua @@ -6,7 +6,7 @@ local default_opts = { sidelabels = true, } -local gen_graph = function(lines, val, baropts) +local function gen_graph(lines, val, baropts) local icons = baropts.icons val = vim.tbl_map(function(x) diff --git a/lua/volt/ui/graphs/utils.lua b/lua/volt/ui/graphs/utils.lua index bbe5918..87c7e6d 100644 --- a/lua/volt/ui/graphs/utils.lua +++ b/lua/volt/ui/graphs/utils.lua @@ -1,6 +1,7 @@ local M = {} -M.gen_labels = function(format) +--- @param format nil|fun(n: integer): string +function M.gen_labels(format) local result = {} local max_strw = 0 @@ -13,7 +14,7 @@ M.gen_labels = function(format) num = tostring(i * 10) end - if #num > max_strw then + if num:len() > max_strw then max_strw = #num end @@ -27,7 +28,7 @@ M.gen_labels = function(format) return { labels = result, maxw = max_strw + 1 } end -M.footer_label = function(virt_txt, total_w, l_pad) +function M.footer_label(virt_txt, total_w, l_pad) local strw = vim.api.nvim_strwidth(virt_txt[1]) / 2 local pad = (total_w / 2 + l_pad) - strw return { { string.rep(" ", pad) }, virt_txt } diff --git a/lua/volt/ui/grid_col.lua b/lua/volt/ui/grid_col.lua index b3e02ea..f9832ae 100644 --- a/lua/volt/ui/grid_col.lua +++ b/lua/volt/ui/grid_col.lua @@ -1,6 +1,6 @@ local linew = require("volt.ui.components").line_w -local add_empty_space = function(lines, w, pad) +local function add_empty_space(lines, w, pad) for _, line in ipairs(lines) do table.insert(line, { string.rep(" ", w - linew(line) + pad) }) end @@ -8,7 +8,7 @@ local add_empty_space = function(lines, w, pad) return lines end -local append_tb = function(t1, t2) +local function append_tb(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end diff --git a/lua/volt/ui/slider.lua b/lua/volt/ui/slider.lua index 3377108..97c9c8a 100644 --- a/lua/volt/ui/slider.lua +++ b/lua/volt/ui/slider.lua @@ -1,7 +1,7 @@ local api = vim.api local M = {} -M.val = function(w, left_txt, xpad, opts) +function M.val(w, left_txt, xpad, opts) opts = opts or {} local txt_len = vim.fn.strwidth(left_txt or "") w = w - txt_len - (opts.ratio and 5 or 0) @@ -15,7 +15,7 @@ M.val = function(w, left_txt, xpad, opts) return math.ceil((col / w) * 100) end -M.config = function(o) +function M.config(o) local line = {} local left_txt_len = vim.fn.strwidth(o.txt or "") diff --git a/lua/volt/ui/table.lua b/lua/volt/ui/table.lua index 5e1c0c1..02e5639 100644 --- a/lua/volt/ui/table.lua +++ b/lua/volt/ui/table.lua @@ -1,6 +1,6 @@ local virt_linew = require("volt.ui.components").line_w -local get_column_widths = function(tb, w) +local function get_column_widths(tb, w) local fit_w = type(w) == "string" local maxrow = #tb[1] local sum = 0 @@ -50,19 +50,23 @@ local border_chars = { corners_right = { top = "┐", bot = "┘", none = "┤" }, } -local table_border = function(points, row_type) +--- @param points integer[] +--- @param row_type string|nil +local function table_border(points, row_type) + row_type = row_type or "none" + local str = "" local tblen = #points for i, n in ipairs(points) do - local t_char = border_chars.mid[row_type or "none"] + local t_char = border_chars.mid[row_type] t_char = i == tblen and "" or t_char str = str .. string.rep("─", n + 1) .. t_char end - local l_char = border_chars.corners_left[row_type or "none"] - local r_char = border_chars.corners_right[row_type or "none"] + local l_char = border_chars.corners_left[row_type] + local r_char = border_chars.corners_right[row_type] return { { l_char .. str .. r_char, "linenr" } } end diff --git a/lua/volt/utils.lua b/lua/volt/utils.lua index 3c8f6fe..9521b36 100644 --- a/lua/volt/utils.lua +++ b/lua/volt/utils.lua @@ -4,7 +4,13 @@ local state = require "volt.state" local buf_i = 1 -M.cycle_bufs = function(bufs) +--- @param hex integer|nil +local function hexadecimal_to_hex(hex) + return "#" .. ("%06x"):format(hex == nil and 0 or hex) +end + +--- @param bufs integer[] +function M.cycle_bufs(bufs) buf_i = buf_i == #bufs and 1 or buf_i + 1 local new_buf = bufs[buf_i] @@ -13,7 +19,9 @@ M.cycle_bufs = function(bufs) api.nvim_set_current_win(a) end -M.cycle_clickables = function(buf, step) +--- @param buf integer +--- @param step number +function M.cycle_clickables(buf, step) local bufstate = state[buf] local lines = {} @@ -37,7 +45,7 @@ M.cycle_clickables = function(buf, step) end end -M.close = function(val) +function M.close(val) local event_bufs = require("volt.events").bufs for _, buf in ipairs(val.bufs) do @@ -67,13 +75,12 @@ M.close = function(val) vim.g.nvmark_hovered = nil end -M.get_hl = function(name) - local hexadecimal_to_hex = function(hex) - return "#" .. ("%06x"):format(hex == nil and 0 or hex) - end - - local hl = api.nvim_get_hl(0, { name = name }) +--- @param name string +--- @return vim.api.keyset.highlight result +function M.get_hl(name) + ---@type vim.api.keyset.highlight local result = {} + local hl = api.nvim_get_hl(0, { name = name }) if hl.fg ~= nil then result.fg = hexadecimal_to_hex(hl.fg)