Files
u-tabs/internal/workspace.go
绝尘 a027fe1703 新增: u-tabs 初始版本
Go TUI 项目启动器,基于 bubbletea v2 + lipgloss v2。
支持分组 Tab、多选启动、编号跳转、Windows Terminal 集成。
2026-05-16 21:01:03 +08:00

81 lines
1.8 KiB
Go

package internal
import (
"fmt"
"os"
"path/filepath"
"charm.land/lipgloss/v2"
"u-tabs/internal/style"
)
// Workspace 工作空间定义
type Workspace struct {
Index int // 全局索引
N int // 编号
Title string // 短名
Prompt string // 描述
Tech string // 技术栈
Deploy string // 部署情况
Dir string // 目录路径
Group string // 分组标签
}
// Group 分组定义
type Group struct {
Label string
Desc string
}
// 运行时数据
var Groups []Group
var AllWorkspaces []Workspace
var wsByNum map[int]*Workspace
// NoConfig 配置文件不存在标记
var NoConfig bool
func InitData() {
cfg, err := LoadConfig()
if err != nil {
NoConfig = true
home, _ := os.UserHomeDir()
Groups = []Group{{Label: "HOME", Desc: "默认"}}
AllWorkspaces = []Workspace{
{Index: 0, N: 0, Title: "home", Prompt: "用户主目录", Tech: "-", Deploy: "本地", Dir: home, Group: "HOME"},
}
wsByNum = map[int]*Workspace{0: &AllWorkspaces[0]}
return
}
Groups, AllWorkspaces = cfg.ToInternal()
wsByNum = make(map[int]*Workspace, len(AllWorkspaces))
for i := range AllWorkspaces {
wsByNum[AllWorkspaces[i].N] = &AllWorkspaces[i]
}
}
func WorkspacesByGroup(groupLabel string) []Workspace {
var result []Workspace
for _, ws := range AllWorkspaces {
if ws.Group == groupLabel {
result = append(result, ws)
}
}
return result
}
func FindByNumber(n int) *Workspace {
return wsByNum[n]
}
// ConfigHint 配置文件提示文案
func ConfigHint() string {
if !NoConfig {
return ""
}
home, _ := os.UserHomeDir()
path := filepath.Join(home, ".u-tabs", "config.yaml")
return lipgloss.NewStyle().Foreground(style.Warning).Bold(true).Render(
fmt.Sprintf(" ⚠ 未找到配置文件,请创建: %s (参考 config.example.yaml)", path))
}