mirror of
https://github.com/chenasraf/input-form.nvim.git
synced 2026-05-18 01:38:59 +00:00
feat: initial commit
Release-As: 0.1.0
This commit is contained in:
42
lua/input-form/utils.lua
Normal file
42
lua/input-form/utils.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
local M = {}
|
||||
|
||||
--- Deep-merge two tables, with `t2` taking precedence over `t1`.
|
||||
---@param t1 table
|
||||
---@param t2 table
|
||||
---@return table
|
||||
function M.merge(t1, t2)
|
||||
return vim.tbl_deep_extend("force", t1 or {}, t2 or {})
|
||||
end
|
||||
|
||||
--- Resolve a width value: if a float 0<v<=1, treat as a ratio of `vim.o.columns`.
|
||||
---@param value number
|
||||
---@return integer
|
||||
function M.resolve_width(value)
|
||||
if value > 0 and value <= 1 then
|
||||
return math.floor(vim.o.columns * value)
|
||||
end
|
||||
return math.floor(value)
|
||||
end
|
||||
|
||||
--- Resolve a height value similarly against `vim.o.lines`.
|
||||
---@param value number
|
||||
---@return integer
|
||||
function M.resolve_height(value)
|
||||
if value > 0 and value <= 1 then
|
||||
return math.floor(vim.o.lines * value)
|
||||
end
|
||||
return math.floor(value)
|
||||
end
|
||||
|
||||
--- Clamp an integer into [lo, hi].
|
||||
function M.clamp(v, lo, hi)
|
||||
if v < lo then
|
||||
return lo
|
||||
end
|
||||
if v > hi then
|
||||
return hi
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user