Skip to content
Myr

main.go

This file

Path

examples/fullscreen/main.go

58 lines · 938 bytes · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23// A simple program that opens the alternate screen buffer then counts down4// from 5 and then exits.56import (7	"fmt"8	"log"9	"time"1011	tea "charm.land/bubbletea/v2"12)1314type model int1516type tickMsg time.Time1718func main() {19	p := tea.NewProgram(model(5))20	if _, err := p.Run(); err != nil {21		log.Fatal(err)22	}23}2425func (m model) Init() tea.Cmd {26	return tick()27}2829func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {30	switch msg := message.(type) {31	case tea.KeyPressMsg:32		switch msg.String() {33		case "q", "esc", "ctrl+c":34			return m, tea.Quit35		}3637	case tickMsg:38		m--39		if m <= 0 {40			return m, tea.Quit41		}42		return m, tick()43	}4445	return m, nil46}4748func (m model) View() tea.View {49	v := tea.NewView(fmt.Sprintf("\n\n     Hi. This program will exit in %d seconds...", m))50	v.AltScreen = true51	return v52}5354func tick() tea.Cmd {55	return tea.Tick(time.Second, func(t time.Time) tea.Msg {56		return tickMsg(t)57	})58}