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/api.lua

89 lines
2.3 KiB
Lua

--[[/* IMPORTS */]]
local globals = require('libmodal/src/globals')
local HighlightSegment = require('libmodal/src/Indicator/HighlightSegment')
--[[/* MODULE */]]
local api = {}
--- Send a character to exit a mode.
--- @param exit_char string the character used to exit the mode, or ESCAPE if none was provided.
function api.mode_exit(exit_char)
-- If there was no provided `exit_char`, or it is a character code.
if not exit_char or type(exit_char) == globals.TYPE_NUM then
-- Translate the character code or default to escape.
exit_char = string.char(exit_char or globals.ESC_NR)
end
-- Exit the prompt by sending an escape key.
vim.api.nvim_feedkeys(exit_char, 'nt', false)
end
--- Make vim ring the visual/audio bell, if it is enabled.
function api.nvim_bell()
vim.api.nvim_command('normal '..string.char(27)) -- escape char
end
--- Gets one character of user input, as a number.
function api.nvim_input()
return vim.fn.getchar()
end
--------------------------
--[[ SUMMARY:
* Run `mode` to refresh the screen.
* The function was not named `nvim_mode` because that would be really confusing given the name of this plugin.
]]
--------------------------
function api.nvim_redraw()
vim.api.nvim_command 'mode'
end
---------------------------------
--[[ SUMMARY:
* Echo a table of {`hlgroup`, `str`} tables.
* Meant to be read as "nvim list echo".
]]
--[[ PARAMS:
* `hlTables` => the tables to echo with highlights.
]]
---------------------------------
4 years ago
local lecho_template = {
[1] = "echohl ",
[2] = nil,
[3] = " | echon '",
[4] = nil,
[5] = "'"
}
function api.nvim_lecho(hlTables)
api.nvim_redraw()
for _, hlTable in ipairs(hlTables) do
-- `:echohl` the hlgroup and then `:echon` the string
4 years ago
lecho_template[2] = tostring(hlTable.hl)
lecho_template[4] = tostring(hlTable.str)
vim.api.nvim_command(table.concat(lecho_template))
end
vim.api.nvim_command 'echohl None'
end
--------------------------------------
--[[ SUMMARY:
* Show a `title` error.
]]
--[[ PARAMS:
* `title` => the title of the error.
* `msg` => the message of the error.
]]
--------------------------------------
function api.nvim_show_err(title, msg)
api.nvim_lecho({
HighlightSegment.new('Title', tostring(title)..'\n'),
HighlightSegment.new('Error', tostring(msg)),
})
vim.fn.getchar()
end
return api