2019-11-21 15:03:12 +03:00
|
|
|
module cli
|
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import term
|
|
|
|
import strings
|
2020-04-20 15:50:15 +03:00
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
const (
|
2020-06-29 18:59:55 +03:00
|
|
|
base_indent_len = 2
|
|
|
|
min_description_indent_len = 20
|
|
|
|
spacing = 2
|
2019-11-21 15:03:12 +03:00
|
|
|
)
|
|
|
|
|
2020-07-01 11:54:34 +03:00
|
|
|
fn help_flag(with_abbrev bool) Flag {
|
2019-11-21 15:03:12 +03:00
|
|
|
return Flag{
|
|
|
|
flag: .bool,
|
|
|
|
name: 'help',
|
2020-07-01 11:54:34 +03:00
|
|
|
abbrev: if with_abbrev { 'h' } else { '' },
|
2019-11-21 15:03:12 +03:00
|
|
|
description: 'Prints help information',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help_cmd() Command {
|
|
|
|
return Command{
|
|
|
|
name: 'help',
|
|
|
|
description: 'Prints help information',
|
|
|
|
execute: help_func,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:04:53 +03:00
|
|
|
fn help_func(help_cmd Command) {
|
|
|
|
cmd := help_cmd.parent
|
2019-11-21 15:03:12 +03:00
|
|
|
full_name := cmd.full_name()
|
|
|
|
|
|
|
|
mut help := ''
|
|
|
|
help += 'Usage: ${full_name}'
|
|
|
|
if cmd.flags.len > 0 { help += ' [FLAGS]'}
|
|
|
|
if cmd.commands.len > 0 { help += ' [COMMANDS]'}
|
|
|
|
help += '\n\n'
|
|
|
|
|
|
|
|
if cmd.description != '' {
|
|
|
|
help += '${cmd.description}\n\n'
|
|
|
|
}
|
2020-06-29 18:59:55 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:03:12 +03:00
|
|
|
if cmd.flags.len > 0 {
|
|
|
|
help += 'Flags:\n'
|
|
|
|
for flag in cmd.flags {
|
|
|
|
mut flag_name := ''
|
|
|
|
if flag.abbrev != '' {
|
2020-06-29 18:59:55 +03:00
|
|
|
abbrev_indent := ' '.repeat(abbrev_len - flag.abbrev.len - 1) // - 1 for '-' in front
|
2019-11-21 15:03:12 +03:00
|
|
|
flag_name = '-${flag.abbrev}${abbrev_indent}--${flag.name}'
|
|
|
|
} else {
|
2020-06-29 18:59:55 +03:00
|
|
|
abbrev_indent := ' '.repeat(abbrev_len)
|
2019-11-21 15:03:12 +03:00
|
|
|
flag_name = '${abbrev_indent}--${flag.name}'
|
|
|
|
}
|
|
|
|
mut required := ''
|
|
|
|
if flag.required {
|
|
|
|
required = ' (required)'
|
|
|
|
}
|
|
|
|
|
2020-06-29 18:59:55 +03:00
|
|
|
base_indent := ' '.repeat(base_indent_len)
|
|
|
|
description_indent := ' '.repeat(name_len - flag_name.len)
|
2020-04-20 15:50:15 +03:00
|
|
|
help += '${base_indent}${flag_name}${description_indent}' +
|
2020-06-29 18:59:55 +03:00
|
|
|
pretty_description(flag.description + required, base_indent_len + name_len) + '\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
help += '\n'
|
|
|
|
}
|
|
|
|
if cmd.commands.len > 0 {
|
|
|
|
help += 'Commands:\n'
|
|
|
|
for command in cmd.commands {
|
2020-06-29 18:59:55 +03:00
|
|
|
base_indent := ' '.repeat(base_indent_len)
|
|
|
|
description_indent := ' '.repeat(name_len - command.name.len)
|
2019-11-21 15:03:12 +03:00
|
|
|
|
2020-04-20 15:50:15 +03:00
|
|
|
help += '${base_indent}${command.name}${description_indent}' +
|
2020-06-29 18:59:55 +03:00
|
|
|
pretty_description(command.description, name_len) + '\n'
|
2019-11-21 15:03:12 +03:00
|
|
|
}
|
|
|
|
help += '\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
print(help)
|
|
|
|
}
|
2020-04-17 00:01:04 +03:00
|
|
|
|
2020-04-20 15:50:15 +03:00
|
|
|
// pretty_description resizes description text depending on terminal width.
|
|
|
|
// Essentially, smart wrap-around
|
2020-06-29 18:59:55 +03:00
|
|
|
fn pretty_description(s string, indent_len int) string {
|
2020-04-20 15:50:15 +03:00
|
|
|
width, _ := term.get_terminal_size()
|
|
|
|
// Don't prettify if the terminal is that small, it won't be pretty anyway.
|
2020-06-29 18:59:55 +03:00
|
|
|
if indent_len > width {
|
2020-04-20 15:50:15 +03:00
|
|
|
return s
|
|
|
|
}
|
2020-06-29 18:59:55 +03:00
|
|
|
indent := ' '.repeat(indent_len)
|
|
|
|
chars_per_line := width - indent_len
|
2020-04-20 15:50:15 +03:00
|
|
|
// Give us enough room, better a little bigger than smaller
|
|
|
|
mut acc := strings.new_builder(((s.len / chars_per_line) + 1) * (width + 1))
|
|
|
|
|
2020-06-29 15:47:20 +03:00
|
|
|
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 < 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(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())
|
2020-04-20 15:50:15 +03:00
|
|
|
}
|
|
|
|
return acc.str()
|
|
|
|
}
|
|
|
|
|
2020-04-17 00:01:04 +03:00
|
|
|
fn max(a, b int) int {
|
|
|
|
return if a > b {a} else {b}
|
|
|
|
}
|