You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nvim-libmodal/lua/libmodal/src/utils/Help.lua

86 lines
2.1 KiB
Lua

--[[/* IMPORTS */]]
local globals = require('libmodal/src/globals')
--[[/* Utilities */]]
--- Align `tbl` according to the `longestKeyLen`.
--- @param tbl table what to align.
--- @param longestKeyLen number how long the longest key is.
--- @return table aligned
local function tabAlign(tbl, longestKeyLen)
local toPrint = {}
for key, value in pairs(tbl) do
toPrint[#toPrint + 1] = key
local len = string.len(key)
local byte = string.byte(key)
-- account for ASCII chars that take up more space.
if byte <= 32 or byte == 127 then len = len + 1 end
for _ = len, longestKeyLen do
toPrint[#toPrint + 1] = ' '
end
toPrint[#toPrint + 1] = table.concat(
{'', '\n'},
(type(value) == globals.TYPE_STR) and value or '<lua function>'
)
end
return toPrint
end
--[[/* MODULE */]]
local Help = {TYPE = 'libmodal-help'}
--[[/* META `Help` */]]
4 years ago
4 years ago
local _metaHelp = require('libmodal/src/classes').new(Help.TYPE)
4 years ago
--- Show the contents of this `Help`.
4 years ago
function _metaHelp:show()
for _, helpText in ipairs(self) do
print(helpText)
end
vim.fn.getchar()
4 years ago
end
--[[/* CLASS `Help` */]]
--- Create a default help table with `commandsOrMaps` and vim expressions.
--- @param commandsOrMaps table commands or mappings to vim expressions.
--- @return table Help
function Help.new(commandsOrMaps, title)
-- find the longest key in the table.
local longestKeyLen = 0
for key, _ in pairs(commandsOrMaps) do
local keyLen = string.len(key)
if keyLen > longestKeyLen then
longestKeyLen = keyLen
end
end
-- adjust the longest key length if the table header is longer.
if longestKeyLen < string.len(title) then
longestKeyLen = string.len(title)
end
-- define the separator for the help table.
local helpSeparator = {}
for i = 1, string.len(title) do helpSeparator[i] = '-' end
helpSeparator = table.concat(helpSeparator)
-- Create a new `Help`.
4 years ago
return setmetatable(
{
[1] = ' ',
[2] = table.concat(tabAlign({[title] = 'VIM EXPRESSION'}, longestKeyLen)),
[3] = table.concat(tabAlign({[helpSeparator] = '--------------'}, longestKeyLen)),
[4] = table.concat(tabAlign(commandsOrMaps, longestKeyLen)),
4 years ago
},
_metaHelp
)
end
return Help