nvim/lua/cosmic/utils/init.lua
2023-01-06 10:09:34 -08:00

68 lines
1.2 KiB
Lua

local M = {}
function M.map(mode, lhs, rhs, opts)
local defaults = {
silent = true,
noremap = true,
}
vim.keymap.set(mode, lhs, rhs, M.merge(defaults, opts or {}))
end
function M.create_buf_map(opts)
return function(mode, lhs, rhs)
M.map(mode, lhs, rhs, opts or {})
end
end
function M.merge_list(tbl1, tbl2)
for _, v in ipairs(tbl2) do
table.insert(tbl1, v)
end
return tbl1
end
function M.merge(...)
return vim.tbl_deep_extend('force', ...)
end
function M.split(str, sep)
local res = {}
for w in str:gmatch('([^' .. sep .. ']*)') do
if w ~= '' then
table.insert(res, w)
end
end
return res
end
function M.get_short_file_path(path)
local dirs = {}
for dir in string.gmatch(path, '([^/]+)') do
table.insert(dirs, dir)
end
local n = #dirs
if n > 3 then
return '../' .. dirs[n - 2] .. '/' .. dirs[n - 1] .. '/' .. dirs[n]
end
return path
end
function M.get_short_cwd()
local parts = vim.split(vim.fn.getcwd(), '/')
return parts[#parts]
end
function M.diff_source()
local gitsigns = vim.b.gitsigns_status_dict
if gitsigns then
return {
added = gitsigns.added,
modified = gitsigns.changed,
removed = gitsigns.removed,
}
end
end
return M