2026-07-07 07:29:03 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2026 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
package printer
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func any2string(in any) string {
|
2026-07-07 23:46:43 +02:00
|
|
|
//nolint:gocritic
|
2026-07-07 07:29:03 +02:00
|
|
|
switch val := in.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
return val
|
|
|
|
|
case bool:
|
|
|
|
|
return strconv.FormatBool(val)
|
|
|
|
|
case int64:
|
|
|
|
|
return strconv.FormatInt(val, 10)
|
|
|
|
|
case int:
|
|
|
|
|
return strconv.Itoa(val)
|
|
|
|
|
case float64:
|
|
|
|
|
return fmt.Sprintf("%.2f", val)
|
2026-07-08 11:47:07 +02:00
|
|
|
case float32:
|
|
|
|
|
return fmt.Sprintf("%.2f", val)
|
2026-07-07 07:29:03 +02:00
|
|
|
case []string:
|
|
|
|
|
return strings.Join(val, ",")
|
|
|
|
|
case ByteSize:
|
|
|
|
|
return val.String()
|
|
|
|
|
case time.Time:
|
|
|
|
|
return val.Format("2006-01-02 15:04:05")
|
2026-07-08 11:47:07 +02:00
|
|
|
case time.Duration:
|
|
|
|
|
return val.String()
|
2026-07-07 07:29:03 +02:00
|
|
|
case []byte:
|
|
|
|
|
return string(val)
|
|
|
|
|
case nil:
|
|
|
|
|
return "null"
|
2026-07-07 23:46:43 +02:00
|
|
|
case types.Percentage, types.DateTime:
|
|
|
|
|
return val.(string)
|
2026-07-07 07:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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] = any2string(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
|
2026-07-07 23:46:43 +02:00
|
|
|
|
2026-07-07 07:29:03 +02:00
|
|
|
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
|
|
|
|
|
}
|