-
Notifications
You must be signed in to change notification settings - Fork 198
/
tabs.go
76 lines (63 loc) · 1.91 KB
/
tabs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package tabs
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dlvhdr/gh-dash/v4/config"
"github.com/dlvhdr/gh-dash/v4/ui/components/section"
"github.com/dlvhdr/gh-dash/v4/ui/context"
"github.com/dlvhdr/gh-dash/v4/utils"
)
type Model struct {
sectionsConfigs []config.SectionConfig
sectionCounts []*int
CurrSectionId int
}
func NewModel(ctx *context.ProgramContext) Model {
return Model{
CurrSectionId: 1,
}
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}
func (m Model) View(ctx context.ProgramContext) string {
sectionTitles := make([]string, 0, len(m.sectionsConfigs))
for i, section := range m.sectionsConfigs {
title := section.Title
// handle search section
if i > 0 && m.sectionCounts[i] != nil && ctx.Config.Theme.Ui.SectionsShowCount {
title = fmt.Sprintf("%s (%s)", title, utils.ShortNumber(*m.sectionCounts[i]))
}
sectionTitles = append(sectionTitles, title)
}
var tabs []string
for i, sectionTitle := range sectionTitles {
if m.CurrSectionId == i {
tabs = append(tabs, ctx.Styles.Tabs.ActiveTab.Render(sectionTitle))
} else {
tabs = append(tabs, ctx.Styles.Tabs.Tab.Render(sectionTitle))
}
}
renderedTabs := lipgloss.NewStyle().
Width(ctx.ScreenWidth).
MaxWidth(ctx.ScreenWidth).
Render(lipgloss.JoinHorizontal(lipgloss.Top, strings.Join(tabs, ctx.Styles.Tabs.TabSeparator.Render("|"))))
return ctx.Styles.Tabs.TabsRow.Copy().
Width(ctx.ScreenWidth).
MaxWidth(ctx.ScreenWidth).
Render(renderedTabs)
}
func (m *Model) SetCurrSectionId(id int) {
m.CurrSectionId = id
}
func (m *Model) UpdateSectionsConfigs(ctx *context.ProgramContext) {
m.sectionsConfigs = ctx.GetViewSectionsConfig()
}
func (m *Model) UpdateSectionCounts(sections []section.Section) {
m.sectionCounts = make([]*int, len(sections))
for i, s := range sections {
m.sectionCounts[i] = s.GetTotalCount()
}
}