feat: wip tui updates

This commit is contained in:
cato 2025-12-07 11:42:57 +01:00
parent 5d7e9d79c4
commit dde2d98a84
5 changed files with 158 additions and 24 deletions

View file

@ -9,13 +9,15 @@ import (
type (
HeaderModel struct{
Size Size
Tabs []HeaderTabModel
Width int
}
HeaderTabModel struct{
Current bool
Size Size
Active bool
Name string
Key rune
Unread int
}
)
@ -23,16 +25,16 @@ type (
func NewHeader() HeaderModel {
return HeaderModel{
Tabs: []HeaderTabModel {
NewHeaderTab("Home", 10, true),
NewHeaderTab("Notifications", 3, false),
NewHeaderTab("Lists", 5, false),
NewHeaderTab("Private", 0, false),
NewHeaderTab("Home", 'H', 10, true),
NewHeaderTab("Notifications", 'N', 3, false),
NewHeaderTab("Lists", 'L', 5, false),
NewHeaderTab("Private", 'P', 0, false),
},
}
}
func NewHeaderTab(name string, unread int, current bool) HeaderTabModel {
return HeaderTabModel{ Name: name, Unread: unread, Current: current }
func NewHeaderTab(name string, key rune, unread int, current bool) HeaderTabModel {
return HeaderTabModel{ Name: name, Key: key, Unread: unread, Active: current }
}
var (
@ -59,6 +61,7 @@ var (
}
headerTabStyle = lipgloss.NewStyle().
Padding(0, 1).
Border(tabBorder, true)
activeHeaderTabStyle = headerTabStyle.
@ -72,43 +75,80 @@ func (m HeaderModel) Init() tea.Cmd {
func (m HeaderModel) Update(msg tea.Msg) (HeaderModel, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case Size:
m.Width = msg.Width
switch msg.(type) {
case SwitchHomeScreenMsg:
m.SetActive(0)
case SwitchNotificationScreenMsg:
m.SetActive(1)
}
return m, cmd
}
func (m *HeaderModel) SetActive(tabIndex int) {
for i := range m.Tabs {
m.Tabs[i].Active = i == tabIndex
}
}
func (m *HeaderModel) SetWidth(width int) {
tabWidth := width / len(m.Tabs)
var maxTabHeight int
for i := range m.Tabs {
m.Tabs[i].SetSize(tabWidth)
maxTabHeight = max(maxTabHeight, m.Tabs[i].Size.Height)
}
m.Size = Size{
Width: width,
Height: 3+maxTabHeight,
}
}
func (m HeaderModel) View() string {
header := lipgloss.NewStyle().
Padding(1, 0).
Align(lipgloss.Center).
Width(m.Width)
Width(m.Size.Width)
renderedTabs := make([]string, len(m.Tabs))
for i, tab := range m.Tabs {
renderedTabs[i] = tab.View()
}
joinedTabs := lipgloss.JoinHorizontal(lipgloss.Bottom, renderedTabs...)
return header.Render(renderedTabs...)
return header.Render(joinedTabs)
}
func (m HeaderTabModel) Init() tea.Cmd {
return nil
}
func (m HeaderTabModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m HeaderTabModel) Update(msg tea.Msg) (HeaderTabModel, tea.Cmd) {
var cmd tea.Cmd
return m, cmd
}
func (m HeaderTabModel) View() string {
content := lipgloss.JoinHorizontal(lipgloss.Center, m.Name, fmt.Sprintf("(%d)", m.Unread))
func (m *HeaderTabModel) SetSize(width int) {
m.Size = Size{
Width: width,
Height: 1,
}
}
if m.Current {
return lipgloss.NewStyle().Underline(true).Render(content)
func (m HeaderTabModel) View() string {
contentWidth := m.Size.Width - 4
content := fmt.Sprintf("[%c] %s - %d", m.Key, m.Name, m.Unread)
if len(content) > contentWidth {
content = fmt.Sprintf("[%c] %d", m.Key, m.Unread)
if len(content) > contentWidth {
content = fmt.Sprintf("[%c]", m.Key)
}
}
content = lipgloss.PlaceHorizontal(contentWidth, lipgloss.Center, content)
if m.Active {
return activeHeaderTabStyle.Render(content)
}
return content
return headerTabStyle.Render(content)
}