Fix table formatting
This commit is contained in:
@ -2,6 +2,7 @@ package table
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
@ -23,7 +24,7 @@ type Model struct {
|
||||
rowStyles []lipgloss.Style
|
||||
cursor int
|
||||
focus bool
|
||||
styles Styles
|
||||
styles common.TableStyle
|
||||
styleFunc StyleFunc
|
||||
|
||||
viewport viewport.Model
|
||||
@ -108,25 +109,8 @@ func DefaultKeyMap() KeyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Styles contains style definitions for this list component. By default, these
|
||||
// values are generated by DefaultStyles.
|
||||
type Styles struct {
|
||||
Header lipgloss.Style
|
||||
Cell lipgloss.Style
|
||||
Selected lipgloss.Style
|
||||
}
|
||||
|
||||
// DefaultStyles returns a set of default style definitions for this table.
|
||||
func DefaultStyles() Styles {
|
||||
return Styles{
|
||||
Selected: lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212")),
|
||||
Header: lipgloss.NewStyle().Bold(true).Padding(0, 1),
|
||||
Cell: lipgloss.NewStyle().Padding(0, 1),
|
||||
}
|
||||
}
|
||||
|
||||
// SetStyles sets the table styles.
|
||||
func (m *Model) SetStyles(s Styles) {
|
||||
func (m *Model) SetStyles(s common.TableStyle) {
|
||||
m.styles = s
|
||||
m.UpdateViewport()
|
||||
}
|
||||
@ -139,11 +123,11 @@ 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(),
|
||||
styles: DefaultStyles(),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@ -158,9 +142,75 @@ func New(com *common.Common, opts ...Option) Model {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Model) parseRowStyles()
|
||||
// TODO: dynamically read rule.precedence.color
|
||||
func (m *Model) parseRowStyles(rows taskwarrior.Tasks) []lipgloss.Style {
|
||||
styles := make([]lipgloss.Style, len(rows))
|
||||
if len(rows) == 0 {
|
||||
return styles
|
||||
}
|
||||
for i, task := range rows {
|
||||
if task.Status == "deleted" {
|
||||
styles[i] = m.common.Styles.Deleted.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if task.Status == "completed" {
|
||||
styles[i] = m.common.Styles.Completed.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if task.Status == "pending" && task.Start != "" {
|
||||
styles[i] = m.common.Styles.Active.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
// TODO: implement keyword
|
||||
// TODO: implement tag
|
||||
// TODO: implement project
|
||||
if !task.GetDate("due").IsZero() && task.GetDate("due").Before(time.Now()) {
|
||||
styles[i] = m.common.Styles.Overdue.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if task.Scheduled != "" {
|
||||
styles[i] = m.common.Styles.Scheduled.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if !task.GetDate("due").IsZero() && task.GetDate("due").Truncate(24*time.Hour).Equal(time.Now().Truncate(24*time.Hour)) {
|
||||
styles[i] = m.common.Styles.DueToday.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if task.Due != "" {
|
||||
styles[i] = m.common.Styles.Due.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if len(task.Depends) > 0 {
|
||||
styles[i] = m.common.Styles.Blocked.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
// TODO implement blocking
|
||||
if task.Recur != "" {
|
||||
styles[i] = m.common.Styles.Recurring.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
continue
|
||||
}
|
||||
if len(task.Tags) > 0 {
|
||||
styles[i] = m.common.Styles.Tagged.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
taskIteration:
|
||||
for _, tag := range task.Tags {
|
||||
if tag == "next" {
|
||||
styles[i] = m.common.Styles.TagNext.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
|
||||
break taskIteration
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
// TODO implement uda
|
||||
styles[i] = m.common.Styles.Base.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 _, task := range m.rows {
|
||||
col.ContentWidth = max(col.ContentWidth, lipgloss.Width(task.GetString(col.Name)))
|
||||
@ -172,9 +222,8 @@ func (m *Model) parseColumns(cols []Column) []Column {
|
||||
nonZeroWidths := 0
|
||||
descIndex := -1
|
||||
for i, col := range cols {
|
||||
col.Width = max(col.ContentWidth, lipgloss.Width(col.Title))
|
||||
|
||||
if col.Width > 0 {
|
||||
if col.ContentWidth > 0 {
|
||||
col.Width = max(col.ContentWidth, lipgloss.Width(col.Title))
|
||||
nonZeroWidths++
|
||||
}
|
||||
|
||||
@ -251,7 +300,7 @@ func WithFocused(f bool) Option {
|
||||
}
|
||||
|
||||
// WithStyles sets the table styles.
|
||||
func WithStyles(s Styles) Option {
|
||||
func WithStyles(s common.TableStyle) Option {
|
||||
return func(m *Model) {
|
||||
m.styles = s
|
||||
}
|
||||
@ -497,13 +546,16 @@ func (m *Model) renderRow(r int) string {
|
||||
continue
|
||||
}
|
||||
var cellStyle lipgloss.Style
|
||||
if m.styleFunc != nil {
|
||||
cellStyle = m.styleFunc(r, i, m.rows[r].GetString(col.Name))
|
||||
if r == m.cursor {
|
||||
cellStyle.Inherit(m.styles.Selected)
|
||||
}
|
||||
} else {
|
||||
cellStyle = m.rowStyle[r]
|
||||
// if m.styleFunc != nil {
|
||||
// cellStyle = m.styleFunc(r, i, m.rows[r].GetString(col.Name))
|
||||
// if r == m.cursor {
|
||||
// cellStyle.Inherit(m.styles.Selected)
|
||||
// }
|
||||
// } else {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user