A powerful little TUI framework 🏗
Go to file
Christian Rocha 6c8ee9f7dc
Remove authors
2020-04-27 11:43:11 -04:00
.github/workflows
examples Remove ErrMsg and ModelAssertionErr 2020-04-22 11:00:30 -04:00
.gitignore
LICENSE
README.md Remove authors 2020-04-27 11:43:11 -04:00
go.mod
go.sum
key.go
logging.go
tea.go Remove ErrMsg and ModelAssertionErr 2020-04-22 11:00:30 -04:00
tty_unix.go
tty_windows.go

README.md

Tea

The fun, functional way to build terminal apps. A Go framework based on The Elm Architecture. 茶!

⚠️ This project is a pre-release! The API is subject to change a little.

Simple example

package main

// A simple program that counts down from 5 and then exits.

import (
	"fmt"
	"log"
	"time"
	"github.com/charmbracelet/tea"
)

type model int

type tickMsg struct{}

func main() {
	p := tea.NewProgram(init, update, view, subscriptions)
	if err := p.Start(); err != nil {
		log.Fatal(err)
	}
}

// Listen for messages and update the model accordingly
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
	m, _ := mdl.(model)

	switch msg.(type) {
	case tickMsg:
        m--
		if m <= 0 {
			return m, tea.Quit
		}
	}
	return m, nil
}

// Render to the terminal
func view(mdl tea.Model) string {
	m, _ := mdl.(model)
	return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
}

// Subscribe to events
func subscriptions(_ tea.Model) tea.Subs {
    return tea.Subs{
        "tick": tick,
    }
}

func tick(_ tea.Model) tea.Msg {
	time.Sleep(time.Second)
	return tickMsg{}
}

Hungry for more? See the other examples.

Other Resources

  • Termenv: advanced ANSI style and color support for your terminal applications. Very useful when rendering your views.

Acknowledgments

Heavily inspired by both The Elm Architecture by Evan Czaplicki et al. and go-tea by TJ Holowaychuk.

License

MIT


Part of Charm. For more info see ssh charm.sh.

the Charm logo

Charm热爱开源!