-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tab/feature/loader
feat(loader): Add loading indicator functionality
- Loading branch information
Showing
3 changed files
with
112 additions
and
2 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,35 @@ | ||
package loader | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type Loader struct { | ||
running int32 | ||
} | ||
|
||
func New() *Loader { | ||
return &Loader{} | ||
} | ||
|
||
func (l *Loader) Start() { | ||
atomic.StoreInt32(&l.running, 1) | ||
|
||
chars := []rune{'|', '/', '-', '\\'} | ||
idx := 0 | ||
|
||
go func() { | ||
for atomic.LoadInt32(&l.running) == 1 { | ||
fmt.Printf("\r%c Loading...", chars[idx]) | ||
idx = (idx + 1) % len(chars) | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}() | ||
} | ||
|
||
func (l *Loader) Stop() { | ||
atomic.StoreInt32(&l.running, 0) | ||
fmt.Printf("\r\033[K") | ||
} |
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,69 @@ | ||
package loader | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoader_Start(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expectRunning bool | ||
}{ | ||
{ | ||
name: "Loader starts and runs", | ||
waitTime: 200 * time.Millisecond, | ||
expectRunning: true, | ||
}, | ||
{ | ||
name: "Loader runs for a short time", | ||
waitTime: 100 * time.Millisecond, | ||
expectRunning: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := New() | ||
|
||
l.Start() | ||
time.Sleep(tt.waitTime) | ||
|
||
running := atomic.LoadInt32(&l.running) == 1 | ||
assert.Equal(t, tt.expectRunning, running) | ||
|
||
l.Stop() | ||
}) | ||
} | ||
} | ||
|
||
func TestLoader_Stop(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
waitTime time.Duration | ||
expectRunning bool | ||
}{ | ||
{ | ||
name: "Loader stops after start", | ||
waitTime: 100 * time.Millisecond, | ||
expectRunning: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := New() | ||
|
||
l.Start() | ||
l.Stop() | ||
time.Sleep(tt.waitTime) | ||
|
||
running := atomic.LoadInt32(&l.running) == 1 | ||
assert.Equal(t, tt.expectRunning, running) | ||
}) | ||
} | ||
} |