1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cli: add pretty print for multiline descriptions

This commit is contained in:
Tim Basel 2020-06-29 14:47:20 +02:00 committed by GitHub
parent 025652bb78
commit 4a1ce3e1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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