added current date highlight and the ability to switch to the daily schecule by pressing enter
This commit is contained in:
parent
e425b0ad65
commit
bc1b4a2238
1 changed files with 153 additions and 34 deletions
187
main.go
187
main.go
|
|
@ -10,25 +10,44 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
numRows = 6 // Max possible rows in a calendar month view
|
numRows = 6
|
||||||
numCols = 7 // Days in a week
|
numCols = 7
|
||||||
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")) // Blue bg
|
||||||
unselectedStyle = cellStyle.Copy().Background(lipgloss.Color("236")).Foreground(lipgloss.Color("250"))
|
unselectedStyle = cellStyle.Copy().Background(lipgloss.Color("236")).Foreground(lipgloss.Color("250"))
|
||||||
|
todayStyle = cellStyle.Copy().Background(lipgloss.Color("99")).Foreground(lipgloss.Color("15")).Bold(true) // Purple bg + white text
|
||||||
headerStyle = lipgloss.NewStyle().Width(numCols * cellW).Align(lipgloss.Center).Bold(true)
|
headerStyle = lipgloss.NewStyle().Width(numCols * cellW).Align(lipgloss.Center).Bold(true)
|
||||||
daysOfWeekStyle = lipgloss.NewStyle().Width(cellW).Align(lipgloss.Center).Bold(true)
|
daysOfWeekStyle = lipgloss.NewStyle().Width(cellW).Align(lipgloss.Center).Bold(true)
|
||||||
|
|
||||||
|
hourCellStyle = lipgloss.NewStyle().Width(cellW * 3).Height(cellH).Align(lipgloss.Left).PaddingLeft(1)
|
||||||
|
hourSelectedStyle = hourCellStyle.Copy().Bold(true).Background(lipgloss.Color("12")).Foreground(lipgloss.Color("15"))
|
||||||
|
)
|
||||||
|
|
||||||
|
type viewMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
monthView viewMode = iota
|
||||||
|
hourlyView
|
||||||
)
|
)
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
cursorRow int
|
cursorRow int
|
||||||
cursorCol int
|
cursorCol int
|
||||||
monthIndex int
|
monthIndex int
|
||||||
year int
|
year int
|
||||||
|
startOffset int // weekday offset where day 1 starts
|
||||||
|
daysInMonth int
|
||||||
|
|
||||||
|
mode viewMode
|
||||||
|
|
||||||
|
// For hourly view
|
||||||
|
selectedDay int
|
||||||
|
hourCursor int // which hour (0-23) is selected in hourly view
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
|
|
@ -36,11 +55,50 @@ func (m model) Init() tea.Cmd {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch m.mode {
|
||||||
|
case monthView:
|
||||||
|
return m.updateMonthView(msg)
|
||||||
|
case hourlyView:
|
||||||
|
return m.updateHourlyView(msg)
|
||||||
|
default:
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) updateMonthView(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
firstDay := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
m.startOffset = int(firstDay.Weekday())
|
||||||
|
m.daysInMonth = time.Date(m.year, time.Month(m.monthIndex+2), 0, 0, 0, 0, 0, time.UTC).Day()
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "q":
|
case "ctrl+c", "q":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
|
case "a": // Previous month
|
||||||
|
if m.monthIndex == 0 {
|
||||||
|
m.monthIndex = 11
|
||||||
|
m.year--
|
||||||
|
} else {
|
||||||
|
m.monthIndex--
|
||||||
|
}
|
||||||
|
first := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
offset := int(first.Weekday())
|
||||||
|
m.cursorRow = offset / numCols
|
||||||
|
m.cursorCol = offset % numCols
|
||||||
|
return m, nil
|
||||||
|
case "d": // Next month
|
||||||
|
if m.monthIndex == 11 {
|
||||||
|
m.monthIndex = 0
|
||||||
|
m.year++
|
||||||
|
} else {
|
||||||
|
m.monthIndex++
|
||||||
|
}
|
||||||
|
first := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
offset := int(first.Weekday())
|
||||||
|
m.cursorRow = offset / numCols
|
||||||
|
m.cursorCol = offset % numCols
|
||||||
|
return m, nil
|
||||||
case "up":
|
case "up":
|
||||||
if m.cursorRow > 0 {
|
if m.cursorRow > 0 {
|
||||||
m.cursorRow--
|
m.cursorRow--
|
||||||
|
|
@ -57,19 +115,44 @@ 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
|
case "enter":
|
||||||
if m.monthIndex == 0 {
|
// Calculate selected day based on cursor
|
||||||
m.monthIndex = 11
|
dayIndex := m.cursorRow*numCols + m.cursorCol
|
||||||
m.year--
|
if dayIndex >= m.startOffset && dayIndex < m.startOffset+m.daysInMonth {
|
||||||
} else {
|
m.selectedDay = dayIndex - m.startOffset + 1
|
||||||
m.monthIndex--
|
m.hourCursor = 0
|
||||||
|
m.mode = hourlyView
|
||||||
}
|
}
|
||||||
case "d": // Next month
|
}
|
||||||
if m.monthIndex == 11 {
|
|
||||||
m.monthIndex = 0
|
// Clamp cursor to valid days
|
||||||
m.year++
|
dayIndex := m.cursorRow*numCols + m.cursorCol
|
||||||
} else {
|
if dayIndex < m.startOffset || dayIndex >= m.startOffset+m.daysInMonth {
|
||||||
m.monthIndex++
|
m.cursorRow = m.startOffset / numCols
|
||||||
|
m.cursorCol = m.startOffset % numCols
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
default:
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) updateHourlyView(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "esc":
|
||||||
|
m.mode = monthView
|
||||||
|
return m, nil
|
||||||
|
case "ctrl+c", "q":
|
||||||
|
return m, tea.Quit
|
||||||
|
case "up":
|
||||||
|
if m.hourCursor > 0 {
|
||||||
|
m.hourCursor--
|
||||||
|
}
|
||||||
|
case "down":
|
||||||
|
if m.hourCursor < 23 {
|
||||||
|
m.hourCursor++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -77,59 +160,95 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) View() string {
|
func (m model) View() string {
|
||||||
|
switch m.mode {
|
||||||
|
case monthView:
|
||||||
|
return m.viewMonth()
|
||||||
|
case hourlyView:
|
||||||
|
return m.viewHourly()
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) viewMonth() string {
|
||||||
var out string
|
var out string
|
||||||
|
|
||||||
// Header
|
|
||||||
monthTime := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
|
monthTime := time.Date(m.year, time.Month(m.monthIndex+1), 1, 0, 0, 0, 0, time.UTC)
|
||||||
header := headerStyle.Render(monthTime.Format("January 2006"))
|
header := headerStyle.Render(monthTime.Format("January 2006"))
|
||||||
out += header + "\n"
|
out += header + "\n"
|
||||||
|
|
||||||
// Days of the week header
|
|
||||||
weekdays := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
|
weekdays := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
|
||||||
for _, day := range weekdays {
|
for _, day := range weekdays {
|
||||||
out += daysOfWeekStyle.Render(day)
|
out += daysOfWeekStyle.Render(day)
|
||||||
}
|
}
|
||||||
out += "\n"
|
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
|
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++ {
|
||||||
cellNum := r*numCols + c
|
cellNum := r*numCols + c
|
||||||
var content string
|
var content string
|
||||||
|
|
||||||
if cellNum >= startWeekday && day <= daysInMonth {
|
if cellNum >= m.startOffset && day <= m.daysInMonth {
|
||||||
content = fmt.Sprintf("%2d", day)
|
content = fmt.Sprintf("%2d", day)
|
||||||
if r == m.cursorRow && c == m.cursorCol {
|
if r == m.cursorRow && c == m.cursorCol {
|
||||||
out += selectedStyle.Render(content)
|
out += selectedStyle.Render(content)
|
||||||
} else {
|
} else {
|
||||||
out += unselectedStyle.Render(content)
|
today := time.Now()
|
||||||
|
isToday := m.year == today.Year() &&
|
||||||
|
m.monthIndex == int(today.Month())-1 &&
|
||||||
|
day == today.Day()
|
||||||
|
if isToday {
|
||||||
|
out += todayStyle.Render(content)
|
||||||
|
} else {
|
||||||
|
out += unselectedStyle.Render(content)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
day++
|
day++
|
||||||
} else {
|
} else {
|
||||||
out += cellStyle.Render("") // Empty cell
|
out += cellStyle.Render("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out += "\n"
|
out += "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
out += "\n[a]/[d] to change month, arrows to move, q to quit."
|
out += "\n[a]/[d] to change month, arrows to move, enter to select a day, q to quit."
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model) viewHourly() string {
|
||||||
|
var out string
|
||||||
|
|
||||||
|
dateHeader := fmt.Sprintf("Schedule for %04d-%02d-%02d", m.year, m.monthIndex+1, m.selectedDay)
|
||||||
|
out += headerStyle.Render(dateHeader) + "\n\n"
|
||||||
|
|
||||||
|
for hour := 0; hour < 24; hour++ {
|
||||||
|
label := fmt.Sprintf("%02d:00 - %02d:00", hour, hour+1)
|
||||||
|
if hour == m.hourCursor {
|
||||||
|
out += hourSelectedStyle.Render(label) + "\n"
|
||||||
|
} else {
|
||||||
|
out += hourCellStyle.Render(label) + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out += "\nPress ESC to return to month view, q to quit."
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
offset := int(start.Weekday())
|
||||||
p := tea.NewProgram(model{
|
p := tea.NewProgram(model{
|
||||||
year: now.Year(),
|
year: now.Year(),
|
||||||
monthIndex: int(now.Month()) - 1,
|
monthIndex: int(now.Month()) - 1,
|
||||||
|
cursorRow: offset / numCols,
|
||||||
|
cursorCol: offset % numCols,
|
||||||
|
startOffset: offset,
|
||||||
|
mode: monthView,
|
||||||
})
|
})
|
||||||
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\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue