Skip to content

Commit

Permalink
Print small numbers more legibly in Table output
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Elsen committed Jun 12, 2024
1 parent 8d48cb1 commit 2810bd2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions pkg/formatting/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ func Count(val uint64) string {
valF /= 1000.0
count++
}
if valF == 0 {
return fmt.Sprintf("%.2f %s", valF, units[count])
}

return fmt.Sprintf("%.2f %s", valF, units[count])
}

// CountSmall is like Count, but formats numbers smaller than 1000 as integers
func CountSmall(val uint64) string {
func CountSmall(val uint64, align bool) string {
if val >= 1000 {
return Count(val)
}
return fmt.Sprintf("%d", val)
var pad string
if align {
pad = " "
}
return fmt.Sprintf("%d%s", val, pad)
}

// Size prints out size in a human-readable format (e.g. 10 MB)
Expand All @@ -69,20 +69,20 @@ func Size(size uint64) string {
sizeF /= 1024.0
count++
}
if sizeF == 0 {
return fmt.Sprintf("%.2f %s", sizeF, units[count])
}

return fmt.Sprintf("%.2f %s", sizeF, units[count])
}

// SizeSmall prints out size in a human-readable format (e.g. 10 MB) just like Size,
// but makes sure that sizes under 1024 Byte are formatted as integers
func SizeSmall(size uint64) string {
func SizeSmall(size uint64, align bool) string {
if size > 1024 {
return Size(size)
}
return fmt.Sprintf("%d %s", size, "B")
var pad string
if align {
pad = " "
}
return fmt.Sprintf("%d%s", size, pad)
}

// Duration prints out d in a human-readable duration format
Expand Down
20 changes: 10 additions & 10 deletions pkg/results/TablePrinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func NewTextFormatter() TextFormatter {

// Size prints out size in a human-readable format (e.g. 10 MB)
func (TextFormatter) Size(size uint64) string {
return formatting.Size(size)
return formatting.SizeSmall(size, true)
}

// Duration prints out d in a human-readable duration format
Expand All @@ -499,13 +499,13 @@ func (TextFormatter) Duration(d time.Duration) string {

// Count prints val in concise human-readable form (e.g. 1 K instead of 1000)
func (TextFormatter) Count(val uint64) string {
return formatting.Count(val)
return formatting.CountSmall(val, true)
}

// Float prints f rounded to two decimals
func (TextFormatter) Float(f float64) string {
if f == 0 {
return fmt.Sprintf("%.2f", f)
return fmt.Sprintf("%d", int(f))
}
return fmt.Sprintf("%.2f", f)
}
Expand Down Expand Up @@ -697,31 +697,31 @@ func (t *TextTablePrinter) Footer(ctx context.Context, result *Result) error {

result.Query.PrintFooter(t.footerWriter)
t.footerWriter.WriteEntry(queryStatsKey, "displayed top %s hits out of %s in %s",
formatting.CountSmall(uint64(result.Summary.Hits.Displayed)),
formatting.CountSmall(uint64(result.Summary.Hits.Total)),
formatting.CountSmall(uint64(result.Summary.Hits.Displayed), false),
formatting.CountSmall(uint64(result.Summary.Hits.Total), false),
textFormatter.Duration(result.Summary.Timings.QueryDuration),
)

if t.printQueryStats {
stats := result.Summary.Stats
// we leave the key empty on purpose since the displayed info constitutes query statistics
t.footerWriter.WriteEntry("Bytes loaded", "%s",
formatting.SizeSmall(stats.BytesLoaded),
formatting.SizeSmall(stats.BytesLoaded, false),
)
t.footerWriter.WriteEntry("Bytes decompressed", "%s",
formatting.SizeSmall(stats.BytesDecompressed),
formatting.SizeSmall(stats.BytesDecompressed, false),
)
t.footerWriter.WriteEntry("Blocks processed", "%s",
formatting.Count(stats.BlocksProcessed),
)
t.footerWriter.WriteEntry("Blocks corrupted", "%s",
formatting.CountSmall(stats.BlocksCorrupted),
formatting.CountSmall(stats.BlocksCorrupted, false),
)
t.footerWriter.WriteEntry("Directories processed", "%s",
formatting.CountSmall(stats.DirectoriesProcessed),
formatting.CountSmall(stats.DirectoriesProcessed, false),
)
t.footerWriter.WriteEntry("Workloads", "%s",
formatting.CountSmall(stats.Workloads),
formatting.CountSmall(stats.Workloads, false),
)
}

Expand Down

0 comments on commit 2810bd2

Please sign in to comment.