main.go
This file
Path
examples/tabs/main.go
129 lines · 3.1 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
1package main23import (4 "fmt"5 "os"6 "strings"78 tea "charm.land/bubbletea/v2"9 "charm.land/lipgloss/v2"10)1112type styles struct {13 doc lipgloss.Style14 highlight lipgloss.Style15 inactiveTab lipgloss.Style16 activeTab lipgloss.Style17 window lipgloss.Style18}1920func newStyles(bgIsDark bool) *styles {21 lightDark := lipgloss.LightDark(bgIsDark)2223 inactiveTabBorder := tabBorderWithBottom("┴", "─", "┴")24 activeTabBorder := tabBorderWithBottom("┘", " ", "└")25 highlightColor := lightDark(lipgloss.Color("#874BFD"), lipgloss.Color("#7D56F4"))2627 s := new(styles)28 s.doc = lipgloss.NewStyle().29 Padding(1, 2, 1, 2)30 s.inactiveTab = lipgloss.NewStyle().31 Border(inactiveTabBorder, true).32 BorderForeground(highlightColor).33 Padding(0, 1)34 s.activeTab = s.inactiveTab.35 Border(activeTabBorder, true)36 s.window = lipgloss.NewStyle().37 BorderForeground(highlightColor).38 Padding(2, 0).39 Align(lipgloss.Center).40 Border(lipgloss.NormalBorder()).41 UnsetBorderTop()42 return s43}4445type model struct {46 Tabs []string47 TabContent []string48 styles *styles49 activeTab int50}5152func (m model) Init() tea.Cmd {53 return nil54}5556func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {57 switch msg := msg.(type) {58 case tea.KeyPressMsg:59 switch keypress := msg.String(); keypress {60 case "ctrl+c", "q":61 return m, tea.Quit62 case "right", "l", "n", "tab":63 m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)64 return m, nil65 case "left", "h", "p", "shift+tab":66 m.activeTab = max(m.activeTab-1, 0)67 return m, nil68 }69 }7071 return m, nil72}7374func tabBorderWithBottom(left, middle, right string) lipgloss.Border {75 border := lipgloss.RoundedBorder()76 border.BottomLeft = left77 border.Bottom = middle78 border.BottomRight = right79 return border80}8182func (m model) View() tea.View {83 if m.styles == nil {84 return tea.NewView("")85 }8687 doc := strings.Builder{}88 s := m.styles8990 var renderedTabs []string9192 for i, t := range m.Tabs {93 var style lipgloss.Style94 isFirst, isLast, isActive := i == 0, i == len(m.Tabs)-1, i == m.activeTab95 if isActive {96 style = s.activeTab97 } else {98 style = s.inactiveTab99 }100 border, _, _, _, _ := style.GetBorder()101 if isFirst && isActive {102 border.BottomLeft = "│"103 } else if isFirst && !isActive {104 border.BottomLeft = "├"105 } else if isLast && isActive {106 border.BottomRight = "│"107 } else if isLast && !isActive {108 border.BottomRight = "┤"109 }110 style = style.Border(border)111 renderedTabs = append(renderedTabs, style.Render(t))112 }113114 row := lipgloss.JoinHorizontal(lipgloss.Top, renderedTabs...)115 doc.WriteString(row)116 doc.WriteString("\n")117 doc.WriteString(s.window.Width((lipgloss.Width(row))).Render(m.TabContent[m.activeTab]))118 return tea.NewView(s.doc.Render(doc.String()))119}120121func main() {122 tabs := []string{"Lip Gloss", "Blush", "Eye Shadow", "Mascara", "Foundation"}123 tabContent := []string{"Lip Gloss Tab", "Blush Tab", "Eye Shadow Tab", "Mascara Tab", "Foundation Tab"}124 m := model{Tabs: tabs, TabContent: tabContent, styles: newStyles(true)} // default to dark styles.125 if _, err := tea.NewProgram(m).Run(); err != nil {126 fmt.Println("Error running program:", err)127 os.Exit(1)128 }129}