The Throbber
This commit is contained in:
+48
-1
@@ -7,6 +7,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/pipeline"
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/project"
|
||||
@@ -27,6 +29,48 @@ type context struct {
|
||||
args []string
|
||||
}
|
||||
|
||||
type spinner struct {
|
||||
mu sync.Mutex
|
||||
on bool
|
||||
text string
|
||||
msg []string
|
||||
}
|
||||
|
||||
var spin = &spinner{
|
||||
msg: []string{"-", "\\", "|", "/"},
|
||||
}
|
||||
|
||||
func (s *spinner) start(text string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.on = true
|
||||
s.text = text
|
||||
go s.run(text)
|
||||
}
|
||||
|
||||
func (s *spinner) stop() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.on = false
|
||||
}
|
||||
|
||||
func (s *spinner) run(text string) {
|
||||
i := 0
|
||||
for {
|
||||
s.mu.Lock()
|
||||
if !s.on {
|
||||
s.mu.Unlock()
|
||||
fmt.Fprintf(os.Stderr, "\r")
|
||||
return
|
||||
}
|
||||
msg := s.msg[i%len(s.msg)]
|
||||
fmt.Fprintf(os.Stderr, "\r[%s] %s", msg, text)
|
||||
s.mu.Unlock()
|
||||
i++
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
var commands = []command{
|
||||
{
|
||||
name: "build",
|
||||
@@ -118,9 +162,12 @@ func Run(args []string) (int, error) {
|
||||
default:
|
||||
for _, cmd := range commands {
|
||||
if cmd.name == args[0] {
|
||||
spin.start("running " + cmd.name)
|
||||
if err := cmd.run(ctx); err != nil {
|
||||
spin.stop()
|
||||
return 1, err
|
||||
}
|
||||
spin.stop()
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
@@ -741,7 +788,7 @@ func loadProject(cwd string) (*project.Project, error) {
|
||||
return nil, errors.Join(failures...)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func printUsage(w io.Writer) {
|
||||
|
||||
Reference in New Issue
Block a user