Skip to content
Myr

docs: example interacting with tabs using lipgloss

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastian Spaink · committed by Maas Lalani · parent 9b459e5 · Browse files · 1 file+116 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+116 added−0 removed

  • other:+116 added−0 removed

1 file+116 added−0 removed

added:examples/tabs/main.go+116 added
1package main23import (4 "fmt"5 "os"6 "strings"78 tea "github.com/charmbracelet/bubbletea"9 "github.com/charmbracelet/lipgloss"10)1112type model struct {13 Tabs []string14 TabContent []string15 activeTab int16}1718func (m model) Init() tea.Cmd {19 return nil20}2122func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {23 switch msg := msg.(type) {24 case tea.KeyMsg:25 switch keypress := msg.String(); keypress {26 case "ctrl+c", "q":27 return m, tea.Quit28 case "right", "l", "n", "tab":29 m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)30 return m, nil31 case "left", "h", "p", "shift+tab":32 m.activeTab = max(m.activeTab-1, 0)33 return m, nil34 }35 }3637 return m, nil38}3940func tabBorderWithBottom(left, middle, right string) lipgloss.Border {41 border := lipgloss.RoundedBorder()42 border.BottomLeft = left43 border.Bottom = middle44 border.BottomRight = right45 return border46}4748var (49 defaultWidth = 205051 inactiveTabBorder = tabBorderWithBottom("┴", "─", "┴")52 activeTabBorder = tabBorderWithBottom("┘", " ", "└")53 docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)54 highlightColor = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}55 inactiveTabStyle = lipgloss.NewStyle().Border(inactiveTabBorder, true).BorderForeground(highlightColor).Padding(0, 1)56 activeTabStyle = inactiveTabStyle.Copy().Border(activeTabBorder, true)57 windowStyle = lipgloss.NewStyle().BorderForeground(highlightColor).Padding(2, 0).Align(lipgloss.Center).Border(lipgloss.NormalBorder()).UnsetBorderTop()58)5960func (m model) View() string {61 doc := strings.Builder{}6263 var renderedTabs []string6465 for i, t := range m.Tabs {66 var style lipgloss.Style67 isFirst, isLast, isActive := i == 0, i == len(m.Tabs)-1, i == m.activeTab68 if isActive {69 style = activeTabStyle.Copy()70 } else {71 style = inactiveTabStyle.Copy()72 }73 border, _, _, _, _ := style.GetBorder()74 if isFirst && isActive {75 border.BottomLeft = "│"76 } else if isFirst && !isActive {77 border.BottomLeft = "├"78 } else if isLast && isActive {79 border.BottomRight = "│"80 } else if isLast && !isActive {81 border.BottomRight = "┤"82 }83 style = style.Border(border)84 renderedTabs = append(renderedTabs, style.Render(t))85 }8687 row := lipgloss.JoinHorizontal(lipgloss.Top, renderedTabs...)88 doc.WriteString(row)89 doc.WriteString("\n")90 doc.WriteString(windowStyle.Width((lipgloss.Width(row) - windowStyle.GetHorizontalFrameSize())).Render(m.TabContent[m.activeTab]))91 return docStyle.Render(doc.String())92}9394func main() {95 tabs := []string{"Lip Gloss", "Blush", "Eye Shadow", "Mascara", "Foundation"}96 tabContent := []string{"Lip Gloss Tab", "Blush Tab", "Eye Shadow Tab", "Mascara Tab", "Foundation Tab"}97 m := model{Tabs: tabs, TabContent: tabContent}98 if err := tea.NewProgram(m).Start(); err != nil {99 fmt.Println("Error running program:", err)100 os.Exit(1)101 }102}103104func max(a, b int) int {105 if a > b {106 return a107 }108 return b109}110111func min(a, b int) int {112 if a < b {113 return a114 }115 return b116}