some refactoring, fix stringer func

This commit is contained in:
2026-08-08 22:39:37 +02:00
parent a052c70baa
commit b49153bd38
2 changed files with 64 additions and 49 deletions

View File

@@ -291,7 +291,7 @@ func (table *Table) AddRowLate(fields ...any) {
row := make([]string, len(fields))
for idx, field := range fields {
row[idx] = any2string(field)
row[idx] = stringer(field)
}
table.rows = append(table.rows, row)
@@ -311,9 +311,47 @@ func (table *Table) toMap() []map[string]any {
return raw
}
// return the length of a string but only visible chars, w/o ansi color escapes
func visibleLen(word string) int {
return len(ansiCtrlSeq.ReplaceAllLiteralString(word, ""))
func (data *Table) preprocessRows() {
if data.processed {
// only do it once
return
}
// convert entries to strings
data.rows = make([][]string, len(data.Entries))
for rowidx, entries := range data.Entries {
data.rows[rowidx] = make([]string, len(entries))
for colidx, entry := range data.Entries[rowidx] {
data.rows[rowidx][colidx] = stringer(entry)
}
}
// determine header lenght's
for idx, head := range data.Headers {
data.lenHeaders[idx] = visibleLen(head)
}
// determine max width per column
for _, entries := range data.rows {
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]
}
}
data.processed = true
}
func isInt(num string) bool {