cli: calculate indent based on name length
parent
b17ed79773
commit
76176eddab
|
@ -4,9 +4,9 @@ import term
|
||||||
import strings
|
import strings
|
||||||
|
|
||||||
const (
|
const (
|
||||||
c_base_indent = 2
|
base_indent_len = 2
|
||||||
c_abbrev_indent = 5
|
min_description_indent_len = 20
|
||||||
c_description_indent = 20
|
spacing = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
fn help_flag() Flag {
|
fn help_flag() Flag {
|
||||||
|
@ -39,15 +39,26 @@ fn help_func(help_cmd Command) {
|
||||||
if cmd.description != '' {
|
if cmd.description != '' {
|
||||||
help += '${cmd.description}\n\n'
|
help += '${cmd.description}\n\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mut abbrev_len := 0
|
||||||
|
mut name_len := min_description_indent_len
|
||||||
|
for flag in cmd.flags {
|
||||||
|
abbrev_len = max(abbrev_len, flag.abbrev.len + spacing + 1) // + 1 for '-' in front
|
||||||
|
name_len = max(name_len, abbrev_len + flag.name.len + spacing + 2) // + 2 for '--' in front
|
||||||
|
}
|
||||||
|
for command in cmd.commands {
|
||||||
|
name_len = max(name_len, command.name.len + spacing)
|
||||||
|
}
|
||||||
|
|
||||||
if cmd.flags.len > 0 {
|
if cmd.flags.len > 0 {
|
||||||
help += 'Flags:\n'
|
help += 'Flags:\n'
|
||||||
for flag in cmd.flags {
|
for flag in cmd.flags {
|
||||||
mut flag_name := ''
|
mut flag_name := ''
|
||||||
if flag.abbrev != '' {
|
if flag.abbrev != '' {
|
||||||
abbrev_indent := ' '.repeat(max(c_abbrev_indent-(flag.abbrev.len+1), 1))
|
abbrev_indent := ' '.repeat(abbrev_len - flag.abbrev.len - 1) // - 1 for '-' in front
|
||||||
flag_name = '-${flag.abbrev}${abbrev_indent}--${flag.name}'
|
flag_name = '-${flag.abbrev}${abbrev_indent}--${flag.name}'
|
||||||
} else {
|
} else {
|
||||||
abbrev_indent := ' '.repeat(max(c_abbrev_indent-(flag.abbrev.len), 1))
|
abbrev_indent := ' '.repeat(abbrev_len)
|
||||||
flag_name = '${abbrev_indent}--${flag.name}'
|
flag_name = '${abbrev_indent}--${flag.name}'
|
||||||
}
|
}
|
||||||
mut required := ''
|
mut required := ''
|
||||||
|
@ -55,21 +66,21 @@ fn help_func(help_cmd Command) {
|
||||||
required = ' (required)'
|
required = ' (required)'
|
||||||
}
|
}
|
||||||
|
|
||||||
base_indent := ' '.repeat(c_base_indent)
|
base_indent := ' '.repeat(base_indent_len)
|
||||||
description_indent := ' '.repeat(max(c_description_indent-flag_name.len, 1))
|
description_indent := ' '.repeat(name_len - flag_name.len)
|
||||||
help += '${base_indent}${flag_name}${description_indent}' +
|
help += '${base_indent}${flag_name}${description_indent}' +
|
||||||
pretty_description(flag.description + required) + '\n'
|
pretty_description(flag.description + required, base_indent_len + name_len) + '\n'
|
||||||
}
|
}
|
||||||
help += '\n'
|
help += '\n'
|
||||||
}
|
}
|
||||||
if cmd.commands.len > 0 {
|
if cmd.commands.len > 0 {
|
||||||
help += 'Commands:\n'
|
help += 'Commands:\n'
|
||||||
for command in cmd.commands {
|
for command in cmd.commands {
|
||||||
base_indent := ' '.repeat(c_base_indent)
|
base_indent := ' '.repeat(base_indent_len)
|
||||||
description_indent := ' '.repeat(max(c_description_indent-command.name.len, 1))
|
description_indent := ' '.repeat(name_len - command.name.len)
|
||||||
|
|
||||||
help += '${base_indent}${command.name}${description_indent}' +
|
help += '${base_indent}${command.name}${description_indent}' +
|
||||||
pretty_description(command.description) + '\n'
|
pretty_description(command.description, name_len) + '\n'
|
||||||
}
|
}
|
||||||
help += '\n'
|
help += '\n'
|
||||||
}
|
}
|
||||||
|
@ -79,14 +90,14 @@ fn help_func(help_cmd Command) {
|
||||||
|
|
||||||
// pretty_description resizes description text depending on terminal width.
|
// pretty_description resizes description text depending on terminal width.
|
||||||
// Essentially, smart wrap-around
|
// Essentially, smart wrap-around
|
||||||
fn pretty_description(s string) string {
|
fn pretty_description(s string, indent_len int) 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 c_description_indent > width {
|
if indent_len > width {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
indent := ' '.repeat(c_description_indent + 2)
|
indent := ' '.repeat(indent_len)
|
||||||
chars_per_line := width - c_description_indent
|
chars_per_line := width - indent_len
|
||||||
// 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))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue