mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 18:04:17 +02:00
add type caster to table, convert cells automatically, users use any
This commit is contained in:
@@ -32,8 +32,10 @@ import (
|
||||
type Table struct {
|
||||
Mode string // tsv, json, yaml
|
||||
Headers []string
|
||||
Entries [][]string
|
||||
Entries [][]any
|
||||
|
||||
rows [][]string // representation used for printing
|
||||
processed bool
|
||||
lenHeaders []int
|
||||
alignInts bool
|
||||
maxwidth int
|
||||
@@ -43,7 +45,7 @@ func NewTable(conf *cfg.Config, columns, rows int) *Table {
|
||||
table := Table{Mode: conf.Output, maxwidth: cfg.GetTermWidth()}
|
||||
|
||||
table.Headers = make([]string, columns)
|
||||
table.Entries = make([][]string, rows)
|
||||
table.Entries = make([][]any, rows)
|
||||
table.lenHeaders = make([]int, columns)
|
||||
table.alignInts = conf.AlignInts
|
||||
|
||||
@@ -59,7 +61,7 @@ func NewTableEmpty(conf *cfg.Config) *Table {
|
||||
func (table *Table) WithHeaders(headers ...string) *Table {
|
||||
count := len(headers)
|
||||
|
||||
table.Entries = [][]string{}
|
||||
table.Entries = [][]any{}
|
||||
table.lenHeaders = make([]int, count)
|
||||
table.Headers = make([]string, count)
|
||||
|
||||
@@ -84,11 +86,11 @@ var (
|
||||
)
|
||||
|
||||
// needed for json and yaml output
|
||||
func (data *Table) toMap() []map[string]string {
|
||||
raw := make([]map[string]string, len(data.Entries))
|
||||
func (data *Table) toMap() []map[string]any {
|
||||
raw := make([]map[string]any, len(data.Entries))
|
||||
|
||||
for idx, entries := range data.Entries {
|
||||
raw[idx] = make(map[string]string, len(data.Headers))
|
||||
raw[idx] = make(map[string]any, len(data.Headers))
|
||||
for eidx, entry := range entries {
|
||||
raw[idx][data.Headers[eidx]] = entry
|
||||
}
|
||||
@@ -124,27 +126,8 @@ func (data *Table) PrintJSON() error {
|
||||
}
|
||||
|
||||
func (data *Table) PrintTSV() error {
|
||||
// determine lenght's
|
||||
for idx, head := range data.Headers {
|
||||
data.lenHeaders[idx] = visibleLen(head)
|
||||
}
|
||||
|
||||
for _, entries := range data.Entries {
|
||||
currentWidth := 0
|
||||
for idx, entry := range entries {
|
||||
length := visibleLen(entry)
|
||||
|
||||
if data.lenHeaders[idx] < length {
|
||||
if length > currentWidth+data.maxwidth {
|
||||
data.lenHeaders[idx] = data.maxwidth - currentWidth
|
||||
} else {
|
||||
data.lenHeaders[idx] = length
|
||||
}
|
||||
}
|
||||
|
||||
currentWidth += data.lenHeaders[idx]
|
||||
}
|
||||
}
|
||||
// length's, convert cell types
|
||||
data.preprocessRows()
|
||||
|
||||
// output headers
|
||||
for idx, header := range data.Headers {
|
||||
@@ -161,7 +144,7 @@ func (data *Table) PrintTSV() error {
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
for _, entries := range data.Entries {
|
||||
for _, entries := range data.rows {
|
||||
currentWidth := 0
|
||||
|
||||
for idx, entry := range entries {
|
||||
@@ -211,8 +194,10 @@ func (data *Table) Sort() {
|
||||
return
|
||||
}
|
||||
|
||||
sort.Slice(data.Entries, func(i, j int) bool {
|
||||
return data.Entries[i][0] < data.Entries[j][0]
|
||||
data.preprocessRows()
|
||||
|
||||
sort.Slice(data.rows, func(i, j int) bool {
|
||||
return data.rows[i][0] < data.rows[j][0]
|
||||
})
|
||||
}
|
||||
|
||||
@@ -227,7 +212,7 @@ func (data *Table) Addheaders(headers ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (data *Table) AddRow(fields ...string) {
|
||||
func (data *Table) AddRow(fields ...any) {
|
||||
data.Entries = append(data.Entries, fields)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user