cli: add pretty print for multiline descriptions
parent
025652bb78
commit
4a1ce3e1f5
|
@ -82,28 +82,31 @@ fn help_func(help_cmd Command) {
|
|||
fn pretty_description(s string) string {
|
||||
width, _ := term.get_terminal_size()
|
||||
// Don't prettify if the terminal is that small, it won't be pretty anyway.
|
||||
if s.len + c_description_indent < width || c_description_indent > width {
|
||||
if c_description_indent > width {
|
||||
return s
|
||||
}
|
||||
indent := ' '.repeat(c_description_indent + 1)
|
||||
indent := ' '.repeat(c_description_indent + 2)
|
||||
chars_per_line := width - c_description_indent
|
||||
// Give us enough room, better a little bigger than smaller
|
||||
mut acc := strings.new_builder(((s.len / chars_per_line) + 1) * (width + 1))
|
||||
|
||||
for k, line in s.split('\n') {
|
||||
if k != 0 { acc.write('\n${indent}') }
|
||||
mut i := chars_per_line - 2
|
||||
mut j := 0
|
||||
for ; i < s.len ; i += chars_per_line - 2 {
|
||||
for s.str[i] != ` ` { i-- }
|
||||
for ; i < line.len; i += chars_per_line - 2 {
|
||||
for line.str[i] != ` ` { i-- }
|
||||
// indent was already done the first iteration
|
||||
if j != 0 { acc.write(indent) }
|
||||
acc.writeln(s[j..i])
|
||||
acc.writeln(line[j..i].trim_space())
|
||||
j = i
|
||||
}
|
||||
// We need this even though it should never happen
|
||||
if j != 0 {
|
||||
acc.write(indent)
|
||||
}
|
||||
acc.write(s[j..])
|
||||
acc.write(line[j..].trim_space())
|
||||
}
|
||||
return acc.str()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue