From cf4f33f5ea34f9c4e9975299b094f1a2b8b34aa0 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Thu, 15 Sep 2022 10:00:00 +0530 Subject: Better structure and naming | Misc improvements --- lua/colorizer/utils.lua | 107 +++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 56 deletions(-) (limited to 'lua/colorizer/utils.lua') diff --git a/lua/colorizer/utils.lua b/lua/colorizer/utils.lua index 9dad9d8..c83c32d 100644 --- a/lua/colorizer/utils.lua +++ b/lua/colorizer/utils.lua @@ -5,6 +5,8 @@ local band, bor, rshift, lshift = bit.band, bit.bor, bit.rshift, bit.lshift local uv = vim.loop +local utils = {} + -- -- TODO use rgb as the return value from the matcher functions -- -- instead of the rgb_hex. Can be the highlight key as well -- -- when you shift it left 8 bits. Use the lower 8 bits for @@ -22,36 +24,38 @@ local CATEGORY_ALPHA = lshift(1, 1) local CATEGORY_HEX = lshift(1, 2) local CATEGORY_ALPHANUM = bor(CATEGORY_ALPHA, CATEGORY_DIGIT) --- do not run the loop multiple times -local b = string.byte -local byte_values = { ["0"] = b "0", ["9"] = b "9", ["a"] = b "a", ["f"] = b "f", ["z"] = b "z" } -local extra_char = { [b "-"] = true } - -for i = 0, 255 do - local v = 0 - local lowercase = bor(i, 0x20) - -- Digit is bit 1 - if i >= byte_values["0"] and i <= byte_values["9"] then - v = bor(v, lshift(1, 0)) - v = bor(v, lshift(1, 2)) - v = bor(v, lshift(i - byte_values["0"], 4)) - elseif lowercase >= byte_values["a"] and lowercase <= byte_values["z"] then - -- Alpha is bit 2 - v = bor(v, lshift(1, 1)) - if lowercase <= byte_values["f"] then +do + -- do not run the loop multiple times + local b = string.byte + local byte_values = { ["0"] = b "0", ["9"] = b "9", ["a"] = b "a", ["f"] = b "f", ["z"] = b "z" } + local extra_char = { [b "-"] = true } + + for i = 0, 255 do + local v = 0 + local lowercase = bor(i, 0x20) + -- Digit is bit 1 + if i >= byte_values["0"] and i <= byte_values["9"] then + v = bor(v, lshift(1, 0)) v = bor(v, lshift(1, 2)) - v = bor(v, lshift(lowercase - byte_values["a"] + 10, 4)) + v = bor(v, lshift(i - byte_values["0"], 4)) + elseif lowercase >= byte_values["a"] and lowercase <= byte_values["z"] then + -- Alpha is bit 2 + v = bor(v, lshift(1, 1)) + if lowercase <= byte_values["f"] then + v = bor(v, lshift(1, 2)) + v = bor(v, lshift(lowercase - byte_values["a"] + 10, 4)) + end + elseif extra_char[i] then + v = i end - elseif extra_char[i] then - v = i + BYTE_CATEGORY[i] = v end - BYTE_CATEGORY[i] = v end ---Obvious. ---@param byte number ---@return boolean -local function byte_is_alphanumeric(byte) +function utils.byte_is_alphanumeric(byte) local category = BYTE_CATEGORY[byte] return band(category, CATEGORY_ALPHANUM) ~= 0 end @@ -59,15 +63,33 @@ end ---Obvious. ---@param byte number ---@return boolean -local function byte_is_hex(byte) +function utils.byte_is_hex(byte) return band(BYTE_CATEGORY[byte], CATEGORY_HEX) ~= 0 end +--- Get last modified time of a file +---@param path string: file path +---@return number|nil: modified time +function utils.get_last_modified(path) + local fd = uv.fs_open(path, "r", 438) + if not fd then + return + end + + local stat = uv.fs_fstat(fd) + uv.fs_close(fd) + if stat then + return stat.mtime.nsec + else + return + end +end + ---Merge two tables. -- -- todo: Remove this and use `vim.tbl_deep_extend` ---@return table -local function merge(...) +function utils.merge(...) local res = {} for i = 1, select("#", ...) do local o = select(i, ...) @@ -84,7 +106,7 @@ end --- Obvious. ---@param byte number ---@return number -local function parse_hex(byte) +function utils.parse_hex(byte) return rshift(BYTE_CATEGORY[byte], 4) end @@ -92,7 +114,7 @@ local b_percent = string.byte "%" --- Obvious. ---@param v string ---@return number|nil -local function percent_or_hex(v) +function utils.percent_or_hex(v) if v:byte(-1) == b_percent then return tonumber(v:sub(1, -2)) / 100 * 255 end @@ -103,30 +125,12 @@ local function percent_or_hex(v) return x end ---- Get last modified time of a file ----@param path string: file path ----@return number|nil: modified time -local function get_last_modified(path) - local fd = uv.fs_open(path, "r", 438) - if not fd then - return - end - - local stat = uv.fs_fstat(fd) - uv.fs_close(fd) - if stat then - return stat.mtime.nsec - else - return - end -end - --- Watch a file for changes and execute callback ---@param path string: File path ---@param callback function: Callback to execute ---@param ... array: params for callback ---@return function|nil -local function watch_file(path, callback, ...) +function utils.watch_file(path, callback, ...) if not path or type(callback) ~= "function" then return end @@ -145,7 +149,7 @@ local function watch_file(path, callback, ...) callback(filename, unpack(args)) -- Debounce: stop/start. handle:stop() - if not err or not get_last_modified(filename) then + if not err or not utils.get_last_modified(filename) then start() end end @@ -165,13 +169,4 @@ local function watch_file(path, callback, ...) return handle end ---- @export -return { - byte_is_alphanumeric = byte_is_alphanumeric, - byte_is_hex = byte_is_hex, - merge = merge, - parse_hex = parse_hex, - percent_or_hex = percent_or_hex, - get_last_modified = get_last_modified, - watch_file = watch_file, -} +return utils -- cgit v1.2.3-70-g09d2