1+ --- @type libmodal.globals
12local globals = require ' libmodal/src/globals'
23
34--- @class libmodal.utils.Help
@@ -27,6 +28,32 @@ local function align_columns(tbl, longest_key_len)
2728 return to_print
2829end
2930
31+ --- create a default help table with `commands_or_maps` and vim expressions.
32+ --- @param commands_or_maps { [string] : fun ()| string } commands or mappings to vim expressions.
33+ --- @param title string
34+ --- @return libmodal.utils.Help
35+ function Help .new (commands_or_maps , title )
36+ -- find the longest key in the table, or at least the length of the title
37+ local longest_key_maps = string.len (title )
38+ for key , _ in pairs (commands_or_maps ) do
39+ local key_len = string.len (key )
40+ if key_len > longest_key_maps then
41+ longest_key_maps = key_len
42+ end
43+ end
44+
45+ -- create a new `Help`.
46+ return setmetatable (
47+ {
48+ [1 ] = ' ' ,
49+ [2 ] = table.concat (align_columns ({[title ] = ' VIM EXPRESSION' }, longest_key_maps )),
50+ [3 ] = table.concat (align_columns ({[string.rep (' -' , string.len (title ))] = ' --------------' }, longest_key_maps )),
51+ [4 ] = table.concat (align_columns (commands_or_maps , longest_key_maps )),
52+ },
53+ Help
54+ )
55+ end
56+
3057--- show the contents of this `Help`.
3158function Help :show ()
3259 for _ , help_text in ipairs (self ) do
@@ -35,33 +62,4 @@ function Help:show()
3562 vim .fn .getchar ()
3663end
3764
38- --[[ /* CLASS `Help` */]]
39-
40- return
41- {
42- --- create a default help table with `commands_or_maps` and vim expressions.
43- --- @param commands_or_maps { [string] : fun ()| string } commands or mappings to vim expressions.
44- --- @param title string
45- --- @return libmodal.utils.Help
46- new = function (commands_or_maps , title )
47- -- find the longest key in the table, or at least the length of the title
48- local longest_key_maps = string.len (title )
49- for key , _ in pairs (commands_or_maps ) do
50- local key_len = string.len (key )
51- if key_len > longest_key_maps then
52- longest_key_maps = key_len
53- end
54- end
55-
56- -- create a new `Help`.
57- return setmetatable (
58- {
59- [1 ] = ' ' ,
60- [2 ] = table.concat (align_columns ({[title ] = ' VIM EXPRESSION' }, longest_key_maps )),
61- [3 ] = table.concat (align_columns ({[string.rep (' -' , string.len (title ))] = ' --------------' }, longest_key_maps )),
62- [4 ] = table.concat (align_columns (commands_or_maps , longest_key_maps )),
63- },
64- Help
65- )
66- end
67- }
65+ return Help
0 commit comments