mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Reworked logger progress
This commit is contained in:
parent
588d402c63
commit
a6c14cb6c9
1 changed files with 39 additions and 39 deletions
|
|
@ -1,12 +1,12 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -24,6 +24,7 @@ type logger struct {
|
||||||
|
|
||||||
progress chan progress
|
progress chan progress
|
||||||
done chan interface{}
|
done chan interface{}
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLogger(debug, info, err bool) *logger {
|
func newLogger(debug, info, err bool) *logger {
|
||||||
|
|
@ -87,69 +88,66 @@ func (l *logger) Empty() {
|
||||||
func (l *logger) SetProgress(format string, v ...interface{}) {
|
func (l *logger) SetProgress(format string, v ...interface{}) {
|
||||||
if out.InfoLog.Writer() == io.Discard {
|
if out.InfoLog.Writer() == io.Discard {
|
||||||
return
|
return
|
||||||
}
|
} else if l.devView {
|
||||||
|
l.Debug(format, v...)
|
||||||
message := fmt.Sprintf(format, v...)
|
|
||||||
|
|
||||||
if l.progress != nil {
|
|
||||||
l.progress <- progress{
|
|
||||||
message: message,
|
|
||||||
stop: false,
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l.progress = make(chan progress)
|
initialMessage := fmt.Sprintf(format, v...)
|
||||||
l.done = make(chan interface{})
|
|
||||||
|
p := progress{
|
||||||
|
message: initialMessage,
|
||||||
|
}
|
||||||
|
|
||||||
|
l.lock.Lock()
|
||||||
|
if l.done != nil {
|
||||||
|
l.progress <- p
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
l.progress = make(chan progress, 1)
|
||||||
|
l.progress <- p
|
||||||
|
l.done = make(chan interface{})
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
states := []string{"-", "\\", "|", "/"}
|
states := []string{"-", "\\", "|", "/"}
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
|
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Millisecond)
|
|
||||||
select {
|
select {
|
||||||
case p := <-l.progress:
|
case p := <-l.progress:
|
||||||
cancel()
|
|
||||||
|
|
||||||
if p.stop {
|
if p.stop {
|
||||||
if !l.devView {
|
fmt.Printf("\r" + strings.Repeat(" ", 2+len(initialMessage)))
|
||||||
fmt.Printf("\r" + strings.Repeat(" ", 2+len(message)))
|
if count > 1 {
|
||||||
fmt.Printf("\r➞ %s\n", p.message)
|
fmt.Printf("\r↳ %s\n", p.message)
|
||||||
} else {
|
} else {
|
||||||
l.Debug(p.message)
|
fmt.Printf("\r➞ %s\n", p.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.done != nil {
|
||||||
|
l.done <- nil
|
||||||
|
}
|
||||||
l.progress = nil
|
l.progress = nil
|
||||||
|
|
||||||
if count > 0 {
|
l.lock.Unlock()
|
||||||
fmt.Printf("↳ %s\n", p.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
l.done <- nil
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if !l.devView {
|
if count > 0 {
|
||||||
fmt.Printf("\r↓ %s\n", message)
|
fmt.Printf("\r↓ %s\n", p.message)
|
||||||
} else {
|
|
||||||
l.Debug(message)
|
|
||||||
}
|
}
|
||||||
|
l.progress = make(chan progress, 1)
|
||||||
|
|
||||||
l.progress = make(chan progress)
|
|
||||||
count++
|
count++
|
||||||
|
|
||||||
if !l.devView {
|
fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
|
||||||
fmt.Printf("\r" + strings.Repeat(" ", 2+len(message)))
|
l.lock.Unlock()
|
||||||
fmt.Printf("\r➞ %s\n", p.message)
|
|
||||||
} else {
|
|
||||||
l.Debug(p.message)
|
|
||||||
}
|
|
||||||
message = p.message
|
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
default:
|
||||||
if !l.devView && i%10 == 0 {
|
if i%10 == 0 {
|
||||||
fmt.Printf("\r%s %s", states[i/10%4], message)
|
fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
|
||||||
}
|
}
|
||||||
|
time.Sleep(35 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
@ -160,9 +158,11 @@ func (l *logger) StopProgress(format string, v ...interface{}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.lock.Lock()
|
||||||
l.progress <- progress{
|
l.progress <- progress{
|
||||||
message: fmt.Sprintf(format, v...),
|
message: fmt.Sprintf(format, v...),
|
||||||
stop: true,
|
stop: true,
|
||||||
}
|
}
|
||||||
<-l.done
|
<-l.done
|
||||||
|
l.done = nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue