Skip to content

Commit

Permalink
control notation position of progress bar tracker value (#180)
Browse files Browse the repository at this point in the history
* Added support for notation position of progress bar tracker value
* linter fix for units postion

Co-authored-by: Evgeniy Belousov <evgeny.belousov@orbitsoft.com>
  • Loading branch information
ferryvg and Evgeniy Belousov committed Aug 9, 2021
1 parent 95681a3 commit f0fe8f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
30 changes: 27 additions & 3 deletions progress/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ import (
"fmt"
)

// UnitsNotationPosition determines units position relative of tracker value
// Defaults is UnitsNotationPositionBefore
type UnitsNotationPosition int

// Supported unit positions relative to tracker value
const (
UnitsNotationPositionBefore UnitsNotationPosition = iota
UnitsNotationPositionAfter
)

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

var (
Expand Down Expand Up @@ -52,10 +63,23 @@ var (

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

formattedValue := formatter(value)

switch tu.NotationPosition {
case UnitsNotationPositionBefore:
return tu.Notation + formattedValue

case UnitsNotationPositionAfter:
return formattedValue + tu.Notation

default:
return tu.Notation + formattedValue
}
return tu.Notation + tu.Formatter(value)
}

// FormatBytes formats the given value as a "Byte".
Expand Down
8 changes: 8 additions & 0 deletions progress/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ func TestUnits_Sprint(t *testing.T) {
customUnits := Units{Notation: "#"}
assert.Equal(t, "#1.50K", customUnits.Sprint(1500))
}

func TestUnits_NotationPosition(t *testing.T) {
afterUnits := Units{Notation: " ₽", NotationPosition: UnitsNotationPositionAfter}
assert.Equal(t, "1.50K ₽", afterUnits.Sprint(1500))

unknownNotationPosition := Units{Notation: "* ", NotationPosition: UnitsNotationPosition(999)}
assert.Equal(t, "* 1.50K", unknownNotationPosition.Sprint(1500))
}

0 comments on commit f0fe8f9

Please sign in to comment.