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 ( import (
"log" "log"
"sync"
"time" "time"
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
@ -11,70 +10,30 @@ import (
) )
var ( var (
count time.Duration = 0 count time.Duration = 15 * time.Second
str string = ""
p *widgets.Paragraph
) )
const ( const (
frame1 = " ROFL:ROFL: \n _^____ \n L __/ [] \\ \n O ===__ \\ \n L \\________] \n I I \n --------/ \n" playSound = true
frame2 = " :ROFL:ROFL\n _^____ \n __/ [] \\ \nLOL===__ \\ \n \\________] \n I I \n --------/ \n" timer = 15*time.Second
) )
var wg sync.WaitGroup func updateScreen() {
func copter(delay time.Duration) {
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.Text = frame1 p.SetRect(0, 0, 25, 12)
p.SetRect(0, 0, 25, 10)
ui.Render(p) 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() { func main() {
//fmt.Println("HW") p = widgets.NewParagraph()
//roflcopter.Sound(&wg, false) rofl := roflcopter.NewRoflcopter(timer, &str, playSound, updateScreen)
go player() rofl.Start()
go copter(120) time.Sleep(timer)
time.Sleep(15 * time.Second)
} }

View File

@ -3,7 +3,6 @@ package roflcopter
import ( import (
"bytes" "bytes"
_ "embed" _ "embed"
"fmt"
"io" "io"
"log" "log"
"sync" "sync"
@ -16,11 +15,84 @@ import (
//go:embed embed\swah.mp3 //go:embed embed\swah.mp3
var swah []byte var swah []byte
func Sound(wg *sync.WaitGroup, output bool) { var (
wg.Add(1) work bool = true
if output { duration time.Duration
fmt.Println("SWAH") 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 t := 1*time.Second + 390*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 {