2022-05-07 21:50:20 +02:00
|
|
|
module console
|
2022-05-09 16:16:25 +02:00
|
|
|
|
|
|
|
import arrays
|
|
|
|
import strings
|
2022-06-01 13:59:52 +02:00
|
|
|
import cli
|
|
|
|
import os
|
2022-05-09 16:16:25 +02:00
|
|
|
|
2022-09-11 21:50:29 +02:00
|
|
|
// tabbed_table returns a simple textual table, with tabs as separators.
|
|
|
|
pub fn tabbed_table(data [][]string) string {
|
|
|
|
return data.map(it.join('\t')).join('\n')
|
|
|
|
}
|
|
|
|
|
2022-05-09 16:16:25 +02:00
|
|
|
// pretty_table converts a list of string data into a pretty table. Many thanks
|
|
|
|
// to @hungrybluedev in the Vlang Discord for providing this code!
|
|
|
|
// https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn pretty_table(header []string, data [][]string) !string {
|
2022-05-09 16:16:25 +02:00
|
|
|
column_count := header.len
|
|
|
|
|
|
|
|
mut column_widths := []int{len: column_count, init: header[it].len}
|
|
|
|
|
|
|
|
for values in data {
|
|
|
|
for col, value in values {
|
|
|
|
if column_widths[col] < value.len {
|
|
|
|
column_widths[col] = value.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
single_line_length := arrays.sum(column_widths)! + (column_count + 1) * 3 - 4
|
2022-05-09 16:16:25 +02:00
|
|
|
|
|
|
|
horizontal_line := '+' + strings.repeat(`-`, single_line_length) + '+'
|
|
|
|
mut buffer := strings.new_builder(data.len * single_line_length)
|
|
|
|
|
|
|
|
buffer.writeln(horizontal_line)
|
|
|
|
|
|
|
|
buffer.write_string('| ')
|
|
|
|
for col, head in header {
|
|
|
|
if col != 0 {
|
|
|
|
buffer.write_string(' | ')
|
|
|
|
}
|
|
|
|
buffer.write_string(head)
|
|
|
|
buffer.write_string(strings.repeat(` `, column_widths[col] - head.len))
|
|
|
|
}
|
|
|
|
buffer.writeln(' |')
|
|
|
|
|
|
|
|
buffer.writeln(horizontal_line)
|
|
|
|
|
|
|
|
for values in data {
|
|
|
|
buffer.write_string('| ')
|
|
|
|
for col, value in values {
|
|
|
|
if col != 0 {
|
|
|
|
buffer.write_string(' | ')
|
|
|
|
}
|
|
|
|
buffer.write_string(value)
|
|
|
|
buffer.write_string(strings.repeat(` `, column_widths[col] - value.len))
|
|
|
|
}
|
|
|
|
buffer.writeln(' |')
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.writeln(horizontal_line)
|
|
|
|
|
|
|
|
return buffer.str()
|
|
|
|
}
|
2022-06-01 13:59:52 +02:00
|
|
|
|
|
|
|
// export_man_pages recursively generates all man pages for the given
|
|
|
|
// cli.Command & writes them to the given directory.
|
2022-11-01 21:10:45 +01:00
|
|
|
pub fn export_man_pages(cmd cli.Command, path string) ! {
|
2022-06-01 13:59:52 +02:00
|
|
|
man := cmd.manpage()
|
|
|
|
os.write_file(os.join_path_single(path, cmd.full_name().replace(' ', '-') + '.1'),
|
2022-11-01 21:10:45 +01:00
|
|
|
man)!
|
2022-06-01 13:59:52 +02:00
|
|
|
|
|
|
|
for sub_cmd in cmd.commands {
|
2022-11-01 21:10:45 +01:00
|
|
|
export_man_pages(sub_cmd, path)!
|
2022-06-01 13:59:52 +02:00
|
|
|
}
|
|
|
|
}
|