From 5611ef8b28216aa9dd2ceb6ed17d5779a29154f6 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 21 Jul 2025 19:10:51 -0400 Subject: wip: vscode extension --- packages/tui/internal/components/ide/ide.go | 112 ++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 packages/tui/internal/components/ide/ide.go (limited to 'packages/tui/internal/components') diff --git a/packages/tui/internal/components/ide/ide.go b/packages/tui/internal/components/ide/ide.go new file mode 100644 index 000000000..cb10f0fc9 --- /dev/null +++ b/packages/tui/internal/components/ide/ide.go @@ -0,0 +1,112 @@ +package ide + +import ( + "fmt" + "strings" + + tea "github.com/charmbracelet/bubbletea/v2" + "github.com/charmbracelet/lipgloss/v2" + "github.com/charmbracelet/lipgloss/v2/compat" + "github.com/sst/opencode/internal/styles" + "github.com/sst/opencode/internal/theme" +) + +type IdeComponent interface { + tea.ViewModel + SetSize(width, height int) tea.Cmd + SetBackgroundColor(color compat.AdaptiveColor) +} + +type ideComponent struct { + width, height int + background *compat.AdaptiveColor +} + +func (c *ideComponent) SetSize(width, height int) tea.Cmd { + c.width = width + c.height = height + return nil +} + +func (c *ideComponent) SetBackgroundColor(color compat.AdaptiveColor) { + c.background = &color +} + +func (c *ideComponent) View() string { + t := theme.CurrentTheme() + + triggerStyle := styles.NewStyle().Foreground(t.Primary()).Bold(true) + descriptionStyle := styles.NewStyle().Foreground(t.Text()) + + if c.background != nil { + triggerStyle = triggerStyle.Background(*c.background) + descriptionStyle = descriptionStyle.Background(*c.background) + } + + // VSCode shortcuts data + shortcuts := []struct { + shortcut string + description string + }{ + {"Cmd+Esc", "open opencode in VS Code"}, + {"Cmd+Opt+K", "insert file from VS Code"}, + } + + // Calculate column widths + maxShortcutWidth := 0 + maxDescriptionWidth := 0 + + for _, shortcut := range shortcuts { + if len(shortcut.shortcut) > maxShortcutWidth { + maxShortcutWidth = len(shortcut.shortcut) + } + if len(shortcut.description) > maxDescriptionWidth { + maxDescriptionWidth = len(shortcut.description) + } + } + + // Add padding between columns + columnPadding := 3 + + // Build the output + var output strings.Builder + + maxWidth := 0 + for _, shortcut := range shortcuts { + // Pad each column to align properly + shortcutText := fmt.Sprintf("%-*s", maxShortcutWidth, shortcut.shortcut) + description := fmt.Sprintf("%-*s", maxDescriptionWidth, shortcut.description) + + // Apply styles and combine + line := triggerStyle.Render(shortcutText) + + triggerStyle.Render(strings.Repeat(" ", columnPadding)) + + descriptionStyle.Render(description) + + output.WriteString(line + "\n") + maxWidth = max(maxWidth, lipgloss.Width(line)) + } + + // Remove trailing newline + result := strings.TrimSuffix(output.String(), "\n") + if c.background != nil { + result = styles.NewStyle().Background(*c.background).Width(maxWidth).Render(result) + } + + return result +} + +type Option func(*ideComponent) + +func WithBackground(background compat.AdaptiveColor) Option { + return func(c *ideComponent) { + c.background = &background + } +} + +func New(opts ...Option) IdeComponent { + c := &ideComponent{} + for _, opt := range opts { + opt(c) + } + return c +} -- cgit v1.2.3