Initial Commit
This commit is contained in:
commit
87a522daab
22 changed files with 1555 additions and 0 deletions
109
tui/update.go
Normal file
109
tui/update.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.skdevstudios.com/SK-Development-Studios/go-cal-tui/internal/storage"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
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) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
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++
|
||||
}
|
||||
case "up":
|
||||
if m.cursorRow > 0 {
|
||||
m.cursorRow--
|
||||
}
|
||||
case "down":
|
||||
if m.cursorRow < numRows-1 {
|
||||
m.cursorRow++
|
||||
}
|
||||
case "left":
|
||||
if m.cursorCol > 0 {
|
||||
m.cursorCol--
|
||||
}
|
||||
case "right":
|
||||
if m.cursorCol < numCols-1 {
|
||||
m.cursorCol++
|
||||
}
|
||||
case "enter":
|
||||
dayIndex := m.cursorRow*numCols + m.cursorCol
|
||||
if dayIndex >= m.startOffset && dayIndex < m.startOffset+m.daysInMonth {
|
||||
m.selectedDay = dayIndex - m.startOffset + 1
|
||||
m.hourCursor = 0
|
||||
m.mode = hourlyView
|
||||
}
|
||||
}
|
||||
|
||||
// Clamp cursor to valid days
|
||||
dayIndex := m.cursorRow*numCols + m.cursorCol
|
||||
if dayIndex < m.startOffset || dayIndex >= m.startOffset+m.daysInMonth {
|
||||
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 "n":
|
||||
showEventForm(context.Background(), func(e storage.EventInput) {
|
||||
_ = storage.SaveEvent(e)
|
||||
})
|
||||
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++
|
||||
}
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue