feat: Added --format, -f flag.

Allows printing data in tabular or json format.
This commit is contained in:
2026-06-25 17:34:30 -04:00
parent 6fa68d10b1
commit e74fc4f35a
7 changed files with 175 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import "core:fmt"
import "core:io"
import "core:os"
import "core:strings"
import "core:terminal"
import "core:text/table"
Command :: struct {
@@ -18,6 +19,11 @@ Command :: struct {
err: io.Writer,
}
Output_Format :: enum {
Table,
JSON,
}
CommandInfo :: struct {
name: string,
usage: string,
@@ -288,6 +294,11 @@ at before, restore your backup with:
COLOR_FLAGS + "-c, --config-file" + ANSI_RESET + " <path>",
`config file (default "~/.envr/config.json")`,
)
table.row(
&tbl,
COLOR_FLAGS + "-f, --format" + ANSI_RESET + " 'json'|'table'",
`the format of output data. (default 'table', unless piping)`,
)
write_borderless_table(w, &tbl)
fmt.wprintf(
@@ -303,6 +314,22 @@ has_flag :: proc(cmd: ^Command, name: string) -> bool {
return name in cmd.flags || name in cmd.bool_set
}
get_format :: proc(cmd: ^Command) -> Output_Format {
flags :: []string{"format", "f"}
for name in flags {
if val, ok := cmd.flags[name]; ok {
switch val {
case "json":
return .JSON
case "table":
return .Table
}
}
}
return terminal.is_terminal(os.stdout) ? .Table : .JSON
}
delete_command :: proc(cmd: ^Command) {
bufio.writer_flush(cmd.out_buf)
delete(cmd.args)