Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions examples/tabs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type model struct {
Tabs []string
TabContent []string
activeTab int
width int
}

func (m model) Init() tea.Cmd {
Expand All @@ -21,6 +22,9 @@ func (m model) Init() tea.Cmd {

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
return m, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c", "q":
Expand Down Expand Up @@ -60,6 +64,29 @@ func (m model) View() string {

var renderedTabs []string

// Calculate the maximum tab length to ensure consistent spacing
maxLen := 0
for _, t := range m.Tabs {
if len(t) > maxLen {
maxLen = len(t)
}
}

// Calculate available width for tabs
totalTabWidth := (maxLen + 2) * len(m.Tabs) // +2 for borders
availableWidth := m.width - docStyle.GetHorizontalFrameSize()

// If tabs don't fit, reduce tab width proportionally
if totalTabWidth > availableWidth && availableWidth > 0 {
tabWidth := availableWidth / len(m.Tabs)
if tabWidth < maxLen+2 {
maxLen = tabWidth - 2
if maxLen < 1 {
maxLen = 1
}
}
}

for i, t := range m.Tabs {
var style lipgloss.Style
isFirst, isLast, isActive := i == 0, i == len(m.Tabs)-1, i == m.activeTab
Expand All @@ -78,7 +105,7 @@ func (m model) View() string {
} else if isLast && !isActive {
border.BottomRight = "┤"
}
style = style.Border(border)
style = style.Border(border).Width(maxLen + 2) // Add padding for borders
renderedTabs = append(renderedTabs, style.Render(t))
}

Expand All @@ -92,8 +119,8 @@ func (m model) View() string {
func main() {
tabs := []string{"Lip Gloss", "Blush", "Eye Shadow", "Mascara", "Foundation"}
tabContent := []string{"Lip Gloss Tab", "Blush Tab", "Eye Shadow Tab", "Mascara Tab", "Foundation Tab"}
m := model{Tabs: tabs, TabContent: tabContent}
if _, err := tea.NewProgram(m).Run(); err != nil {
m := model{Tabs: tabs, TabContent: tabContent, width: 80} // Default width
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down