Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a spinner for let users wait that the test is running #148

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ package common

import (
"fmt"
"os"
"strings"

"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"os"
"sigs.k8s.io/hydrophone/pkg/log"
"strings"
"time"
)

// PrintInfo prints the information about the cluster
func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {
spinner := NewSpinner(os.Stdout)
spinner.Start()

time.Sleep(2 * time.Second)
serverVersion, err := clientSet.ServerVersion()
if err != nil {
log.Fatal("Error fetching server version: ", err)
Expand All @@ -41,7 +44,7 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {
viper.Set("busybox-image", busyboxImage)
}

log.Printf("API endpoint : %s", config.Host)
log.PrintfAPI("API endpoint : %s", config.Host)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By moving the spinner we can revert this to a normal printf and remove the PrintAPI method.

Copy link
Member Author

@Bharadwajshivam28 Bharadwajshivam28 Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rjsadow I made a new method PrintAPI because I wanted the Spinner to be at top most and below this the function to be running and when using Printf the output is-
Screenshot from 2024-02-11 23-41-05

and when i create separate method for API line the output is-
Screenshot from 2024-02-11 23-41-29

My end goal is to keep the spinner at top most and everything below it and if i use the common Printf and add a new line in that then all the lines will have a new line..

Please correct me if i am wrong

log.Printf("Server version : %#v", *serverVersion)
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/common/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package common

import (
"fmt"
"io"
"runtime"
"sync"
"time"
)

var spinnerFrames = []string{
"⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰",
"⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁",
}

type Spinner struct {
stop chan struct{}
stopped chan struct{}
mu *sync.Mutex
running bool
writer io.Writer
ticker *time.Ticker
prefix string
suffix string
frameFormat string
}

func NewSpinner(w io.Writer) *Spinner {
frameFormat := "\x1b[?7l\r%s%s%s\x1b[?7h"
if runtime.GOOS == "windows" {
frameFormat = "\r%s%s%s"
}
return &Spinner{
stop: make(chan struct{}, 1),
stopped: make(chan struct{}),
mu: &sync.Mutex{},
writer: w,
frameFormat: frameFormat,
}
}

func (s *Spinner) Start() {
s.mu.Lock()
defer s.mu.Unlock()
if s.running {
return
}
s.running = true
s.ticker = time.NewTicker(time.Millisecond * 100)
go func() {
for {
for _, frame := range spinnerFrames {
select {
case <-s.stop:
func() {
s.mu.Lock()
defer s.mu.Unlock()
s.ticker.Stop()
s.running = false
s.stopped <- struct{}{}
}()
return
case <-s.ticker.C:
func() {
s.mu.Lock()
defer s.mu.Unlock()
fmt.Fprintf(s.writer, s.frameFormat, s.prefix, frame, s.suffix)
}()
}
}
}
}()
}

func (s *Spinner) Stop() {
s.mu.Lock()
if !s.running {
s.mu.Unlock()
return
}
s.stop <- struct{}{}
s.mu.Unlock()
<-s.stopped
}
6 changes: 6 additions & 0 deletions pkg/log/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func Printf(format string, v ...any) {
slog.Info(fmt.Sprintf(format, v...))
}

// Print logs for API
func PrintfAPI(format string, v ...interface{}) {
fmt.Print("\n")
slog.Info(fmt.Sprintf(format, v...))
}

// Println logs an info message from the given arguments.
func Println(v ...any) {
slog.Info(fmt.Sprint(v...))
Expand Down