ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > https://github.com/charmbracelet/bubbletea ## 概述 ![](https://camo.githubusercontent.com/11ddd998187e4c8dca87e1c1f98d8e4619663ba5250257db1b895b0ebb08d93a/68747470733a2f2f73747566662e636861726d2e73682f627562626c657465612f627562626c657465612d6578616d706c652e676966) ## 示例 ``` package main import ( "fmt" "os" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" ) var docStyle = lipgloss.NewStyle().Margin(1, 2) type item struct { title, desc string } func (i item) Title() string { return i.title } func (i item) Description() string { return i.desc } func (i item) FilterValue() string { return i.title } type model struct { list list.Model } func (m model) Init() tea.Cmd { return nil } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: if msg.String() == "ctrl+c" { return m, tea.Quit } case tea.WindowSizeMsg: h, v := docStyle.GetFrameSize() m.list.SetSize(msg.Width-h, msg.Height-v) } var cmd tea.Cmd m.list, cmd = m.list.Update(msg) return m, cmd } func (m model) View() string { return docStyle.Render(m.list.View()) } func main() { items := []list.Item{ item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"}, item{title: "Nutella", desc: "It's good on toast"}, item{title: "Bitter melon", desc: "It cools you down"}, item{title: "Nice socks", desc: "And by that I mean socks without holes"}, item{title: "Eight hours of sleep", desc: "I had this once"}, item{title: "Cats", desc: "Usually"}, item{title: "Plantasia, the album", desc: "My plants love it too"}, item{title: "Pour over coffee", desc: "It takes forever to make though"}, item{title: "VR", desc: "Virtual reality...what is there to say?"}, item{title: "Noguchi Lamps", desc: "Such pleasing organic forms"}, item{title: "Linux", desc: "Pretty much the best OS"}, item{title: "Business school", desc: "Just kidding"}, item{title: "Pottery", desc: "Wet clay is a great feeling"}, item{title: "Shampoo", desc: "Nothing like clean hair"}, } m := model{list: list.New(items, list.NewDefaultDelegate(), 0, 0)} m.list.Title = "My Fave Things" p := tea.NewProgram(m, tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Println("Error running program:", err) os.Exit(1) } } ```