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

@@ -1,3 +1,5 @@
package printer
/* /*
Copyright © 2026 Thomas von Dein Copyright © 2026 Thomas von Dein
@@ -14,7 +16,6 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package printer
import ( import (
"fmt" "fmt"
@@ -25,7 +26,9 @@ import (
"github.com/elastic/go-elasticsearch/v9/typedapi/types" "github.com/elastic/go-elasticsearch/v9/typedapi/types"
) )
func any2string(in any) string { // stringer returns the string representation of different types of
// values
func stringer(in any) string {
//nolint:gocritic //nolint:gocritic
switch val := in.(type) { switch val := in.(type) {
case string: case string:
@@ -54,52 +57,26 @@ func any2string(in any) string {
return string(val) return string(val)
case nil: case nil:
return "null" return "null"
case types.Percentage, types.DateTime: case *types.Float64:
return val.(string) // ignore err here, because types.Float64.MarshalJSON() never returns one
} f, _ := val.MarshalJSON()
return string(f)
return "" default:
} // Caution: this may cause a panic if the [unknown] type does
// not implement fmt.Stringer. In this case add another case
func (data *Table) preprocessRows() { // to the type switch for it above.
if data.processed { return in.(fmt.Stringer).String()
// 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] = any2string(entry)
} }
} }
// determine header lenght's // visibleLen returns the length of a string but only visible chars,
for idx, head := range data.Headers { // w/o ansi color escapes
data.lenHeaders[idx] = visibleLen(head) func visibleLen(word string) int {
if !strings.Contains(word, "\x1b") {
// no ansi escape in there, use faster method
return len(word)
} }
// determine max width per column // contains escapes, need to clean up before counting
for _, entries := range data.rows { return len(ansiCtrlSeq.ReplaceAllLiteralString(word, ""))
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
} }

View File

@@ -291,7 +291,7 @@ func (table *Table) AddRowLate(fields ...any) {
row := make([]string, len(fields)) row := make([]string, len(fields))
for idx, field := range fields { for idx, field := range fields {
row[idx] = any2string(field) row[idx] = stringer(field)
} }
table.rows = append(table.rows, row) table.rows = append(table.rows, row)
@@ -311,9 +311,47 @@ func (table *Table) toMap() []map[string]any {
return raw return raw
} }
// return the length of a string but only visible chars, w/o ansi color escapes func (data *Table) preprocessRows() {
func visibleLen(word string) int { if data.processed {
return len(ansiCtrlSeq.ReplaceAllLiteralString(word, "")) // 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 { func isInt(num string) bool {