Skip to content

Commit

Permalink
Merge pull request #6 from tab/feature/loader
Browse files Browse the repository at this point in the history
feat(loader): Add loading indicator functionality
  • Loading branch information
tab authored Oct 13, 2024
2 parents 9dff7f4 + b6a7e40 commit 81261dd
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cmt/internal/flags"
"cmt/internal/git"
"cmt/internal/gpt"
"cmt/internal/loader"
)

func main() {
Expand All @@ -36,15 +37,20 @@ func main() {
diff, err := g.Diff(ctx)
if err != nil {
if errors.Is(err, errors.ErrNoGitChanges) {
fmt.Println("⚠️ No changes to commit")
fmt.Println("\n⚠️ No changes to commit")
} else {
log.Fatalf("❌ Error getting git diff: %s\n", err)
log.Fatalf("\n❌ Error getting git diff: %s\n", err)
}
return
}

progress := loader.New()
progress.Start()

model := &gpt.GPTModel{}
message, err := model.Fetch(ctx, diff)
progress.Stop()

if err != nil {
log.Fatalf("⚠️ Error requesting commit message: %s\n", err)
return
Expand Down
35 changes: 35 additions & 0 deletions internal/loader/loader.go
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")
}
69 changes: 69 additions & 0 deletions internal/loader/loader_test.go
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)
})
}
}

0 comments on commit 81261dd

Please sign in to comment.