refactor(cli): write_usage and write_command_help now use text/table.

This commit is contained in:
2026-06-24 16:21:35 -04:00
parent 91d0800731
commit bd39e93785
3 changed files with 71 additions and 36 deletions

View File

@@ -6,7 +6,7 @@
3. Add color flag and support non colored output. 3. Add color flag and support non colored output.
4. Use text/tables for command output 4. Rewrite `write_command_help` to use text/tables
5. Generate md and man pages again. 5. Generate md and man pages again.

View File

@@ -5,6 +5,7 @@ import "core:fmt"
import "core:io" import "core:io"
import "core:os" import "core:os"
import "core:strings" import "core:strings"
import "core:text/table"
Command :: struct { Command :: struct {
name: string, name: string,
@@ -253,56 +254,47 @@ at before, restore your backup with:
%senvr%s [command] %senvr%s [command]
%sAvailable Commands:%s
`, `,
COLOR_HEADINGS, COLOR_HEADINGS,
ANSI_RESET, ANSI_RESET,
COLOR_FLAGS, COLOR_FLAGS,
ANSI_RESET, ANSI_RESET,
COLOR_HEADINGS,
ANSI_RESET,
flush = false, flush = false,
) )
tbl: table.Table
table.init(&tbl, context.temp_allocator, context.temp_allocator)
table.padding(&tbl, 2, 0)
table.caption(&tbl, "Available Commands:")
for c in COMMANDS { for c in COMMANDS {
name_start := len(c.name) name := c.name
fmt.wprintf(w, " %s%s", COLOR_COMMANDS, c.name, flush = false) // TODO: Can we do better?
for a in c.aliases { for a in c.aliases {
fmt.wprintf(w, ", %s", a, flush = false) name = strings.join([]string{name, a}, ", ", tbl.format_allocator)
name_start += len(a) + 2
} }
fmt.wprint(w, ANSI_RESET) table.row(&tbl, table.format(&tbl, "%s%s%s", COLOR_COMMANDS, name, ANSI_RESET), c.short)
padding := 20 - name_start
if padding > 0 {
for _ in 0 ..< padding {
io.write_byte(w, ' ')
}
}
fmt.wprintf(w, " %s\n", c.short, flush = false)
} }
write_borderless_table(w, &tbl)
table_reset(&tbl)
table.caption(&tbl, "Flags:")
table.row(&tbl, COLOR_FLAGS + "-h, --help" + ANSI_RESET, `show this documentation`)
table.row(
&tbl,
COLOR_FLAGS + "-c, --config-file" + ANSI_RESET + " <path>",
`config file (default "~/.envr/config.json")`,
)
write_borderless_table(w, &tbl)
fmt.wprintf( fmt.wprintf(
w, w,
"\n" + `Use "%senvr%s [command] --help" for more information about a command.`,
COLOR_HEADINGS + COLOR_FLAGS,
"Flags:" + ANSI_RESET,
ANSI_RESET +
"\n\n " +
COLOR_FLAGS +
"-h, --help" +
ANSI_RESET +
" help for envr\n" +
COLOR_FLAGS +
` -c, --config-file` +
ANSI_RESET +
` <path> config file (default "~/.envr/config.json")
Use "` +
COLOR_FLAGS +
"envr" +
ANSI_RESET +
` [command] --help" for more information about a command.
`,
flush = false, flush = false,
) )
} }

View File

@@ -1,5 +1,7 @@
package main package main
import "core:fmt"
import "core:io"
import "core:text/table" import "core:text/table"
import "core:unicode/utf8" import "core:unicode/utf8"
@@ -34,3 +36,44 @@ ansi_aware_width :: proc(str: string) -> int {
return width return width
} }
write_borderless_table :: proc(w: io.Writer, t: ^table.Table) {
table.build(t, ansi_aware_width)
write_table_separator :: proc(w: io.Writer, tbl: ^table.Table) {
io.write_byte(w, '\n')
}
if t.caption != "" {
table.write_text_align(
w,
fmt.tprintf("%s%s%s", COLOR_HEADINGS, t.caption, ANSI_RESET),
.Left,
0, //t.lpad,
0, //t.rpad,
t.tblw + t.nr_cols - 1 - ansi_aware_width(t.caption) - t.lpad - t.rpad,
)
io.write_byte(w, '\n')
}
write_table_separator(w, t)
for row in 0 ..< t.nr_rows {
for col in 0 ..< t.nr_cols {
table.write_table_cell(w, t, row, col)
}
io.write_byte(w, '\n')
if t.has_header_row && row == table.header_row(t) {
write_table_separator(w, t)
}
}
write_table_separator(w, t)
}
table_reset :: proc(t: ^table.Table) {
clear(&t.cells)
clear(&t.colw)
t.caption = ""
t.tblw = 0
t.nr_cols = 0
t.nr_rows = 0
}