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
Provenance
- Commit
6faba76- Tree
199f16e- Author
Sebastian Spaink <sspaink@influxdata.com>- Committer
Maas Lalani <maas@lalani.dev>- Parent
9b459e5
1 file changed+116 added−0 removed
added:examples/tabs/main.go+116 added
1+package main2+3+import (4+ "fmt"5+ "os"6+ "strings"7+8+ tea "github.com/charmbracelet/bubbletea"9+ "github.com/charmbracelet/lipgloss"10+)11+12+type model struct {13+ Tabs []string14+ TabContent []string15+ activeTab int16+}17+18+func (m model) Init() tea.Cmd {19+ return nil20+}21+22+func (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+ }36+37+ return m, nil38+}39+40+func tabBorderWithBottom(left, middle, right string) lipgloss.Border {41+ border := lipgloss.RoundedBorder()42+ border.BottomLeft = left43+ border.Bottom = middle44+ border.BottomRight = right45+ return border46+}47+48+var (49+ defaultWidth = 2050+51+ 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+)59+60+func (m model) View() string {61+ doc := strings.Builder{}62+63+ var renderedTabs []string64+65+ 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+ }86+87+ 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+}93+94+func 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+}103+104+func max(a, b int) int {105+ if a > b {106+ return a107+ }108+ return b109+}110+111+func min(a, b int) int {112+ if a < b {113+ return a114+ }115+ return b116+}