wrap ls and tags output

develop
myk002 2022-07-25 17:48:39 -07:00
parent 094f8d7257
commit 8cf7f59c39
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 19 additions and 11 deletions

@ -653,21 +653,31 @@ function help(entry)
print(get_entry_long_help(entry)) print(get_entry_long_help(entry))
end end
local function get_max_width(list, min_width) -- prints col1 (width 20), a 2 space gap, and col2 (width 58)
local width = min_width or 0 -- if col1text is longer than 20 characters, col2text is printed on the next
for _,item in ipairs(list) do -- line. if col2text is longer than 58 characters, it is wrapped.
width = math.max(width, #item) local COL1WIDTH, COL2WIDTH = 20, 58
local function print_columns(col1text, col2text)
col2text = col2text:wrap(COL2WIDTH)
local wrapped_col2 = {}
for line in col2text:gmatch('[^'..NEWLINE..']*') do
table.insert(wrapped_col2, line)
end
if #col1text > COL1WIDTH then
print(col1text)
else
print(('%-'..COL1WIDTH..'s %s'):format(col1text, wrapped_col2[1]))
end
for i=2,#wrapped_col2 do
print(('%'..COL1WIDTH..'s %s'):format(' ', wrapped_col2[i]))
end end
return width
end end
-- implements the 'tags' builtin command -- implements the 'tags' builtin command
function tags() function tags()
local tags = get_tags() local tags = get_tags()
local width = get_max_width(tags, 10)
for _,tag in ipairs(tags) do for _,tag in ipairs(tags) do
print((' %-'..width..'s %s'):format(tag, print_columns(tag, get_tag_data(tag).description)
get_tag_data(tag).description))
end end
end end
@ -675,10 +685,8 @@ end
-- defined as in search_entries() above. -- defined as in search_entries() above.
function list_entries(skip_tags, include, exclude) function list_entries(skip_tags, include, exclude)
local entries = search_entries(include, exclude) local entries = search_entries(include, exclude)
local width = get_max_width(entries, 10)
for _,entry in ipairs(entries) do for _,entry in ipairs(entries) do
print((' %-'..width..'s %s'):format( print_columns(entry, get_entry_short_help(entry))
entry, get_entry_short_help(entry)))
if not skip_tags then if not skip_tags then
local tags = get_entry_tags(entry) local tags = get_entry_tags(entry)
if #tags > 0 then if #tags > 0 then