Bubbletea
概述
bubbletea是一个简单、小巧、可以非常方便地用来编写 TUI(terminal User Interface,控制台界面程序)程序的框架。内置简单的事件处理机制,可以对外部事件做出响应,如键盘按键。
依赖导入
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
)
快速开始
应用状态模型
type model struct {
choices []string // items on the to-do list,所有待完成事项;
cursor int // which to-do list item our cursor is pointing at,界面上光标位置;
selected map[int]struct{} // which to-do items are selected,已完成标识。
}
model用于存储Bubbletea应用程序的状态信息(该类型被我们自主创建,里面的内容由我们自行定义)
初始化接口
实现
func initialModel() model {
return model{
// Our to-do list is a grocery list
choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},
// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
}
}
func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
Init()方法在程序启动时会立刻调用,它会做一些初始化工作,并返回**一个可以执行一些初始的I/O的Cmd**告诉bubbletea要执行什么命令;
tea.cmd
Cmd是一个IO操作,当它完成时返回一条消息。如果它是nil,它被认为是一个无操作。它被用于HTTP请求、计时器、从磁盘保存和加载等等。
请注意,几乎没有理由使用命令向程序的另一部分发送消息。这几乎总是可以在更新函数中完成。
package tea
type Cmd func() Msg
更新接口
实现
更新函数在“事情发生”时被调用。它的任务是查看发生了什么并返回一个更新的模型作为响应。它还可以返回 一个Cmd让更多的事情发生
在下面的例子中,ctrl+c 和 q 会返回tea.Quit带有模型的命令。这是一个特殊的命令,它指示 Bubble Tea 运行时退出,退出程序。
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
// The "enter" key and the spacebar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]
if ok {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}
tea.Quit
tea.Quit是一个特殊的命令,它告诉Bubble Tea程序退出
package tea
func Quit() Msg {
return quitMsg{}
}
视图接口
现在要渲染 UI 了。在所有方法中,视图是最简单的。我们查看模型的当前状态并使用它返回一个string. 该字符串是我们的 UI!
因为视图描述了应用程序的整个 UI,所以不必担心重绘逻辑和类似的事情。Bubble Tea 会为您解决。
func (m model) View() string {
// The header
s := "What should we buy at the market?\n\n"
// Iterate over our choices
for i, choice := range m.choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Is this choice selected?
checked := " " // not selected
if _, ok := m.selected[i]; ok {
checked = "x" // selected!
}
// Render the row
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
// The footer
s += "\nPress q to quit.\n"
// Send the UI for rendering
return s
}
启动
最后一步是简单地运行我们的程序。我们将我们的初始模型传递给 tea.NewProgram并让它撕裂:
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}