blob: 880375f961b5ea8d25abd75453b1c69fd95fe09a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package util
import (
"io"
"strings"
"time"
"github.com/briandowns/spinner"
)
// Spinner writes message to writer w and executes function fn. While fn is running a spinning animation will be
// displayed after message.
func Spinner(w io.Writer, message string, fn func() error) error {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(w))
if err := s.Color("blue", "bold"); err != nil {
return err
}
if !strings.HasSuffix(message, " ") {
message += " "
}
s.Prefix = message
s.FinalMSG = "\r" + message + "done\n"
s.Start()
err := fn()
if err != nil {
s.FinalMSG = "\r" + message + "failed\n"
}
s.Stop()
return err
}
|