added pagination for months

This commit is contained in:
steven carpenter 2025-06-28 21:47:32 -04:00
parent af03d06bf5
commit e425b0ad65

78
main.go
View file

@ -3,27 +3,32 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
const ( const (
numRows = 5 numRows = 6 // Max possible rows in a calendar month view
numCols = 7 numCols = 7 // Days in a week
cellW = 10 cellW = 10
cellH = 1 cellH = 1
) )
var ( var (
cellStyle = lipgloss.NewStyle().Width(cellW).Height(cellH).Align(lipgloss.Center) cellStyle = lipgloss.NewStyle().Width(cellW).Height(cellH).Align(lipgloss.Center)
selectedStyle = cellStyle.Copy().Bold(true).Background(lipgloss.Color("12")).Foreground(lipgloss.Color("15")) selectedStyle = cellStyle.Copy().Bold(true).Background(lipgloss.Color("12")).Foreground(lipgloss.Color("15"))
unselectedStyle = cellStyle.Copy().Background(lipgloss.Color("236")).Foreground(lipgloss.Color("250")) unselectedStyle = cellStyle.Copy().Background(lipgloss.Color("236")).Foreground(lipgloss.Color("250"))
headerStyle = lipgloss.NewStyle().Width(numCols * cellW).Align(lipgloss.Center).Bold(true)
daysOfWeekStyle = lipgloss.NewStyle().Width(cellW).Align(lipgloss.Center).Bold(true)
) )
type model struct { type model struct {
cursorRow int cursorRow int
cursorCol int cursorCol int
monthIndex int
year int
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -52,6 +57,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.cursorCol < numCols-1 { if m.cursorCol < numCols-1 {
m.cursorCol++ m.cursorCol++
} }
case "a": // Previous month
if m.monthIndex == 0 {
m.monthIndex = 11
m.year--
} else {
m.monthIndex--
}
case "d": // Next month
if m.monthIndex == 11 {
m.monthIndex = 0
m.year++
} else {
m.monthIndex++
}
} }
} }
return m, nil return m, nil
@ -59,25 +78,56 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string { func (m model) View() string {
var out string var out string
count := 1
// Header
monthTime := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
header := headerStyle.Render(monthTime.Format("January 2006"))
out += header + "\n"
// Days of the week header
weekdays := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
for _, day := range weekdays {
out += daysOfWeekStyle.Render(day)
}
out += "\n"
// First weekday and number of days
firstDay := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
startWeekday := int(firstDay.Weekday())
daysInMonth := time.Date(m.year, time.Month(m.monthIndex+2), 0, 0, 0, 0, 0, time.UTC).Day()
// Calendar grid
day := 1
for r := 0; r < numRows; r++ { for r := 0; r < numRows; r++ {
for c := 0; c < numCols; c++ { for c := 0; c < numCols; c++ {
label := fmt.Sprintf("Cell %02d", count) cellNum := r*numCols + c
if r == m.cursorRow && c == m.cursorCol { var content string
out += selectedStyle.Render(label)
if cellNum >= startWeekday && day <= daysInMonth {
content = fmt.Sprintf("%2d", day)
if r == m.cursorRow && c == m.cursorCol {
out += selectedStyle.Render(content)
} else {
out += unselectedStyle.Render(content)
}
day++
} else { } else {
out += unselectedStyle.Render(label) out += cellStyle.Render("") // Empty cell
} }
count++
} }
out += "\n" out += "\n"
} }
out += "\nUse arrow keys to move, q to quit."
out += "\n[a]/[d] to change month, arrows to move, q to quit."
return out return out
} }
func main() { func main() {
p := tea.NewProgram(model{}) now := time.Now()
p := tea.NewProgram(model{
year: now.Year(),
monthIndex: int(now.Month()) - 1,
})
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1) os.Exit(1)