moved frame changing into go-roflcopter package. terminal render is broken

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

65
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"log"
"sync"
"time"
ui "github.com/gizak/termui/v3"
@ -11,70 +10,30 @@ import (
)
var (
count time.Duration = 0
count time.Duration = 15 * time.Second
str string = ""
p *widgets.Paragraph
)
const (
frame1 = " ROFL:ROFL: \n _^____ \n L __/ [] \\ \n O ===__ \\ \n L \\________] \n I I \n --------/ \n"
frame2 = " :ROFL:ROFL\n _^____ \n __/ [] \\ \nLOL===__ \\ \n \\________] \n I I \n --------/ \n"
playSound = true
timer = 15*time.Second
)
var wg sync.WaitGroup
func copter(delay time.Duration) {
func updateScreen() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
p := widgets.NewParagraph()
p.Text = frame1
p.SetRect(0, 0, 25, 10)
p.Text = str
p.SetRect(0, 0, 25, 12)
ui.Render(p)
nextframe := 2
count = 15 * time.Second
go timecounter(1 * time.Second)
for {
switch nextframe {
case 1:
{
p.Text = frame1 + count.String()
ui.Render(p)
time.Sleep(delay * time.Millisecond)
nextframe = 2
}
case 2:
{
p.Text = frame2 + count.String()
ui.Render(p)
time.Sleep(delay * time.Millisecond)
nextframe = 1
}
}
}
}
func timecounter(step time.Duration) {
for count > 0 {
count -= step
time.Sleep(step)
}
}
func player() {
roflcopter.Sound(&wg, false)
wg.Wait()
player()
}
func main() {
//fmt.Println("HW")
//roflcopter.Sound(&wg, false)
go player()
go copter(120)
time.Sleep(15 * time.Second)
p = widgets.NewParagraph()
rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen)
rofl.Start()
time.Sleep(timer)
}

View File

@ -3,7 +3,6 @@ package roflcopter
import (
"bytes"
_ "embed"
"fmt"
"io"
"log"
"sync"
@ -16,11 +15,84 @@ import (
//go:embed embed\swah.mp3
var swah []byte
func Sound(wg *sync.WaitGroup, output bool) {
wg.Add(1)
if output {
fmt.Println("SWAH")
var (
work bool = true
duration time.Duration
wg sync.WaitGroup
)
const (
frame1 = " ROFL:ROFL: \n _^____ \n L __/ [] \\ \n O ===__ \\ \n L \\________] \n I I \n --------/ \n"
frame2 = " :ROFL:ROFL\n _^____ \n __/ [] \\ \nLOL===__ \\ \n \\________] \n I I \n --------/ \n"
)
type Roflcopter struct {
output bool
work bool
count time.Duration
str *string
runner func()
}
func NewRoflcopter(amount time.Duration, outStr *string, out bool, funcrunner func()) *Roflcopter {
return &Roflcopter{
output: out,
work: true,
str: outStr,
count: amount,
runner: funcrunner,
}
}
func (b *Roflcopter) timecounter(step time.Duration) {
for b.count > 0 {
b.count -= step
time.Sleep(step)
}
work = false
}
func (b *Roflcopter) Start() {
*b.str = frame1 + b.count.String()
work = b.work
duration = b.count
if b.output {
go b.player()
}
go b.timecounter(1 * time.Second)
go b.frameChanger(120 * time.Millisecond)
}
func (b *Roflcopter) player() {
b.sound()
wg.Wait()
b.player()
}
func (b *Roflcopter) frameChanger(delay time.Duration) {
b.runner()
nextframe := 2
for work {
switch nextframe {
case 1:
{
*b.str = frame1 + b.count.String()
time.Sleep(delay)
nextframe = 2
}
case 2:
{
*b.str = frame2 + b.count.String()
time.Sleep(delay)
nextframe = 1
}
}
}
}
func (b *Roflcopter) sound() {
wg.Add(1)
t := 1*time.Second + 390*time.Millisecond
streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(swah)))
if err != nil {