Skip to content

Commit

Permalink
improve utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mosheavni committed Jan 30, 2025
1 parent 3c43747 commit 70fb6c0
Showing 1 changed file with 78 additions and 76 deletions.
154 changes: 78 additions & 76 deletions nvim/.config/nvim/lua/user/utils.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local M = {}

---Creates an augroup while clearing previous
--- @param name string The name of the augroup.
---@param name string The name of the augroup.
---@return number id The augroup id
M.augroup = function(name)
function M.augroup(name)
return vim.api.nvim_create_augroup(name, { clear = true })
end

Expand Down Expand Up @@ -37,103 +37,103 @@ function! ReplaceMotion(motion, text)
endfunction
]]

M.get_visual_selection = function()
-- Cache commonly used vim.fn calls
local getpos = vim.fn.getpos
local getregion = vim.fn.getregion
local getcwd = vim.fn.getcwd

-- Consolidated VimL functions into Lua
function M.get_visual_selection()
local mode = vim.api.nvim_get_mode().mode
local opts = {}
-- \22 is an escaped version of <c-v>
if mode == 'v' or mode == 'V' or mode == '\22' then
opts.type = mode
return getregion(getpos 'v', getpos '.', { type = mode })
end
return vim.fn.getregion(vim.fn.getpos 'v', vim.fn.getpos '.', opts)
return getregion(getpos 'v', getpos '.')
end

M.get_os_command_output = function(cmd, cwd)
local Job = require 'plenary.job'
if not cwd then
cwd = vim.fn.getcwd()
end
function M.get_os_command_output(cmd, cwd)
if type(cmd) ~= 'table' then
M.pretty_print('cmd has to be a table', vim.log.leger.ERROR, [[๐Ÿ–ฅ๏ธ]])
M.pretty_print('cmd has to be a table', vim.log.levels.ERROR, '๐Ÿ–ฅ๏ธ')
return '', -1, ''
end

local Job = require 'plenary.job'
local command = table.remove(cmd, 1)
local stderr = {}

---@diagnostic disable-next-line: missing-fields
local stdout, ret = Job:new({
command = command,
args = cmd,
cwd = cwd,
cwd = cwd or getcwd(),
on_stderr = function(_, data)
table.insert(stderr, data)
end,
}):sync()

return stdout, ret, stderr
end

--- Pretty print using vim.notify
---@param message string: The message to print
---@param title? string: The title of the notification
---@param icon? string: The icon of the notification
---@param level? number: The log level of the notification (vim.log.levels.INFO by default)
---@param timeout? number: The timeout of the notification
---@return nil
M.pretty_print = function(message, title, icon, level, timeout)
if not icon then
icon = '๏š'
end
if not title then
title = 'Neovim'
end
if not level then
level = vim.log.levels.INFO
end
if not timeout then
timeout = 3000
end
vim.notify(message, level, { title = title, icon = icon, timeout = timeout })
---Pretty print using vim.notify
---@param message string The message to print
---@param title? string The title of the notification
---@param icon? string The icon of the notification
---@param level? number The log level of the notification
---@param timeout? number The timeout of the notification
function M.pretty_print(message, title, icon, level, timeout)
vim.notify(message, level or vim.log.levels.INFO, {
title = title or 'Neovim',
icon = icon or '๏š',
timeout = timeout or 3000,
})
end

-- Precompute byte offsets for country code conversion
local COUNTRY_CODE_OFFSET = 127397

---Converts country code to emoji of the country flag
---@param country_iso string: The country code in 2 uppercase letters
---@return string: emoji of the country flag
M.country_os_to_emoji = function(country_iso)
local flag_icon = ''
---@param country_iso string The country code in 2 uppercase letters
---@return string emoji of the country flag
function M.country_os_to_emoji(country_iso)
local flag_icon = {}
for i = 1, #country_iso do
local code_point = country_iso:byte(i) + 127397
local code_point = country_iso:byte(i) + COUNTRY_CODE_OFFSET
if code_point <= 0x7F then
flag_icon = flag_icon .. string.char(code_point)
table.insert(flag_icon, string.char(code_point))
elseif code_point <= 0x7FF then
flag_icon = flag_icon .. string.char(0xC0 + math.floor(code_point / 0x40), 0x80 + code_point % 0x40)
table.insert(flag_icon, string.char(0xC0 + math.floor(code_point / 0x40), 0x80 + code_point % 0x40))
elseif code_point <= 0xFFFF then
flag_icon = flag_icon .. string.char(0xE0 + math.floor(code_point / 0x1000), 0x80 + math.floor((code_point % 0x1000) / 0x40), 0x80 + code_point % 0x40)
table.insert(flag_icon, string.char(0xE0 + math.floor(code_point / 0x1000), 0x80 + math.floor((code_point % 0x1000) / 0x40), 0x80 + code_point % 0x40))
elseif code_point <= 0x10FFFF then
flag_icon = flag_icon
.. string.char(
table.insert(
flag_icon,
string.char(
0xF0 + math.floor(code_point / 0x40000),
0x80 + math.floor((code_point % 0x40000) / 0x1000),
0x80 + math.floor((code_point % 0x1000) / 0x40),
0x80 + code_point % 0x40
)
)
end
end
return flag_icon
return table.concat(flag_icon)
end

--- Get the next index in a table after the current element
--- @param tbl table The table to search in
--- @param cur any The current element to find
--- @return number index The next index in the table (loops back to 1)
M.tbl_get_next = function(tbl, cur)
local idx = 1
---Get the next index in a table after the current element
---@param tbl table The table to search in
---@param cur any The current element to find
---@return number index The next index in the table (loops back to 1)
function M.tbl_get_next(tbl, cur)
for i, v in ipairs(tbl) do
if v == cur then
idx = i % #tbl + 1
break
return i % #tbl + 1
end
end
return idx
return 1
end

-- Static mappings
M.filetype_to_extension = {
bash = 'sh',
javascript = 'js',
Expand Down Expand Up @@ -163,28 +163,30 @@ M.filetype_to_command = {
json = 'jq',
}

M.random_emoji = function()
local emojis = {
'๐Ÿคฉ',
'๐Ÿ‘ป',
'๐Ÿ˜ˆ',
'โœจ',
'๐Ÿ‘ฐ',
'๐Ÿ‘‘',
'๐Ÿ’ฏ',
'๐Ÿ’–',
'๐ŸŒ’',
'๐Ÿ‡ฎ๐Ÿ‡ฑ',
'โ˜…',
'โš“๏ธ',
'๐Ÿ™‰',
'โ˜˜๏ธ',
'๐ŸŒ',
'๐Ÿฅจ',
'๐Ÿ”ฅ',
'๐Ÿš€',
}
return emojis[math.random(#emojis)]
-- Cache emojis table
local EMOJIS = {
'๐Ÿคฉ',
'๐Ÿ‘ป',
'๐Ÿ˜ˆ',
'โœจ',
'๐Ÿ‘ฐ',
'๐Ÿ‘‘',
'๐Ÿ’ฏ',
'๐Ÿ’–',
'๐ŸŒ’',
'๐Ÿ‡ฎ๐Ÿ‡ฑ',
'โ˜…',
'โš“๏ธ',
'๐Ÿ™‰',
'โ˜˜๏ธ',
'๐ŸŒ',
'๐Ÿฅจ',
'๐Ÿ”ฅ',
'๐Ÿš€',
}

function M.random_emoji()
return EMOJIS[math.random(#EMOJIS)]
end

return M

0 comments on commit 70fb6c0

Please sign in to comment.