Skip to content

Commit

Permalink
fix issues/smells identified by sonar (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Aug 20, 2021
1 parent eaf4190 commit c7805ed
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 298 deletions.
64 changes: 36 additions & 28 deletions cmd/demo-table/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ import (
"github.com/jedib0t/go-pretty/v6/text"
)

var (
colTitleIndex = "#"
colTitleFirstName = "First Name"
colTitleLastName = "Last Name"
colTitleSalary = "Salary"
rowHeader = table.Row{colTitleIndex, colTitleFirstName, colTitleLastName, colTitleSalary}
row1 = table.Row{1, "Arya", "Stark", 3000}
row2 = table.Row{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"}
row3 = table.Row{300, "Tyrion", "Lannister", 5000}
rowFooter = table.Row{"", "", "Total", 10000}
)

func demoTableColors() {
tw := table.NewWriter()
tw.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
tw.AppendRows([]table.Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
tw.AppendFooter(table.Row{"", "", "Total", 10000})
tw.AppendHeader(rowHeader)
tw.AppendRows([]table.Row{row1, row2, row3})
tw.AppendFooter(rowFooter)
tw.SetIndexColumn(1)
tw.SetTitle("Game Of Thrones")

Expand Down Expand Up @@ -99,7 +107,7 @@ func demoTableFeatures() {
//+---+-----+--------+-----------+------+-----------------------------+
//Table with Auto-Indexing.
//
t.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
t.AppendHeader(rowHeader)
t.SetCaption("Table with Auto-Indexing (columns-only).\n")
fmt.Println(t.Render())
//+---+-----+------------+-----------+--------+-----------------------------+
Expand Down Expand Up @@ -151,7 +159,7 @@ func demoTableFeatures() {
// go right and everything else left. but what if you want the first name to
// go right too? and the last column to be "justified"?
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "First Name", Align: text.AlignRight},
{Name: colTitleFirstName, Align: text.AlignRight},
// the 5th column does not have a title, so use the column number as the
// identifier for the column
{Number: 5, Align: text.AlignJustify},
Expand Down Expand Up @@ -202,9 +210,9 @@ func demoTableFeatures() {
//
// time to Align/VAlign the columns...
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "First Name", Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: "Last Name", VAlign: text.VAlignBottom},
{Name: "Salary", Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: colTitleFirstName, Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: colTitleLastName, VAlign: text.VAlignBottom},
{Name: colTitleSalary, Align: text.AlignRight, VAlign: text.VAlignMiddle},
// the 5th column does not have a title, so use the column number
{Number: 5, Align: text.AlignJustify},
})
Expand All @@ -229,9 +237,9 @@ func demoTableFeatures() {
//
// changed your mind about AlignJustify?
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "First Name", Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: "Last Name", VAlign: text.VAlignBottom},
{Name: "Salary", Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: colTitleFirstName, Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Name: colTitleLastName, VAlign: text.VAlignBottom},
{Name: colTitleSalary, Align: text.AlignRight, VAlign: text.VAlignMiddle},
{Number: 5, Align: text.AlignCenter},
})
t.SetCaption("Table with a Multi-line Row with VAlign and changed Align.\n")
Expand Down Expand Up @@ -259,10 +267,10 @@ func demoTableFeatures() {
// custom separators?
//==========================================================================
t.ResetRows()
t.AppendRow(table.Row{1, "Arya", "Stark", 3000})
t.AppendRow(table.Row{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"})
t.AppendRow(row1)
t.AppendRow(row2)
t.AppendSeparator()
t.AppendRow([]interface{}{300, "Tyrion", "Lannister", 5000})
t.AppendRow(row3)
t.SetCaption("Simple Table with 3 Rows and a Separator in-between.\n")
fmt.Println(t.Render())
//+-----+--------+-----------+------+-----------------------------+
Expand All @@ -279,9 +287,9 @@ func demoTableFeatures() {
//==========================================================================
t.ResetRows()
t.SetColumnConfigs(nil)
t.AppendRow(table.Row{1, "Arya", "Stark", 3000})
t.AppendRow(table.Row{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"})
t.AppendRow([]interface{}{300, "Tyrion", "Lannister", 5000})
t.AppendRow(row1)
t.AppendRow(row2)
t.AppendRow(row3)
t.SetCaption("Starting afresh with a Simple Table again.\n")
fmt.Println(t.Render())
//+-----+------------+-----------+--------+-----------------------------+
Expand Down Expand Up @@ -364,9 +372,9 @@ func demoTableFeatures() {
// But I want to see all the data!
//==========================================================================
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "First Name", WidthMax: 6},
{Name: "Last Name", WidthMax: 9},
{Name: "Salary", WidthMax: 6},
{Name: colTitleFirstName, WidthMax: 6},
{Name: colTitleLastName, WidthMax: 9},
{Name: colTitleSalary, WidthMax: 6},
{Number: 5, WidthMax: 10},
})
t.SetCaption("Table on a diet.\n")
Expand Down Expand Up @@ -475,10 +483,10 @@ func demoTableFeatures() {
colorBOnW := text.Colors{text.BgWhite, text.FgBlack}
// set colors using Colors/ColorsHeader/ColorsFooter
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "#", Colors: text.Colors{text.FgYellow}, ColorsHeader: colorBOnW},
{Name: "First Name", Colors: text.Colors{text.FgHiRed}, ColorsHeader: colorBOnW},
{Name: "Last Name", Colors: text.Colors{text.FgHiRed}, ColorsHeader: colorBOnW, ColorsFooter: colorBOnW},
{Name: "Salary", Colors: text.Colors{text.FgGreen}, ColorsHeader: colorBOnW, ColorsFooter: colorBOnW},
{Name: colTitleIndex, Colors: text.Colors{text.FgYellow}, ColorsHeader: colorBOnW},
{Name: colTitleFirstName, Colors: text.Colors{text.FgHiRed}, ColorsHeader: colorBOnW},
{Name: colTitleLastName, Colors: text.Colors{text.FgHiRed}, ColorsHeader: colorBOnW, ColorsFooter: colorBOnW},
{Name: colTitleSalary, Colors: text.Colors{text.FgGreen}, ColorsHeader: colorBOnW, ColorsFooter: colorBOnW},
{Number: 5, Colors: text.Colors{text.FgCyan}, ColorsHeader: colorBOnW},
})
t.SetCaption("Table with Colors.\n")
Expand Down
38 changes: 21 additions & 17 deletions list/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (l *List) renderItem(out *strings.Builder, idx int, item *listItem, hint re

// render the prefix or the leading text before the actual item
l.renderItemBulletPrefix(out, idx, item.Level, lineIdx, hint)
l.renderItemBullet(out, idx, item.Level, lineIdx, hint)
l.renderItemBullet(out, lineIdx, hint)

// render the actual item
out.WriteString(lineStr)
}
}

func (l *List) renderItemBullet(out *strings.Builder, itemIdx int, itemLevel int, lineIdx int, hint renderHint) {
func (l *List) renderItemBullet(out *strings.Builder, lineIdx int, hint renderHint) {
if lineIdx > 0 {
// multi-line item.Text
if hint.isLastItem {
Expand All @@ -73,24 +73,28 @@ func (l *List) renderItemBullet(out *strings.Builder, itemIdx int, itemLevel int
out.WriteString(l.style.CharItemVertical)
}
} else {
// single-line item.Text (or first line of a multi-line item.Text)
if hint.isOnlyItem {
if hint.isTopItem {
out.WriteString(l.style.CharItemSingle)
} else {
out.WriteString(l.style.CharItemBottom)
}
} else if hint.isTopItem {
out.WriteString(l.style.CharItemTop)
} else if hint.isFirstItem {
out.WriteString(l.style.CharItemFirst)
} else if hint.isBottomItem || hint.isLastItem {
out.WriteString(l.style.CharItemBottom)
l.renderItemBulletSingleLine(out, lineIdx, hint)
}
}

func (l *List) renderItemBulletSingleLine(out *strings.Builder, lineIdx int, hint renderHint) {
// single-line item.Text (or first line of a multi-line item.Text)
if hint.isOnlyItem {
if hint.isTopItem {
out.WriteString(l.style.CharItemSingle)
} else {
out.WriteString(l.style.CharItemMiddle)
out.WriteString(l.style.CharItemBottom)
}
out.WriteRune(' ')
} else if hint.isTopItem {
out.WriteString(l.style.CharItemTop)
} else if hint.isFirstItem {
out.WriteString(l.style.CharItemFirst)
} else if hint.isBottomItem || hint.isLastItem {
out.WriteString(l.style.CharItemBottom)
} else {
out.WriteString(l.style.CharItemMiddle)
}
out.WriteRune(' ')
}

func (l *List) renderItemBulletPrefix(out *strings.Builder, itemIdx int, itemLevel int, lineIdx int, hint renderHint) {
Expand Down
38 changes: 21 additions & 17 deletions progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,34 @@ func (p *Progress) renderTrackerStats(out *strings.Builder, t *Tracker, hint ren
outStats.WriteString(" in ")
}
if !hint.hideTime {
var td, tp time.Duration
if t.IsDone() {
td = t.timeStop.Sub(t.timeStart)
} else {
td = time.Since(t.timeStart)
}
if hint.isOverallTracker {
tp = p.style.Options.TimeOverallPrecision
} else if t.IsDone() {
tp = p.style.Options.TimeDonePrecision
} else {
tp = p.style.Options.TimeInProgressPrecision
}
outStats.WriteString(p.style.Colors.Time.Sprint(td.Round(tp)))
if p.showETA || hint.isOverallTracker {
p.renderTrackerStatsETA(&outStats, t, hint)
}
p.renderTrackerStatsTime(&outStats, t, hint)
}
outStats.WriteRune(']')

out.WriteString(p.style.Colors.Stats.Sprint(outStats.String()))
}
}

func (p *Progress) renderTrackerStatsTime(outStats *strings.Builder, t *Tracker, hint renderHint) {
var td, tp time.Duration
if t.IsDone() {
td = t.timeStop.Sub(t.timeStart)
} else {
td = time.Since(t.timeStart)
}
if hint.isOverallTracker {
tp = p.style.Options.TimeOverallPrecision
} else if t.IsDone() {
tp = p.style.Options.TimeDonePrecision
} else {
tp = p.style.Options.TimeInProgressPrecision
}
outStats.WriteString(p.style.Colors.Time.Sprint(td.Round(tp)))
if p.showETA || hint.isOverallTracker {
p.renderTrackerStatsETA(outStats, t, hint)
}
}

func (p *Progress) renderTrackerStatsETA(out *strings.Builder, t *Tracker, hint renderHint) {
tpETA := p.style.Options.ETAPrecision
if eta := t.ETA().Round(tpETA); hint.isOverallTracker || eta > tpETA {
Expand Down
64 changes: 34 additions & 30 deletions progress/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

// UnitsNotationPosition determines units position relative of tracker value.
// UnitsNotationPosition determines notation position relative to unit value.
type UnitsNotationPosition int

// Supported unit positions relative to tracker value;
Expand All @@ -16,68 +16,72 @@ const (

// Units defines the "type" of the value being tracked by the Tracker.
type Units struct {
Formatter func(value int64) string // default: FormatNumber
Notation string
NotationPosition UnitsNotationPosition
Formatter func(value int64) string
NotationPosition UnitsNotationPosition // default: UnitsNotationPositionBefore
}

// Sprint prints the value as defined by the Units.
func (tu Units) Sprint(value int64) string {
formatter := tu.Formatter
if formatter == nil {
formatter = FormatNumber
}

formattedValue := formatter(value)
switch tu.NotationPosition {
case UnitsNotationPositionAfter:
return formattedValue + tu.Notation
default: // UnitsNotationPositionBefore
return tu.Notation + formattedValue
}
}

var (
// UnitsDefault doesn't define any units. The value will be treated as any
// other number.
UnitsDefault = Units{
Notation: "",
Formatter: FormatNumber,
Notation: "",
NotationPosition: UnitsNotationPositionBefore,
Formatter: FormatNumber,
}

// UnitsBytes defines the value as a storage unit. Values will be converted
// and printed in one of these forms: B, KB, MB, GB, TB, PB
UnitsBytes = Units{
Notation: "",
Formatter: FormatBytes,
Notation: "",
NotationPosition: UnitsNotationPositionBefore,
Formatter: FormatBytes,
}

// UnitsCurrencyDollar defines the value as a Dollar amount. Values will be
// converted and printed in one of these forms: $x.yz, $x.yzK, $x.yzM,
// $x.yzB, $x.yzT
UnitsCurrencyDollar = Units{
Notation: "$",
Formatter: FormatNumber,
Notation: "$",
NotationPosition: UnitsNotationPositionBefore,
Formatter: FormatNumber,
}

// UnitsCurrencyEuro defines the value as a Euro amount. Values will be
// converted and printed in one of these forms: ₠x.yz, ₠x.yzK, ₠x.yzM,
// ₠x.yzB, ₠x.yzT
UnitsCurrencyEuro = Units{
Notation: "₠",
Formatter: FormatNumber,
Notation: "₠",
NotationPosition: UnitsNotationPositionBefore,
Formatter: FormatNumber,
}

// UnitsCurrencyPound defines the value as a Pound amount. Values will be
// converted and printed in one of these forms: £x.yz, £x.yzK, £x.yzM,
// £x.yzB, £x.yzT
UnitsCurrencyPound = Units{
Notation: "£",
Formatter: FormatNumber,
Notation: "£",
NotationPosition: UnitsNotationPositionBefore,
Formatter: FormatNumber,
}
)

// Sprint prints the value as defined by the Units.
func (tu Units) Sprint(value int64) string {
formatter := tu.Formatter
if formatter == nil {
formatter = FormatNumber
}

formattedValue := formatter(value)

switch tu.NotationPosition {
case UnitsNotationPositionAfter:
return formattedValue + tu.Notation
default: // UnitsNotationPositionBefore
return tu.Notation + formattedValue
}
}

// FormatBytes formats the given value as a "Byte".
func FormatBytes(value int64) string {
if value < 1000 {
Expand Down
Loading

0 comments on commit c7805ed

Please sign in to comment.