diff --git a/internal/ui/anim.go b/internal/ui/anim.go new file mode 100644 index 0000000..d6e68d9 --- /dev/null +++ b/internal/ui/anim.go @@ -0,0 +1,61 @@ +package ui + +import ( + "math" + "strings" + "time" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/lucasb-eyer/go-colorful" +) + +// ── Motion ───────────────────────────────────────────────────────── +// +// A few things move: Welcome's logo flows through the theme's colours and +// the deploy bar shimmers while it runs. Frames tick only while the screen +// showing asks for them, and never in plain mode. Frame 0 is the still +// picture, which the layout tests draw. + +var animFrame int + +type animTickMsg struct{} + +const animEvery = 100 * time.Millisecond + +// animator is a screen that moves while animating says so. +type animator interface{ animating() bool } + +// animate asks for the next frame if the screen showing moves and no tick +// is on its way already. +func (a *App) animate() tea.Cmd { + an, ok := a.cur().(animator) + if a.animOn || plain || !ok || !an.animating() { + return nil + } + a.animOn = true + return tea.Tick(animEvery, func(time.Time) tea.Msg { return animTickMsg{} }) +} + +func (s *welcomeScreen) animating() bool { return true } + +// flowBar is a progress bar w wide in the theme's ramp; while running, a +// highlight sweeps along its filled part. +func flowBar(frac float64, w int, running bool) string { + n := int(max(0, min(1, frac))*float64(w) + 0.5) + stops := gradientStops() + hi := -100.0 + if running && n > 0 { + hi = float64(animFrame%(n+12) - 6) // starts off the bar, so frame 0 is plain + } + white, _ := colorful.Hex("#FFFFFF") + var b strings.Builder + for i := range n { + c, _ := colorful.Hex(ramp(stops, float64(i)/float64(max(1, w-1)))) + if d := math.Abs(float64(i) - hi); d < 4 { + c = c.BlendLuv(white, 0.55*(1-d/4)) + } + b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color(c.Clamped().Hex())).Render("█")) + } + return b.String() + sFaint.Render(strings.Repeat("░", w-n)) +} diff --git a/internal/ui/anim_test.go b/internal/ui/anim_test.go new file mode 100644 index 0000000..9182a5e --- /dev/null +++ b/internal/ui/anim_test.go @@ -0,0 +1,42 @@ +package ui + +import ( + "testing" + + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" + "github.com/muesli/termenv" +) + +func TestAnimTicksOnlyWhereSomethingMoves(t *testing.T) { + a := demoApp(t, 100, 30) + a.step, a.animOn = a.index("Welcome"), false + if a.animate() == nil { + t.Fatal("Welcome's logo should tick") + } + if a.animate() != nil { + t.Error("a second tick while one is on its way") + } + a.animOn = false + a.step = a.index("Manage") + if a.animate() != nil { + t.Error("Manage doesn't move, yet it ticks") + } +} + +func TestFlowBar(t *testing.T) { + defer func(f int, p termenv.Profile) { animFrame = f; lipgloss.SetColorProfile(p) }(animFrame, lipgloss.ColorProfile()) + lipgloss.SetColorProfile(termenv.TrueColor) + animFrame = 0 + still := flowBar(0.5, 20, true) + if still != flowBar(0.5, 20, false) { + t.Error("frame 0 should be the still picture") + } + animFrame = 8 + if flowBar(0.5, 20, true) == still { + t.Error("the bar doesn't shimmer while running") + } + if w := ansi.StringWidth(flowBar(0.3, 17, true)); w != 17 { + t.Errorf("bar is %d wide, want 17", w) + } +} diff --git a/internal/ui/app.go b/internal/ui/app.go index 8582df4..ca8e2eb 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -70,6 +70,7 @@ type App struct { health *deploy.Health // the header's dot (health.go) healthGen int + animOn bool // a frame tick is on its way (anim.go) } func New(cfg *config.Config, loaded bool) *App { @@ -123,7 +124,7 @@ func (a *App) index(name string) int { // wizard is the screens shown in the stepper, Welcome and Manage excluded. func (a *App) wizard() []screen { return a.screens[1:a.index("Manage")] } -func (a *App) Init() tea.Cmd { return tea.Batch(a.cur().enter(a.cfg), a.checkHealth()) } +func (a *App) Init() tea.Cmd { return tea.Batch(a.cur().enter(a.cfg), a.checkHealth(), a.animate()) } func (a *App) cur() screen { return a.screens[a.step] } @@ -160,6 +161,16 @@ func (a *App) jump(n int) tea.Cmd { type ownedMsg interface{ owner() string } func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if _, ok := msg.(animTickMsg); ok { + animFrame++ + a.animOn = false + return a, a.animate() + } + m, cmd := a.update(msg) + return m, tea.Batch(cmd, a.animate()) +} + +func (a *App) update(msg tea.Msg) (tea.Model, tea.Cmd) { switch m := msg.(type) { case tea.WindowSizeMsg: a.w, a.h = m.Width, m.Height diff --git a/internal/ui/screens_deploy.go b/internal/ui/screens_deploy.go index dffc343..8a4d027 100644 --- a/internal/ui/screens_deploy.go +++ b/internal/ui/screens_deploy.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "github.com/charmbracelet/bubbles/progress" "github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" @@ -663,7 +662,6 @@ type deployScreen struct { vp viewport.Model follow bool spin spinner.Model - bar progress.Model } func newDeployScreen() *deployScreen { @@ -673,7 +671,6 @@ func newDeployScreen() *deployScreen { return &deployScreen{ spin: sp, vp: viewport.New(0, 0), - bar: progress.New(progress.WithGradient("#9D7CFF", "#3EF0B0"), progress.WithoutPercentage()), } } @@ -719,7 +716,8 @@ func (s *deployScreen) title() string { } return "Deploy" } -func (s *deployScreen) typing() bool { return false } +func (s *deployScreen) typing() bool { return false } +func (s *deployScreen) animating() bool { return s.running } func (s *deployScreen) keys() [][2]string { if s.running { return [][2]string{{"↑↓", "scroll log"}, {"end", "follow"}, {"ctrl+c", "abort"}} @@ -981,12 +979,12 @@ func (s *deployScreen) view(w, h int) string { nDone++ } } - s.bar.Width = max(10, w-40) + barW := max(10, w-40) if narrow { - s.bar.Width = max(10, w-2) + barW = max(10, w-2) } stats := []string{ - s.bar.ViewAs(float64(nDone) / float64(len(s.plan))), + flowBar(float64(nDone)/float64(len(s.plan)), barW, s.running), "", sMuted.Render("elapsed ") + sTitle.Render(s.elapsed.Round(time.Second).String()), sMuted.Render("tasks ") + sTitle.Render(fmt.Sprint(s.tasks)), diff --git a/internal/ui/screens_setup.go b/internal/ui/screens_setup.go index 56b6af2..0db45cb 100644 --- a/internal/ui/screens_setup.go +++ b/internal/ui/screens_setup.go @@ -71,7 +71,7 @@ func (s *welcomeScreen) view(w, h int) string { feature := func(icon, title, text string) string { return sPink.Render(icon) + " " + sTitle.Render(title) + "\n " + wrap(sMuted, " ", text) } - brand := gradientLines(strings.TrimPrefix(logo, "\n")) + brand := gradientLinesAt(strings.TrimPrefix(logo, "\n"), float64(animFrame)*0.03) if lipgloss.Width(brand) > w || plain { brand = gradient("◢◤ interzone") }