// Package tui holds skm's bubbletea views: the skill picker, the update review, // and the dashboard that hosts both. All state changes go through internal/core. package tui import ( "context" "errors" "os" "strings" tea "charm.land/bubbletea/v2" "charm.land/lipgloss/v2" "github.com/charmbracelet/x/ansi" "github.com/charmbracelet/x/term" ) // ErrCanceled is returned when the user backs out of a picker or review. var ErrCanceled = errors.New("canceled") // Interactive reports whether stdin and stdout are a terminal. func Interactive() bool { return term.IsTerminal(os.Stdin.Fd()) && term.IsTerminal(os.Stdout.Fd()) } // component is a view that can run standalone or inside the dashboard. type component interface { update(tea.Msg) tea.Cmd view() string resize(width, height int) finished() bool } // runner runs one component as a full-screen program until it finishes. type runner struct{ c component } func (r runner) Init() tea.Cmd { return nil } func (r runner) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if size, ok := msg.(tea.WindowSizeMsg); ok { r.c.resize(size.Width, size.Height) return r, nil } cmd := r.c.update(msg) if r.c.finished() { return r, tea.Quit } return r, cmd } func (r runner) View() tea.View { v := tea.NewView(r.c.view()) v.AltScreen = true return v } func run(ctx context.Context, c component) error { c.resize(80, 24) _, err := tea.NewProgram(runner{c}, tea.WithContext(ctx)).Run() return err } var ( bold = lipgloss.NewStyle().Bold(true) dim = lipgloss.NewStyle().Faint(true) green = lipgloss.NewStyle().Foreground(lipgloss.Color("2")) red = lipgloss.NewStyle().Foreground(lipgloss.Color("1")) yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("3")) cyan = lipgloss.NewStyle().Foreground(lipgloss.Color("6")) selected = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("5")) ) // colorDiff styles a unified diff line by line. func colorDiff(d string) string { lines := strings.Split(strings.TrimRight(d, "\n"), "\n") for i, l := range lines { switch { case strings.HasPrefix(l, "diff "), strings.HasPrefix(l, "--- "), strings.HasPrefix(l, "+++ "): lines[i] = bold.Render(l) case strings.HasPrefix(l, "@@"): lines[i] = cyan.Render(l) case strings.HasPrefix(l, "+"): lines[i] = green.Render(l) case strings.HasPrefix(l, "-"): lines[i] = red.Render(l) } } return strings.Join(lines, "\n") } // window returns the [start, end) slice of n rows that keeps cursor visible in height rows. func window(n, cursor, height int) (int, int) { start := max(0, cursor-max(height, 1)+1) return start, min(n, start+max(height, 1)) } // pad right-pads s with spaces to width cells, ignoring escape codes. func pad(s string, width int) string { return s + strings.Repeat(" ", max(0, width-ansi.StringWidth(s))) } // fit truncates each line to width and pads or cuts the block to height lines. func fit(s string, width, height int) string { lines := strings.Split(s, "\n") if len(lines) > height { lines = lines[:height] } for i, l := range lines { lines[i] = ansi.Truncate(l, width, "…") } for len(lines) < height { lines = append(lines, "") } return strings.Join(lines, "\n") }