The Throbber

This commit is contained in:
2026-04-21 19:31:19 +02:00
parent 85e98acc45
commit d880661599
2 changed files with 48 additions and 1 deletions
+48 -1
View File
@@ -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) {
BIN
View File
Binary file not shown.