Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
674 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package progress | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/jedib0t/go-pretty/v6/text" | ||
) | ||
|
||
// IndeterminateIndicator defines the structure for the indicator to indicate | ||
// indeterminate progress. Ex.: <=> | ||
type IndeterminateIndicator struct { | ||
Position int | ||
Text string | ||
} | ||
|
||
// IndeterminateIndicatorGenerator returns an IndeterminateIndicator for cases | ||
// where the progress percentage cannot be calculated. Ex.: [........<=>....] | ||
type IndeterminateIndicatorGenerator func(maxLen int) IndeterminateIndicator | ||
|
||
// IndeterminateIndicatorMovingBackAndForth returns an instance of | ||
// IndeterminateIndicatorGenerator function that incrementally moves from the | ||
// left to right and back for each specified duration. If duration is 0, then | ||
// every single invocation moves the indicator. | ||
func IndeterminateIndicatorMovingBackAndForth(indicator string, duration time.Duration) IndeterminateIndicatorGenerator { | ||
var indeterminateIndicator *IndeterminateIndicator | ||
indicatorGenerator := indeterminateIndicatorMovingBackAndForth(indicator) | ||
lastRenderTime := time.Now() | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
currRenderTime := time.Now() | ||
if indeterminateIndicator == nil || duration == 0 || currRenderTime.Sub(lastRenderTime) > duration { | ||
tmpIndeterminateIndicator := indicatorGenerator(maxLen) | ||
indeterminateIndicator = &tmpIndeterminateIndicator | ||
lastRenderTime = currRenderTime | ||
} | ||
|
||
return *indeterminateIndicator | ||
} | ||
} | ||
|
||
// IndeterminateIndicatorMovingLeftToRight returns an instance of | ||
// IndeterminateIndicatorGenerator function that incrementally moves from the | ||
// left to right and starts from left again for each specified duration. If | ||
// duration is 0, then every single invocation moves the indicator. | ||
func IndeterminateIndicatorMovingLeftToRight(indicator string, duration time.Duration) IndeterminateIndicatorGenerator { | ||
var indeterminateIndicator *IndeterminateIndicator | ||
indicatorGenerator := indeterminateIndicatorMovingLeftToRight(indicator) | ||
lastRenderTime := time.Now() | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
currRenderTime := time.Now() | ||
if indeterminateIndicator == nil || duration == 0 || currRenderTime.Sub(lastRenderTime) > duration { | ||
tmpIndeterminateIndicator := indicatorGenerator(maxLen) | ||
indeterminateIndicator = &tmpIndeterminateIndicator | ||
lastRenderTime = currRenderTime | ||
} | ||
|
||
return *indeterminateIndicator | ||
} | ||
} | ||
|
||
// IndeterminateIndicatorMovingRightToLeft returns an instance of | ||
// IndeterminateIndicatorGenerator function that incrementally moves from the | ||
// right to left and starts from right again for each specified duration. If | ||
// duration is 0, then every single invocation moves the indicator. | ||
func IndeterminateIndicatorMovingRightToLeft(indicator string, duration time.Duration) IndeterminateIndicatorGenerator { | ||
var indeterminateIndicator *IndeterminateIndicator | ||
indicatorGenerator := indeterminateIndicatorMovingRightToLeft(indicator) | ||
lastRenderTime := time.Now() | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
currRenderTime := time.Now() | ||
if indeterminateIndicator == nil || duration == 0 || currRenderTime.Sub(lastRenderTime) > duration { | ||
tmpIndeterminateIndicator := indicatorGenerator(maxLen) | ||
indeterminateIndicator = &tmpIndeterminateIndicator | ||
lastRenderTime = currRenderTime | ||
} | ||
|
||
return *indeterminateIndicator | ||
} | ||
} | ||
|
||
func indeterminateIndicatorMovingBackAndForth(indicator string) IndeterminateIndicatorGenerator { | ||
increment := 1 | ||
nextPosition := 0 | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
currentPosition := nextPosition | ||
if currentPosition == 0 { | ||
increment = 1 | ||
} else if currentPosition+text.RuneCount(indicator) == maxLen { | ||
increment = -1 | ||
} | ||
nextPosition += increment | ||
|
||
return IndeterminateIndicator{ | ||
Position: currentPosition, | ||
Text: indicator, | ||
} | ||
} | ||
} | ||
|
||
func indeterminateIndicatorMovingLeftToRight(indicator string) IndeterminateIndicatorGenerator { | ||
nextPosition := 0 | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
currentPosition := nextPosition | ||
nextPosition++ | ||
if nextPosition+text.RuneCount(indicator) > maxLen { | ||
nextPosition = 0 | ||
} | ||
|
||
return IndeterminateIndicator{ | ||
Position: currentPosition, | ||
Text: indicator, | ||
} | ||
} | ||
} | ||
|
||
func indeterminateIndicatorMovingRightToLeft(indicator string) IndeterminateIndicatorGenerator { | ||
nextPosition := -1 | ||
|
||
return func(maxLen int) IndeterminateIndicator { | ||
if nextPosition == -1 { | ||
nextPosition = maxLen - text.RuneCount(indicator) | ||
} | ||
currentPosition := nextPosition | ||
nextPosition-- | ||
|
||
return IndeterminateIndicator{ | ||
Position: currentPosition, | ||
Text: indicator, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package progress | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndeterminateIndicatorMovingBackAndForth(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<=>" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, | ||
} | ||
|
||
f := IndeterminateIndicatorMovingBackAndForth(indicator, time.Millisecond*10) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
time.Sleep(time.Millisecond * 10) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingBackAndForth1(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "?" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, | ||
} | ||
|
||
f := indeterminateIndicatorMovingBackAndForth(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingBackAndForth2(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<>" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, | ||
} | ||
|
||
f := indeterminateIndicatorMovingBackAndForth(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingBackAndForth3(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<=>" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, | ||
} | ||
|
||
f := indeterminateIndicatorMovingBackAndForth(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func TestIndeterminateIndicatorMovingLeftToRight(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "?" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
} | ||
|
||
f := IndeterminateIndicatorMovingLeftToRight(indicator, time.Millisecond*10) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
time.Sleep(time.Millisecond * 10) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingLeftToRight1(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "?" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
} | ||
|
||
f := indeterminateIndicatorMovingLeftToRight(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingLeftToRight2(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<>" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, | ||
} | ||
|
||
f := indeterminateIndicatorMovingLeftToRight(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingLeftToRight3(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<=>" | ||
expectedPositions := []int{ | ||
0, 1, 2, 3, 4, 5, 6, 7, | ||
0, 1, 2, 3, 4, 5, 6, 7, | ||
} | ||
|
||
f := indeterminateIndicatorMovingLeftToRight(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func TestIndeterminateIndicatorMovingRightToLeft(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "?" | ||
expectedPositions := []int{ | ||
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
} | ||
|
||
f := IndeterminateIndicatorMovingRightToLeft(indicator, time.Millisecond*10) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
time.Sleep(time.Millisecond * 10) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingRightToLeft1(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "?" | ||
expectedPositions := []int{ | ||
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
} | ||
|
||
f := indeterminateIndicatorMovingRightToLeft(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingRightToLeft2(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<>" | ||
expectedPositions := []int{ | ||
8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
8, 7, 6, 5, 4, 3, 2, 1, 0, | ||
} | ||
|
||
f := indeterminateIndicatorMovingRightToLeft(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} | ||
|
||
func Test_indeterminateIndicatorMovingRightToLeft3(t *testing.T) { | ||
maxLen := 10 | ||
indicator := "<=>" | ||
expectedPositions := []int{ | ||
7, 6, 5, 4, 3, 2, 1, 0, | ||
7, 6, 5, 4, 3, 2, 1, 0, | ||
} | ||
|
||
f := indeterminateIndicatorMovingRightToLeft(indicator) | ||
for idx, expectedPosition := range expectedPositions { | ||
actual := f(maxLen) | ||
assert.Equal(t, expectedPosition, actual.Position, fmt.Sprintf("expectedIndeterminateIndicators[%d]", idx)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.