Rely on bubbletea for sizing information

This commit is contained in:
Maas Lalani 2021-11-05 00:17:57 -04:00
parent 654be31c1b
commit 9c59ff5446
6 changed files with 14 additions and 88 deletions

View File

@ -57,13 +57,10 @@ func Spawn(width, height int) []*simulation.Particle {
return particles
}
func InitialModelWithSize(width, height int) model {
func InitialModel() model {
return model{system: &simulation.System{
Particles: Spawn(width, height),
Frame: simulation.Frame{
Width: width,
Height: height,
},
Particles: []*simulation.Particle{},
Frame: simulation.Frame{},
}}
}
@ -88,6 +85,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.system.Update()
return m, animate()
case tea.WindowSizeMsg:
if m.system.Frame.Width == 0 && m.system.Frame.Height == 0 {
// For the first frameMsg spawn a system of particles
m.system.Particles = Spawn(msg.Width, msg.Height)
}
m.system.Frame.Width = msg.Width
m.system.Frame.Height = msg.Height
return m, nil

View File

@ -1,16 +0,0 @@
//go:build !windows
// +build !windows
package confetti
import (
"golang.org/x/term"
)
func InitialModel() model {
width, height, err := term.GetSize(0)
if err != nil {
panic(err)
}
return InitialModelWithSize(width, height)
}

View File

@ -1,22 +0,0 @@
//go:build windows
// +build windows
package confetti
import (
"syscall"
"golang.org/x/term"
)
func InitialModel() model {
h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
if err != nil {
panic(err)
}
width, height, err := term.GetSize(int(h))
if err != nil {
panic(err)
}
return InitialModelWithSize(width, height)
}

View File

@ -79,13 +79,10 @@ func SpawnExplosion(x, y float64, width, height int) []*simulation.Particle {
return particles
}
func InitialModelWithSize(width, height int) model {
func InitialModel() model {
return model{system: &simulation.System{
Particles: []*simulation.Particle{SpawnShoot(width, height)},
Frame: simulation.Frame{
Width: width,
Height: height,
},
Particles: []*simulation.Particle{},
Frame: simulation.Frame{},
}}
}
@ -110,6 +107,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.system.Update()
return m, animate()
case tea.WindowSizeMsg:
if m.system.Frame.Width == 0 && m.system.Frame.Height == 0 {
// For the first frameMsg spawn a system of particles
m.system.Particles = append(m.system.Particles, SpawnShoot(msg.Width, msg.Height))
}
m.system.Frame.Width = msg.Width
m.system.Frame.Height = msg.Height
return m, nil

View File

@ -1,16 +0,0 @@
//go:build !windows
// +build !windows
package fireworks
import (
"golang.org/x/term"
)
func InitialModel() model {
width, height, err := term.GetSize(0)
if err != nil {
panic(err)
}
return InitialModelWithSize(width, height)
}

View File

@ -1,22 +0,0 @@
//go:build windows
// +build windows
package fireworks
import (
"syscall"
"golang.org/x/term"
)
func InitialModel() model {
h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
if err != nil {
panic(err)
}
width, height, err := term.GetSize(int(h))
if err != nil {
panic(err)
}
return InitialModelWithSize(width, height)
}