BubbleTeaTUI脚手架工具 bubble-tea-scaffolder

这是一个用于快速生成基于Go语言Bubble Tea框架的终端用户界面(TUI)应用程序的脚手架工具。它采用Elm架构模式,自动创建项目结构、模型定义、命令处理、视图渲染和样式配置,帮助开发者快速构建交互式命令行界面。关键词:Go TUI开发, Bubble Tea脚手架, 终端界面生成, Elm架构, 命令行工具开发, 交互式CLI

后端开发 0 次安装 0 次浏览 更新于 2/23/2026

名称: bubble-tea-scaffolder 描述: 使用Elm架构生成Bubble Tea(Go)TUI应用程序结构,包括模型、命令和视图。 允许工具: 读取, 写入, 编辑, Bash, Glob, Grep

Bubble Tea 脚手架工具

使用Go和Elm架构生成Bubble Tea TUI应用程序。

功能

  • 生成Bubble Tea项目结构
  • 创建包含Init、Update、View的模型
  • 设置命令和消息
  • 实现组件组合
  • 使用Lip Gloss创建样式
  • 设置测试模式

使用场景

在以下情况调用此技能:

  • 使用Go构建终端用户界面
  • 使用Elm架构创建交互式CLI
  • 实现复杂的TUI应用程序
  • 设置Bubble Tea项目结构

输入参数

参数 类型 是否必需 描述
projectName 字符串 项目名称
modulePath 字符串 Go模块路径
components 数组 组件定义

生成模式

主应用程序

package main

import (
    "fmt"
    "os"

    tea "github.com/charmbracelet/bubbletea"
)

func main() {
    p := tea.NewProgram(initialModel(), tea.WithAltScreen())
    if _, err := p.Run(); err != nil {
        fmt.Printf("错误: %v", err)
        os.Exit(1)
    }
}

模型定义

package main

import (
    "github.com/charmbracelet/bubbles/list"
    "github.com/charmbracelet/bubbles/textinput"
    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/lipgloss"
)

type model struct {
    list       list.Model
    textInput  textinput.Model
    err        error
    quitting   bool
}

func initialModel() model {
    ti := textinput.New()
    ti.Placeholder = "输入搜索词..."
    ti.Focus()

    items := []list.Item{
        item{title: "选项1", desc: "第一个选项"},
        item{title: "选项2", desc: "第二个选项"},
    }

    l := list.New(items, list.NewDefaultDelegate(), 0, 0)
    l.Title = "选择一个选项"

    return model{
        textInput: ti,
        list:      l,
    }
}

func (m model) Init() tea.Cmd {
    return textinput.Blink
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.String() {
        case "ctrl+c", "q":
            m.quitting = true
            return m, tea.Quit
        case "enter":
            // 处理选择
            return m, nil
        }
    case tea.WindowSizeMsg:
        m.list.SetSize(msg.Width, msg.Height-4)
    }

    var cmd tea.Cmd
    m.list, cmd = m.list.Update(msg)
    return m, cmd
}

func (m model) View() string {
    if m.quitting {
        return "再见!
"
    }

    return lipgloss.JoinVertical(
        lipgloss.Left,
        m.textInput.View(),
        m.list.View(),
    )
}

列表项

package main

type item struct {
    title string
    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 }

样式

package main

import "github.com/charmbracelet/lipgloss"

var (
    titleStyle = lipgloss.NewStyle().
        Bold(true).
        Foreground(lipgloss.Color("205")).
        MarginBottom(1)

    selectedStyle = lipgloss.NewStyle().
        Foreground(lipgloss.Color("170")).
        Bold(true)

    normalStyle = lipgloss.NewStyle().
        Foreground(lipgloss.Color("252"))

    helpStyle = lipgloss.NewStyle().
        Foreground(lipgloss.Color("241"))
)

依赖项

require (
    github.com/charmbracelet/bubbletea v0.25.0
    github.com/charmbracelet/bubbles v0.17.0
    github.com/charmbracelet/lipgloss v0.9.0
)

目标流程

  • tui-application-framework
  • dashboard-monitoring-tui
  • interactive-form-implementation