Expand your neovim sense with small UI components
0

Configure Feed

Select the types of activity you want to include in your feed.

chore: remove hlutils

+59 -74
+59 -17
lua/sense/highlights.lua
··· 1 - local hlutils = require("sense.utils.hlutils") 1 + local M = {} 2 2 3 - local M = {} 3 + local function dec_to_rgb(num) 4 + return { 5 + r = bit.rshift(num, 16), 6 + g = bit.band(bit.rshift(num, 8), 0xFF), 7 + b = bit.band(num, 0xFF), 8 + } 9 + end 4 10 5 - local function fill_bg(name) 6 - local hl = hlutils.read_hl(0, { name = name, link = false }) 7 - assert(hl) 8 - assert(hl.fg) 9 - if not hl.bg then 10 - hl.bg = hlutils.tint(hl.fg, -0.8) 11 + local function rgb_to_dec(rgb) 12 + return bit.lshift(rgb.r, 16) + bit.lshift(rgb.g, 8) + rgb.b 13 + end 14 + 15 + ---@param ns_id integer 16 + ---@param opts vim.api.keyset.get_highlight 17 + local function read_hl(ns_id, opts) 18 + opts.link = false 19 + local hl = vim.api.nvim_get_hl(ns_id, opts) 20 + local rich_hl = {} 21 + if vim.tbl_isempty(hl) then 22 + return 11 23 end 12 - return hl 24 + rich_hl.fg = hl.fg and dec_to_rgb(hl.fg) 25 + rich_hl.bg = hl.bg and dec_to_rgb(hl.bg) 26 + return rich_hl 27 + end 28 + 29 + ---@param c table A hex color 30 + ---@param percent number a negative number darkens and a positive one brightens 31 + local function tint(c, percent) 32 + if not c.r or not c.g or not c.b then 33 + return "NONE" 34 + end 35 + local blend = function(component) 36 + component = math.floor(component * (1 + percent)) 37 + return math.min(math.max(component, 0), 255) 38 + end 39 + return { 40 + r = blend(c.r), 41 + g = blend(c.g), 42 + b = blend(c.b), 43 + } 44 + end 45 + 46 + local function set(name, from) 47 + local hl = read_hl(0, { name = from, link = false }) 48 + if hl and hl.fg and not hl.bg then 49 + hl.bg = rgb_to_dec(tint(hl.fg, -0.8)) 50 + hl.fg = hl.fg and rgb_to_dec(hl.fg) 51 + else 52 + hl = { link = from } 53 + end 54 + vim.api.nvim_set_hl(0, name, hl) 13 55 end 14 56 15 57 function M.setup() 16 58 -- generate hl-groups based on existing colors 17 - hlutils.set_hl("SenseVirtualTextError", fill_bg("DiagnosticVirtualTextError")) 18 - hlutils.set_hl("SenseVirtualTextWarn", fill_bg("DiagnosticVirtualTextWarn")) 19 - hlutils.set_hl("SenseVirtualTextInfo", fill_bg("DiagnosticVirtualTextInfo")) 20 - hlutils.set_hl("SenseVirtualTextHint", fill_bg("DiagnosticVirtualTextHint")) 21 - hlutils.set_hl("SenseStatusColError", fill_bg("DiagnosticVirtualTextError")) 22 - hlutils.set_hl("SenseStatusColWarn", fill_bg("DiagnosticVirtualTextWarn")) 23 - hlutils.set_hl("SenseStatusColInfo", fill_bg("DiagnosticVirtualTextInfo")) 24 - hlutils.set_hl("SenseStatusColHint", fill_bg("DiagnosticVirtualTextHint")) 59 + set("SenseVirtualTextError", "DiagnosticVirtualTextError") 60 + set("SenseVirtualTextWarn", "DiagnosticVirtualTextWarn") 61 + set("SenseVirtualTextInfo", "DiagnosticVirtualTextInfo") 62 + set("SenseVirtualTextHint", "DiagnosticVirtualTextHint") 63 + set("SenseStatusColError", "DiagnosticVirtualTextError") 64 + set("SenseStatusColWarn", "DiagnosticVirtualTextWarn") 65 + set("SenseStatusColInfo", "DiagnosticVirtualTextInfo") 66 + set("SenseStatusColHint", "DiagnosticVirtualTextHint") 25 67 end 26 68 27 69 return M
-57
lua/sense/utils/hlutils.lua
··· 1 - local bit = require("bit") 2 - 3 - local M = {} 4 - 5 - function M.dec_to_rgb(num) 6 - return { 7 - r = bit.rshift(num, 16), 8 - g = bit.band(bit.rshift(num, 8), 0xFF), 9 - b = bit.band(num, 0xFF), 10 - } 11 - end 12 - 13 - function M.rgb_to_dec(rgb) 14 - return bit.lshift(rgb.r, 16) + bit.lshift(rgb.g, 8) + rgb.b 15 - end 16 - 17 - ---@param ns_id integer 18 - ---@param opts vim.api.keyset.get_highlight 19 - function M.read_hl(ns_id, opts) 20 - local hl = vim.api.nvim_get_hl(ns_id, opts) 21 - local rich_hl = {} 22 - if vim.tbl_isempty(hl) then 23 - return nil 24 - end 25 - -- if hl.link then 26 - -- return M.read_hl(ns_id, { name = hl.link }) 27 - -- end 28 - rich_hl.fg = hl.fg and M.dec_to_rgb(hl.fg) 29 - rich_hl.bg = hl.bg and M.dec_to_rgb(hl.bg) 30 - return rich_hl 31 - end 32 - 33 - ---@param c table A hex color 34 - ---@param percent number a negative number darkens and a positive one brightens 35 - function M.tint(c, percent) 36 - if not c.r or not c.g or not c.b then 37 - return "NONE" 38 - end 39 - local blend = function(component) 40 - component = math.floor(component * (1 + percent)) 41 - return math.min(math.max(component, 0), 255) 42 - end 43 - return { 44 - r = blend(c.r), 45 - g = blend(c.g), 46 - b = blend(c.b), 47 - } 48 - end 49 - 50 - function M.set_hl(name, hl) 51 - vim.api.nvim_set_hl(0, name, { 52 - fg = M.rgb_to_dec(hl.fg), 53 - bg = M.rgb_to_dec(hl.bg), 54 - }) 55 - end 56 - 57 - return M