Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–7why, one step back
1package main
23import (4 "fmt"5 "os"6 "strings"7

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 8–9why, one step back
8 tea "charm.land/bubbletea/v2"
9 "charm.land/lipgloss/v2"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 10–11why, one step back
10)
11

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 12–44why, one step back
12type 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}44

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 45–47why, one step back
45type model struct {
46 Tabs []string47 TabContent []string

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 48why, one step back
48 styles *styles

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 49–57why, one step back
49 activeTab int
50}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) {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 58why, one step back
58 case tea.KeyPressMsg:

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 59–81why, one step back
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}81

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 82–85why, one step back
82func (m model) View() tea.View {
83 if m.styles == nil {84 return tea.NewView("")85 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 86–87why, one step back
86
87 doc := strings.Builder{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 88why, one step back
88 s := m.styles

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 89–95why, one step back
89
90 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 {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 96why, one step back
96 style = s.activeTab

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 97why, one step back
97 } else {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 98why, one step back
98 style = s.inactiveTab

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 99–116why, one step back
99 }
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")

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 117–118why, one step back
117 doc.WriteString(s.window.Width((lipgloss.Width(row))).Render(m.TabContent[m.activeTab]))
118 return tea.NewView(s.doc.Render(doc.String()))

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 119–123why, one step back
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"}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 124why, one step back
124 m := model{Tabs: tabs, TabContent: tabContent, styles: newStyles(true)} // default to dark styles.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 125–129why, one step back
125 if _, err := tea.NewProgram(m).Run(); err != nil {
126 fmt.Println("Error running program:", err)127 os.Exit(1)128 }129}