A powerful little TUI framework 🏗
Go to file
2020-05-11 17:38:05 -04:00
.github/workflows Add GitHub workflow 2020-01-25 07:17:37 +01:00
examples Update spinner example so it works with the spinner version in master 2020-05-11 17:38:05 -04:00
.gitignore Ignore .envrc 2020-04-22 09:53:18 -04:00
go.mod Bump termenv with fix for redraws on mosh, konsole 2020-04-30 19:29:59 -04:00
go.sum Bump termenv with fix for redraws on mosh, konsole 2020-04-30 19:29:59 -04:00
key.go Add TODO + comment 2020-02-19 22:23:54 -05:00
LICENSE Add MIT license 2020-01-24 15:07:54 -05:00
logging.go syslog is only available on unix systems 2020-01-25 07:15:56 +01:00
README.md Update example in README to use tea.Every 2020-05-11 12:54:44 -04:00
subscriptions.go Add Tick to run timers independent of the system clock 2020-05-11 13:01:42 -04:00
tea.go Subs shouldn't take a model as an argument 2020-05-04 18:52:44 -04:00
tty_unix.go Add missing error handling 2020-04-17 20:35:54 +02:00
tty_windows.go Move ansi helpers to termenv 2020-01-31 13:52:20 +01:00

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 time.Time

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": time.Every(time.Second, func(t time.Time) tea.Msg {
            return tickMsg(t)
        },
    }
}

Hungry for more? See the other examples.

Other Resources

  • Tea Party: a collection of Tea components.
  • Termenv: advanced ANSI style and color support for your terminal applications. Very useful when rendering your views.
  • Reflow: a collection of ANSI-aware text formatting tools. Also useful for view rendering.

Acknowledgments

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

License

MIT


Part of Charm.

the Charm logo

Charm热爱开源!