From 9c59ff5446b41f89d336a68e35fd79c2fb50ae62 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 5 Nov 2021 00:17:57 -0400 Subject: [PATCH] Rely on bubbletea for sizing information --- confetti/confetti.go | 13 +++++++------ confetti/confetti_unix.go | 16 ---------------- confetti/confetti_windows.go | 22 ---------------------- fireworks/fireworks.go | 13 +++++++------ fireworks/fireworks_unix.go | 16 ---------------- fireworks/fireworks_windows.go | 22 ---------------------- 6 files changed, 14 insertions(+), 88 deletions(-) delete mode 100644 confetti/confetti_unix.go delete mode 100644 confetti/confetti_windows.go delete mode 100644 fireworks/fireworks_unix.go delete mode 100644 fireworks/fireworks_windows.go diff --git a/confetti/confetti.go b/confetti/confetti.go index d0fa9a3..9b7749d 100644 --- a/confetti/confetti.go +++ b/confetti/confetti.go @@ -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 diff --git a/confetti/confetti_unix.go b/confetti/confetti_unix.go deleted file mode 100644 index 91e6cfd..0000000 --- a/confetti/confetti_unix.go +++ /dev/null @@ -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) -} diff --git a/confetti/confetti_windows.go b/confetti/confetti_windows.go deleted file mode 100644 index 6bc640a..0000000 --- a/confetti/confetti_windows.go +++ /dev/null @@ -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) -} diff --git a/fireworks/fireworks.go b/fireworks/fireworks.go index b9d5354..275e223 100644 --- a/fireworks/fireworks.go +++ b/fireworks/fireworks.go @@ -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 diff --git a/fireworks/fireworks_unix.go b/fireworks/fireworks_unix.go deleted file mode 100644 index 0448347..0000000 --- a/fireworks/fireworks_unix.go +++ /dev/null @@ -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) -} diff --git a/fireworks/fireworks_windows.go b/fireworks/fireworks_windows.go deleted file mode 100644 index c8298ad..0000000 --- a/fireworks/fireworks_windows.go +++ /dev/null @@ -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) -}