mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
feat: Added --format, -f flag.
Allows printing data in tabular or json format.
This commit is contained in:
27
cli.odin
27
cli.odin
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user