mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 11:44:18 +02:00
add CSV output support
This commit is contained in:
@@ -32,6 +32,7 @@ import (
|
|||||||
type Table struct {
|
type Table struct {
|
||||||
Mode string // tsv, json, yaml
|
Mode string // tsv, json, yaml
|
||||||
Headers []string
|
Headers []string
|
||||||
|
RawHeaders []string
|
||||||
Entries [][]any
|
Entries [][]any
|
||||||
|
|
||||||
rows [][]string // representation used for printing
|
rows [][]string // representation used for printing
|
||||||
@@ -45,6 +46,7 @@ func NewTable(conf *cfg.Config, columns, rows int) *Table {
|
|||||||
table := Table{Mode: conf.Output, maxwidth: cfg.GetTermWidth()}
|
table := Table{Mode: conf.Output, maxwidth: cfg.GetTermWidth()}
|
||||||
|
|
||||||
table.Headers = make([]string, columns)
|
table.Headers = make([]string, columns)
|
||||||
|
table.RawHeaders = make([]string, columns)
|
||||||
table.Entries = make([][]any, rows)
|
table.Entries = make([][]any, rows)
|
||||||
table.lenHeaders = make([]int, columns)
|
table.lenHeaders = make([]int, columns)
|
||||||
table.alignInts = conf.AlignInts
|
table.alignInts = conf.AlignInts
|
||||||
@@ -65,6 +67,7 @@ func (table *Table) WithHeaders(headers ...string) *Table {
|
|||||||
table.Entries = [][]any{}
|
table.Entries = [][]any{}
|
||||||
table.lenHeaders = make([]int, count)
|
table.lenHeaders = make([]int, count)
|
||||||
table.Headers = make([]string, count)
|
table.Headers = make([]string, count)
|
||||||
|
table.RawHeaders = make([]string, count)
|
||||||
|
|
||||||
table.Addheaders(headers...)
|
table.Addheaders(headers...)
|
||||||
|
|
||||||
@@ -77,6 +80,8 @@ func (table *Table) Print() error {
|
|||||||
return table.PrintJSON()
|
return table.PrintJSON()
|
||||||
case "yaml":
|
case "yaml":
|
||||||
return table.PrintYAML()
|
return table.PrintYAML()
|
||||||
|
case "csv":
|
||||||
|
return table.PrintCSV()
|
||||||
default:
|
default:
|
||||||
return table.PrintTSV()
|
return table.PrintTSV()
|
||||||
}
|
}
|
||||||
@@ -178,6 +183,28 @@ func (table *Table) PrintTSV() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (table *Table) PrintCSV() error {
|
||||||
|
table.preprocessRows()
|
||||||
|
|
||||||
|
fmt.Println(strings.Join(table.RawHeaders, ","))
|
||||||
|
|
||||||
|
for _, entries := range table.rows {
|
||||||
|
row := make([]string, len(entries))
|
||||||
|
|
||||||
|
for idx, entry := range entries {
|
||||||
|
if strings.Contains(entry, " ") || strings.Contains(entry, ",") {
|
||||||
|
row[idx] = `"` + entry + `"`
|
||||||
|
} else {
|
||||||
|
row[idx] = entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(strings.Join(row, ","))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (table *Table) Sort() {
|
func (table *Table) Sort() {
|
||||||
// sanity checks
|
// sanity checks
|
||||||
if len(table.Entries) == 0 {
|
if len(table.Entries) == 0 {
|
||||||
@@ -199,6 +226,8 @@ func (table *Table) Addheaders(headers ...string) {
|
|||||||
default:
|
default:
|
||||||
table.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
|
table.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table.RawHeaders[idx] = header
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user