2022-10-04 12:17:05 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-07 13:35:07 -04:00
|
|
|
"io"
|
2022-10-21 08:26:42 -04:00
|
|
|
"os"
|
2022-10-04 12:17:05 -04:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
tty "github.com/mattn/go-isatty"
|
2023-01-04 05:00:07 -05:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-21 08:26:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// hideCursor and showCursor are ANSI escape sequences to hide and show the cursor.
|
|
|
|
hideCursor = "\033[?25l"
|
|
|
|
showCursor = "\033[?25h"
|
2022-10-04 12:17:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-10-21 08:26:42 -04:00
|
|
|
spinnerStates = []string{"⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"}
|
|
|
|
dotsStates = []string{". ", ".. ", "..."}
|
2022-10-04 12:17:05 -04:00
|
|
|
)
|
|
|
|
|
2022-10-07 13:35:07 -04:00
|
|
|
type spinnerInterf interface {
|
|
|
|
Start(text string, showDots bool)
|
|
|
|
Stop()
|
2023-01-04 05:00:07 -05:00
|
|
|
io.Writer
|
2022-10-07 13:35:07 -04:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:17:05 -04:00
|
|
|
type spinner struct {
|
2022-10-21 08:26:42 -04:00
|
|
|
out io.Writer
|
|
|
|
delay time.Duration
|
|
|
|
wg *sync.WaitGroup
|
2022-10-28 08:52:39 -04:00
|
|
|
stop *atomic.Bool
|
|
|
|
spinFunc func(out io.Writer, wg *sync.WaitGroup, stop *atomic.Bool, delay time.Duration, text string, showDots bool)
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
2023-01-18 07:10:24 -05:00
|
|
|
func newSpinnerOrStderr(cmd *cobra.Command) (spinnerInterf, error) {
|
2023-01-04 05:00:07 -05:00
|
|
|
debug, err := cmd.Flags().GetBool("debug")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
return &nopSpinner{cmd.ErrOrStderr()}, nil
|
|
|
|
}
|
|
|
|
return newSpinner(cmd.ErrOrStderr()), nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
func newSpinner(writer io.Writer) *spinner {
|
|
|
|
s := &spinner{
|
|
|
|
out: writer,
|
2022-10-07 13:35:07 -04:00
|
|
|
wg: &sync.WaitGroup{},
|
2022-10-21 08:26:42 -04:00
|
|
|
delay: time.Millisecond * 100,
|
2022-10-28 08:52:39 -04:00
|
|
|
stop: &atomic.Bool{},
|
2022-10-07 13:35:07 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
s.spinFunc = spinTTY
|
2022-10-28 08:52:39 -04:00
|
|
|
|
2022-11-10 11:07:45 -05:00
|
|
|
if !(writer == os.Stderr && tty.IsTerminal(os.Stderr.Fd())) {
|
2022-10-21 08:26:42 -04:00
|
|
|
s.spinFunc = spinNoTTY
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
2022-10-07 13:35:07 -04:00
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
return s
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
// Start starts the spinner using the given text.
|
2022-10-07 13:35:07 -04:00
|
|
|
func (s *spinner) Start(text string, showDots bool) {
|
2022-10-04 12:17:05 -04:00
|
|
|
s.wg.Add(1)
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
go s.spinFunc(s.out, s.wg, s.stop, s.delay, text, showDots)
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
// Stop stops the spinner.
|
2022-10-04 12:17:05 -04:00
|
|
|
func (s *spinner) Stop() {
|
2022-10-28 08:52:39 -04:00
|
|
|
s.stop.Store(true)
|
2022-10-04 12:17:05 -04:00
|
|
|
s.wg.Wait()
|
|
|
|
}
|
2022-10-07 13:35:07 -04:00
|
|
|
|
2022-10-21 08:26:42 -04:00
|
|
|
// Write stops the spinner and writes the given bytes to the underlying writer.
|
|
|
|
func (s *spinner) Write(p []byte) (n int, err error) {
|
|
|
|
s.Stop()
|
|
|
|
return s.out.Write(p)
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
func spinTTY(out io.Writer, wg *sync.WaitGroup, stop *atomic.Bool, delay time.Duration, text string, showDots bool) {
|
2022-10-21 08:26:42 -04:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
fmt.Fprint(out, hideCursor)
|
|
|
|
|
|
|
|
for i := 0; ; i = (i + 1) % len(spinnerStates) {
|
2022-10-28 08:52:39 -04:00
|
|
|
if stop.Load() {
|
2022-10-21 08:26:42 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
dotsState := ""
|
|
|
|
if showDots {
|
|
|
|
dotsState = dotsStates[i%len(dotsStates)]
|
|
|
|
}
|
|
|
|
state := fmt.Sprintf("\r%s %s%s", spinnerStates[i], text, dotsState)
|
|
|
|
fmt.Fprint(out, state)
|
|
|
|
time.Sleep(delay)
|
|
|
|
}
|
|
|
|
dotsState := ""
|
|
|
|
if showDots {
|
|
|
|
dotsState = dotsStates[len(dotsStates)-1]
|
|
|
|
}
|
|
|
|
finalState := fmt.Sprintf("\r%s%s ", text, dotsState)
|
|
|
|
fmt.Fprintln(out, finalState)
|
|
|
|
fmt.Fprint(out, showCursor)
|
2022-10-07 13:35:07 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:52:39 -04:00
|
|
|
func spinNoTTY(out io.Writer, wg *sync.WaitGroup, _ *atomic.Bool, _ time.Duration, text string, _ bool) {
|
2022-10-21 08:26:42 -04:00
|
|
|
defer wg.Done()
|
|
|
|
fmt.Fprintln(out, text+"...")
|
2022-10-07 13:35:07 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 05:00:07 -05:00
|
|
|
type nopSpinner struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
2022-10-07 13:35:07 -04:00
|
|
|
|
2023-01-04 05:00:07 -05:00
|
|
|
func (s *nopSpinner) Start(string, bool) {}
|
|
|
|
func (s *nopSpinner) Stop() {}
|
|
|
|
func (s *nopSpinner) Write(p []byte) (n int, err error) {
|
|
|
|
return s.Writer.Write(p)
|
|
|
|
}
|