Skip to content

Commit 3786991

Browse files
authored
Merge pull request #2 from kaduartur/feature/spinner-windows
[FEATURE] Show title when running on Windows
2 parents a4f8993 + 8aaa84f commit 3786991

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

cmd/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"time"
67

@@ -16,10 +17,17 @@ func main() {
1617
s.Stop()
1718

1819
fmt.Println()
19-
// Run with another template
20+
// Run with another template and success message
2021
sp := spinner.New("Loading...")
2122
sp.SetTemplate(template.Arrow)
2223
sp.Start()
2324
time.Sleep(time.Second * 2)
24-
sp.Stop()
25+
sp.Success("Success")
26+
27+
fmt.Println()
28+
// Run with error message
29+
spi := spinner.New("Loading...")
30+
spi.Start()
31+
time.Sleep(time.Second * 2)
32+
spi.Error(errors.New("error to load"))
2533
}

pkg/spinner/spinner.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package spinner
33
import (
44
"fmt"
55
"io"
6+
"runtime"
67
"sync"
78
"syscall"
89
"time"
@@ -56,7 +57,14 @@ func StartNew(title string) *Spinner {
5657

5758
// Start starts the spinner execution
5859
func (s *Spinner) Start() *Spinner {
60+
goos := runtime.GOOS
61+
if goos == "windows" { // Show title without animation on Windows
62+
fmt.Print(s.Title)
63+
return s
64+
}
65+
5966
go s.writer()
67+
6068
return s
6169
}
6270

@@ -90,13 +98,13 @@ func (s *Spinner) Stop() {
9098
// Success gives success feedback for users and stops the spinner execution
9199
func (s *Spinner) Success(msg string) {
92100
s.Stop()
93-
fmt.Println(msg)
101+
fmt.Printf("\r%s\n", msg)
94102
}
95103

96104
// Error gives error feedback for users and stops the spinner execution
97105
func (s *Spinner) Error(err error) {
98106
s.Stop()
99-
fmt.Println(err)
107+
fmt.Printf("\r%s\n", err)
100108
}
101109

102110
// animate runs the template animation

0 commit comments

Comments
 (0)