package timetable import ( "strings" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/mattn/go-runewidth" "tasksquire/common" "tasksquire/timewarrior" ) // Model defines a state for the table widget. type Model struct { common *common.Common KeyMap KeyMap cols []Column rows timewarrior.Intervals rowStyles []lipgloss.Style cursor int focus bool styles common.TableStyle styleFunc StyleFunc viewport viewport.Model start int end int } // Row represents one line in the table. type Row *timewarrior.Interval // Column defines the table structure. type Column struct { Title string Name string Width int MaxWidth int ContentWidth int } // KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which // is used to render the menu. type KeyMap struct { LineUp key.Binding LineDown key.Binding PageUp key.Binding PageDown key.Binding HalfPageUp key.Binding HalfPageDown key.Binding GotoTop key.Binding GotoBottom key.Binding } // ShortHelp implements the KeyMap interface. func (km KeyMap) ShortHelp() []key.Binding { return []key.Binding{km.LineUp, km.LineDown} } // FullHelp implements the KeyMap interface. func (km KeyMap) FullHelp() [][]key.Binding { return [][]key.Binding{ {km.LineUp, km.LineDown, km.GotoTop, km.GotoBottom}, {km.PageUp, km.PageDown, km.HalfPageUp, km.HalfPageDown}, } } // DefaultKeyMap returns a default set of keybindings. func DefaultKeyMap() KeyMap { const spacebar = " " return KeyMap{ LineUp: key.NewBinding( key.WithKeys("up", "k"), key.WithHelp("↑/k", "up"), ), LineDown: key.NewBinding( key.WithKeys("down", "j"), key.WithHelp("↓/j", "down"), ), PageUp: key.NewBinding( key.WithKeys("b", "pgup"), key.WithHelp("b/pgup", "page up"), ), PageDown: key.NewBinding( key.WithKeys("f", "pgdown", spacebar), key.WithHelp("f/pgdn", "page down"), ), HalfPageUp: key.NewBinding( key.WithKeys("u", "ctrl+u"), key.WithHelp("u", "½ page up"), ), HalfPageDown: key.NewBinding( key.WithKeys("d", "ctrl+d"), key.WithHelp("d", "½ page down"), ), GotoTop: key.NewBinding( key.WithKeys("home", "g"), key.WithHelp("g/home", "go to start"), ), GotoBottom: key.NewBinding( key.WithKeys("end", "G"), key.WithHelp("G/end", "go to end"), ), } } // SetStyles sets the table styles. func (m *Model) SetStyles(s common.TableStyle) { m.styles = s m.UpdateViewport() } // Option is used to set options in New. For example: // // table := New(WithColumns([]Column{{Title: "ID", Width: 10}})) type Option func(*Model) // New creates a new model for the table widget. func New(com *common.Common, opts ...Option) Model { m := Model{ common: com, cursor: 0, viewport: viewport.New(0, 20), KeyMap: DefaultKeyMap(), } for _, opt := range opts { opt(&m) } m.cols = m.parseColumns(m.cols) m.rowStyles = m.parseRowStyles(m.rows) m.UpdateViewport() return m } func (m *Model) parseRowStyles(rows timewarrior.Intervals) []lipgloss.Style { styles := make([]lipgloss.Style, len(rows)) if len(rows) == 0 { return styles } for i := range rows { // Default style styles[i] = m.common.Styles.Base.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding()) // If active, maybe highlight? if rows[i].IsActive() { if c, ok := m.common.Styles.Colors["active"]; ok && c != nil { styles[i] = c.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding()) } } } return styles } func (m *Model) parseColumns(cols []Column) []Column { if len(cols) == 0 { return cols } for i, col := range cols { for _, interval := range m.rows { col.ContentWidth = max(col.ContentWidth, lipgloss.Width(interval.GetString(col.Name))) } cols[i] = col } combinedSize := 0 nonZeroWidths := 0 tagIndex := -1 for i, col := range cols { if col.ContentWidth > 0 { col.Width = max(col.ContentWidth, lipgloss.Width(col.Title)) nonZeroWidths++ } if !strings.Contains(col.Name, "tags") { combinedSize += col.Width } else { tagIndex = i } cols[i] = col } if tagIndex >= 0 { cols[tagIndex].Width = m.Width() - combinedSize - nonZeroWidths } return cols } // WithColumns sets the table columns (headers). func WithColumns(cols []Column) Option { return func(m *Model) { m.cols = cols } } // WithRows sets the table rows (data). func WithIntervals(rows timewarrior.Intervals) Option { return func(m *Model) { m.rows = rows } } // WithHeight sets the height of the table. func WithHeight(h int) Option { return func(m *Model) { m.viewport.Height = h - lipgloss.Height(m.headersView()) } } // WithWidth sets the width of the table. func WithWidth(w int) Option { return func(m *Model) { m.viewport.Width = w } } // WithFocused sets the focus state of the table. func WithFocused(f bool) Option { return func(m *Model) { m.focus = f } } // WithStyles sets the table styles. func WithStyles(s common.TableStyle) Option { return func(m *Model) { m.styles = s } } // WithStyleFunc sets the table style func which can determine a cell style per column, row, and selected state. func WithStyleFunc(f StyleFunc) Option { return func(m *Model) { m.styleFunc = f } } // WithKeyMap sets the key map. func WithKeyMap(km KeyMap) Option { return func(m *Model) { m.KeyMap = km } } // Update is the Bubble Tea update loop. func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { if !m.focus { return m, nil } switch msg := msg.(type) { case tea.KeyMsg: switch { case key.Matches(msg, m.KeyMap.LineUp): m.MoveUp(1) case key.Matches(msg, m.KeyMap.LineDown): m.MoveDown(1) case key.Matches(msg, m.KeyMap.PageUp): m.MoveUp(m.viewport.Height) case key.Matches(msg, m.KeyMap.PageDown): m.MoveDown(m.viewport.Height) case key.Matches(msg, m.KeyMap.HalfPageUp): m.MoveUp(m.viewport.Height / 2) case key.Matches(msg, m.KeyMap.HalfPageDown): m.MoveDown(m.viewport.Height / 2) case key.Matches(msg, m.KeyMap.LineDown): m.MoveDown(1) case key.Matches(msg, m.KeyMap.GotoTop): m.GotoTop() case key.Matches(msg, m.KeyMap.GotoBottom): m.GotoBottom() } } return m, nil } // Focused returns the focus state of the table. func (m Model) Focused() bool { return m.focus } // Focus focuses the table, allowing the user to move around the rows and // interact. func (m *Model) Focus() { m.focus = true m.UpdateViewport() } // Blur blurs the table, preventing selection or movement. func (m *Model) Blur() { m.focus = false m.UpdateViewport() } // View renders the component. func (m Model) View() string { return m.headersView() + "\n" + m.viewport.View() } // UpdateViewport updates the list content based on the previously defined // columns and rows. func (m *Model) UpdateViewport() { renderedRows := make([]string, 0, len(m.rows)) if m.cursor >= 0 { m.start = clamp(m.cursor-m.viewport.Height, 0, m.cursor) } else { m.start = 0 } m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows)) for i := m.start; i < m.end; i++ { renderedRows = append(renderedRows, m.renderRow(i)) } m.viewport.SetContent( lipgloss.JoinVertical(lipgloss.Left, renderedRows...), ) } // SelectedRow returns the selected row. // Returns nil if cursor is on a gap row or out of bounds. func (m Model) SelectedRow() Row { if m.cursor < 0 || m.cursor >= len(m.rows) { return nil } // Don't return gap rows as selected if m.rows[m.cursor].IsGap { return nil } return m.rows[m.cursor] } // Rows returns the current rows. func (m Model) Rows() timewarrior.Intervals { return m.rows } // Columns returns the current columns. func (m Model) Columns() []Column { return m.cols } // SetRows sets a new rows state. func (m *Model) SetRows(r timewarrior.Intervals) { m.rows = r m.UpdateViewport() } // SetColumns sets a new columns state. func (m *Model) SetColumns(c []Column) { m.cols = c m.UpdateViewport() } // SetWidth sets the width of the viewport of the table. func (m *Model) SetWidth(w int) { m.viewport.Width = w m.UpdateViewport() } // SetHeight sets the height of the viewport of the table. func (m *Model) SetHeight(h int) { m.viewport.Height = h - lipgloss.Height(m.headersView()) m.UpdateViewport() } // Height returns the viewport height of the table. func (m Model) Height() int { return m.viewport.Height } // Width returns the viewport width of the table. func (m Model) Width() int { return m.viewport.Width } // Cursor returns the index of the selected row. func (m Model) Cursor() int { return m.cursor } // SetCursor sets the cursor position in the table. // Skips gap rows by moving to the nearest non-gap row. func (m *Model) SetCursor(n int) { m.cursor = clamp(n, 0, len(m.rows)-1) // Skip gap rows - try moving down first, then up if m.cursor < len(m.rows) && m.rows[m.cursor].IsGap { // Try moving down to find non-gap found := false for i := m.cursor; i < len(m.rows); i++ { if !m.rows[i].IsGap { m.cursor = i found = true break } } // If not found down, try moving up if !found { for i := m.cursor; i >= 0; i-- { if !m.rows[i].IsGap { m.cursor = i break } } } } m.UpdateViewport() } // MoveUp moves the selection up by any number of rows. // It can not go above the first row. Skips gap rows. func (m *Model) MoveUp(n int) { originalCursor := m.cursor m.cursor = clamp(m.cursor-n, 0, len(m.rows)-1) // Skip gap rows for m.cursor >= 0 && m.cursor < len(m.rows) && m.rows[m.cursor].IsGap { m.cursor-- } // If we went past the beginning, find the first non-gap row if m.cursor < 0 { for i := 0; i < len(m.rows); i++ { if !m.rows[i].IsGap { m.cursor = i break } } } // If no non-gap row found, restore original cursor if m.cursor < 0 || (m.cursor < len(m.rows) && m.rows[m.cursor].IsGap) { m.cursor = originalCursor } switch { case m.start == 0: m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor)) case m.start < m.viewport.Height: m.viewport.YOffset = (clamp(clamp(m.viewport.YOffset+n, 0, m.cursor), 0, m.viewport.Height)) case m.viewport.YOffset >= 1: m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, m.viewport.Height) } m.UpdateViewport() } // MoveDown moves the selection down by any number of rows. // It can not go below the last row. Skips gap rows. func (m *Model) MoveDown(n int) { originalCursor := m.cursor m.cursor = clamp(m.cursor+n, 0, len(m.rows)-1) // Skip gap rows for m.cursor >= 0 && m.cursor < len(m.rows) && m.rows[m.cursor].IsGap { m.cursor++ } // If we went past the end, find the last non-gap row if m.cursor >= len(m.rows) { for i := len(m.rows) - 1; i >= 0; i-- { if !m.rows[i].IsGap { m.cursor = i break } } } // If no non-gap row found, restore original cursor if m.cursor >= len(m.rows) || (m.cursor >= 0 && m.rows[m.cursor].IsGap) { m.cursor = originalCursor } m.UpdateViewport() switch { case m.end == len(m.rows) && m.viewport.YOffset > 0: m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height)) case m.cursor > (m.end-m.start)/2 && m.viewport.YOffset > 0: m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.cursor)) case m.viewport.YOffset > 1: case m.cursor > m.viewport.YOffset+m.viewport.Height-1: m.viewport.SetYOffset(clamp(m.viewport.YOffset+1, 0, 1)) } } // GotoTop moves the selection to the first row. func (m *Model) GotoTop() { m.MoveUp(m.cursor) } // GotoBottom moves the selection to the last row. func (m *Model) GotoBottom() { m.MoveDown(len(m.rows)) } // StyleFunc is a function that can be used to customize the style of a table cell based on the row and column index. type StyleFunc func(row, col int, value string) lipgloss.Style func (m Model) headersView() string { var s = make([]string, 0, len(m.cols)) for _, col := range m.cols { if col.Width <= 0 { continue } style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true) renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…")) s = append(s, m.styles.Header.Render(renderedCell)) } return lipgloss.JoinHorizontal(lipgloss.Left, s...) } func (m *Model) renderRow(r int) string { // Special rendering for gap rows if m.rows[r].IsGap { gapText := m.rows[r].GetString("gap_display") gapStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("240")). Align(lipgloss.Center). Width(m.Width()) return gapStyle.Render(gapText) } var s = make([]string, 0, len(m.cols)) for i, col := range m.cols { if m.cols[i].Width <= 0 { continue } var cellStyle lipgloss.Style cellStyle = m.rowStyles[r] if r == m.cursor { cellStyle = cellStyle.Inherit(m.styles.Selected) } style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true) renderedCell := cellStyle.Render(style.Render(runewidth.Truncate(m.rows[r].GetString(col.Name), m.cols[i].Width, "…"))) s = append(s, renderedCell) } row := lipgloss.JoinHorizontal(lipgloss.Left, s...) if r == m.cursor { return m.styles.Selected.Render(row) } return row } func max(a, b int) int { if a > b { return a } return b } func min(a, b int) int { if a < b { return a } return b } func clamp(v, low, high int) int { return min(max(v, low), high) }