mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 10:04:18 +02:00
Compare commits
2 Commits
35bd685cfe
...
b49153bd38
| Author | SHA1 | Date | |
|---|---|---|---|
| b49153bd38 | |||
| a052c70baa |
@@ -291,7 +291,7 @@ func gatherClusterStats(clusterstats *clusterstats.Response, table *printer.Tabl
|
|||||||
{"indices", clusterstats.Indices.Count},
|
{"indices", clusterstats.Indices.Count},
|
||||||
{"Docs", clusterstats.Indices.Docs.Count},
|
{"Docs", clusterstats.Indices.Docs.Count},
|
||||||
{"Total Size", printer.Bytes(clusterstats.Indices.Docs.TotalSizeInBytes)},
|
{"Total Size", printer.Bytes(clusterstats.Indices.Docs.TotalSizeInBytes)},
|
||||||
{"Total Queries", "%d", querycount},
|
{"Total Queries", querycount},
|
||||||
{"Shards Primaries", clusterstats.Indices.Shards.Primaries},
|
{"Shards Primaries", clusterstats.Indices.Shards.Primaries},
|
||||||
{"Shards Total", clusterstats.Indices.Shards.Total},
|
{"Shards Total", clusterstats.Indices.Shards.Total},
|
||||||
{"Storage", fmt.Sprintf(
|
{"Storage", fmt.Sprintf(
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user