fix terminal render

This commit is contained in:
Морозов Андрей 2022-02-12 20:30:38 +04:00
parent 1024f75332
commit d238897080
2 changed files with 30 additions and 23 deletions

22
main.go
View File

@ -12,28 +12,34 @@ import (
var ( var (
count time.Duration = 15 * time.Second count time.Duration = 15 * time.Second
str string = "" str string = ""
p *widgets.Paragraph p *widgets.Paragraph
) )
const ( const (
playSound = true playSound = true
timer = 15*time.Second timer = 15 * time.Second
) )
func updateScreen() { func prepareScreen() {
if err := ui.Init(); err != nil { if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err) log.Fatalf("failed to initialize termui: %v", err)
} }
defer ui.Close() //defer ui.Close()
p = widgets.NewParagraph()
p.Text = str
p.SetRect(0, 0, 25, 10)
ui.Render(p)
}
func updateScreen() {
p.Text = str p.Text = str
p.SetRect(0, 0, 25, 12)
ui.Render(p) ui.Render(p)
} }
func main() { func main() {
p = widgets.NewParagraph() prepareScreen()
rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen) rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen)
rofl.Start() rofl.Start()
time.Sleep(timer) time.Sleep(timer + 500*time.Millisecond)
ui.Close()
} }

View File

@ -18,7 +18,7 @@ var swah []byte
var ( var (
work bool = true work bool = true
duration time.Duration duration time.Duration
wg sync.WaitGroup wg sync.WaitGroup
) )
const ( const (
@ -44,14 +44,6 @@ func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner fu
} }
} }
func (b *Roflcopter) timecounter(step time.Duration) {
for b.count > 0 {
b.count -= step
time.Sleep(step)
}
work = false
}
func (b *Roflcopter) Start() { func (b *Roflcopter) Start() {
*b.str = frame1 + b.count.String() *b.str = frame1 + b.count.String()
work = b.work work = b.work
@ -59,9 +51,18 @@ func (b *Roflcopter) Start() {
if b.output { if b.output {
go b.player() go b.player()
} }
go b.timecounter(1 * time.Second) go b.timecounter(500 * time.Millisecond)
go b.frameChanger(120 * time.Millisecond) go b.frameChanger(125 * time.Millisecond)
} }
func (b *Roflcopter) timecounter(step time.Duration) {
for b.count >= 0 {
b.count -= step
time.Sleep(step)
}
work = false
}
func (b *Roflcopter) player() { func (b *Roflcopter) player() {
b.sound() b.sound()
wg.Wait() wg.Wait()
@ -69,31 +70,31 @@ func (b *Roflcopter) player() {
} }
func (b *Roflcopter) frameChanger(delay time.Duration) { func (b *Roflcopter) frameChanger(delay time.Duration) {
b.runner()
nextframe := 2 nextframe := 2
for work { for work {
switch nextframe { switch nextframe {
case 1: case 1:
{ {
*b.str = frame1 + b.count.String() *b.str = frame1 + "Ends in " + b.count.String()
time.Sleep(delay) time.Sleep(delay)
nextframe = 2 nextframe = 2
} }
case 2: case 2:
{ {
*b.str = frame2 + b.count.String() *b.str = frame2 + "Ends in " + b.count.String()
time.Sleep(delay) time.Sleep(delay)
nextframe = 1 nextframe = 1
} }
} }
b.runner()
} }
} }
func (b *Roflcopter) sound() { func (b *Roflcopter) sound() {
wg.Add(1) wg.Add(1)
t := 1*time.Second + 390*time.Millisecond t := 1*time.Second + 380*time.Millisecond
streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah))) streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah)))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)