Skip to content

Commit

Permalink
table: re-organize some tests, reduce cognitive complexity (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Nov 7, 2022
1 parent 711c5ea commit 398d30f
Show file tree
Hide file tree
Showing 6 changed files with 643 additions and 625 deletions.
26 changes: 13 additions & 13 deletions progress/render.go
Expand Up @@ -377,17 +377,6 @@ func (p *Progress) renderTrackerStatsSpeed(out *strings.Builder, t *Tracker, hin
}

speedPrecision := p.style.Options.SpeedPrecision
writeSpeed := func(speed string) {
if p.style.Options.SpeedPosition == PositionRight {
out.WriteString("; ")
}
out.WriteString(p.style.Colors.Speed.Sprint(speed))
out.WriteString(p.style.Options.SpeedSuffix)
if p.style.Options.SpeedPosition == PositionLeft {
out.WriteString("; ")
}
}

if hint.isOverallTracker {
speed := float64(0)

Expand All @@ -398,16 +387,27 @@ func (p *Progress) renderTrackerStatsSpeed(out *strings.Builder, t *Tracker, hin
p.trackersActiveMutex.RUnlock()

if speed > 0 {
writeSpeed(p.style.Options.SpeedOverallFormatter(int64(speed)))
p.renderTrackerStatsSpeedInternal(out, p.style.Options.SpeedOverallFormatter(int64(speed)))
}
} else {
timeTaken := time.Since(t.timeStart)
if timeTakenRounded := timeTaken.Round(speedPrecision); timeTakenRounded > speedPrecision {
writeSpeed(t.Units.Sprint(int64(float64(t.Value()) / timeTakenRounded.Seconds())))
p.renderTrackerStatsSpeedInternal(out, t.Units.Sprint(int64(float64(t.Value())/timeTakenRounded.Seconds())))
}
}
}

func (p *Progress) renderTrackerStatsSpeedInternal(out *strings.Builder, speed string) {
if p.style.Options.SpeedPosition == PositionRight {
out.WriteString("; ")
}
out.WriteString(p.style.Colors.Speed.Sprint(speed))
out.WriteString(p.style.Options.SpeedSuffix)
if p.style.Options.SpeedPosition == PositionLeft {
out.WriteString("; ")
}
}

func (p *Progress) renderTrackerStatsTime(outStats *strings.Builder, t *Tracker, hint renderHint) {
var td, tp time.Duration
if t.IsDone() {
Expand Down
6 changes: 5 additions & 1 deletion table/README.md
Expand Up @@ -37,7 +37,6 @@ Pretty-print tables into ASCII/Unicode strings.
- HTML Table (with custom CSS Class)
- Markdown Table


```
+---------------------------------------------------------------------+
| Game of Thrones +
Expand All @@ -57,6 +56,11 @@ A demonstration of all the capabilities can be found here:

If you want very specific examples, read ahead.

**Hint**: I've tried to ensure that almost all supported use-cases are covered
by unit-tests and that they print the table rendered. Run
`go test -v github.com/jedib0t/go-pretty/v6/table` to see the test outputs and
help you figure out how to do something.

# Examples

All the examples below are going to start with the following block, although
Expand Down
53 changes: 53 additions & 0 deletions table/render_bidi_test.go
@@ -0,0 +1,53 @@
package table

import (
"testing"

"github.com/jedib0t/go-pretty/v6/text"
)

func TestTable_Render_BiDiText(t *testing.T) {
table := Table{}
table.AppendHeader(Row{"תאריך", "סכום", "מחלקה", "תגים"})
table.AppendRow(Row{"2020-01-01", 5.0, "מחלקה1", []string{"תג1", "תג2"}})
table.AppendRow(Row{"2021-02-01", 5.0, "מחלקה1", []string{"תג1"}})
table.AppendRow(Row{"2022-03-01", 5.0, "מחלקה2", []string{"תג1"}})
table.AppendFooter(Row{"סהכ", 30})
table.SetAutoIndex(true)

//table.Style().Format.Direction = text.Default
compareOutput(t, table.Render(), `
+---+------------+------+--------+-----------+
| | תאריך | סכום | מחלקה | תגים |
+---+------------+------+--------+-----------+
| 1 | 2020-01-01 | 5 | מחלקה1 | [תג1 תג2] |
| 2 | 2021-02-01 | 5 | מחלקה1 | [תג1] |
| 3 | 2022-03-01 | 5 | מחלקה2 | [תג1] |
+---+------------+------+--------+-----------+
| | סהכ | 30 | | |
+---+------------+------+--------+-----------+`)

table.Style().Format.Direction = text.LeftToRight
compareOutput(t, table.Render(), `
‪+---+------------+------+--------+-----------+
‪| | ‪תאריך | ‪סכום | ‪מחלקה | ‪תגים |
‪+---+------------+------+--------+-----------+
‪| 1 | ‪2020-01-01 | ‪5 | ‪מחלקה1 | ‪[תג1 תג2] |
‪| 2 | ‪2021-02-01 | ‪5 | ‪מחלקה1 | ‪[תג1] |
‪| 3 | ‪2022-03-01 | ‪5 | ‪מחלקה2 | ‪[תג1] |
‪+---+------------+------+--------+-----------+
‪| | ‪סהכ | ‪30 | | |
‪+---+------------+------+--------+-----------+`)

table.Style().Format.Direction = text.RightToLeft
compareOutput(t, table.Render(), `
‫+---+------------+------+--------+-----------+
‫| | ‫תאריך | ‫סכום | ‫מחלקה | ‫תגים |
‫+---+------------+------+--------+-----------+
‫| 1 | ‫2020-01-01 | ‫5 | ‫מחלקה1 | ‫[תג1 תג2] |
‫| 2 | ‫2021-02-01 | ‫5 | ‫מחלקה1 | ‫[תג1] |
‫| 3 | ‫2022-03-01 | ‫5 | ‫מחלקה2 | ‫[תג1] |
‫+---+------------+------+--------+-----------+
‫| | ‫סהכ | ‫30 | | |
‫+---+------------+------+--------+-----------+`)
}

0 comments on commit 398d30f

Please sign in to comment.