-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalendar.go
More file actions
167 lines (155 loc) · 5.27 KB
/
calendar.go
File metadata and controls
167 lines (155 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"time"
"git.sr.ht/~rockorager/vaxis"
)
func (app *App) drawCalendarWindow(win vaxis.Window) {
monthTitle := fmt.Sprintf("%s %d", app.currentMonth.Month().String(), app.currentMonth.Year())
win.Println(0, vaxis.Segment{
Text: monthTitle,
Style: vaxis.Style{Attribute: vaxis.AttrBold},
})
daysOfWeek := make([]string, 7)
for i := range daysOfWeek {
day := time.Sunday + time.Weekday(i)
daysOfWeek[i] = day.String()[:2]
}
daySegments := make([]vaxis.Segment, 0, len(daysOfWeek)*2-1)
for i, day := range daysOfWeek {
daySegments = append(daySegments, vaxis.Segment{
Text: day,
Style: vaxis.Style{UnderlineStyle: vaxis.UnderlineSingle},
})
if i < len(daysOfWeek)-1 {
daySegments = append(daySegments, vaxis.Segment{
Text: " ",
})
}
}
win.Println(2, daySegments...)
firstDay := app.currentMonth
firstDayOfWeek := int(firstDay.Weekday())
year, month, _ := app.currentMonth.Date()
daysInMonth := time.Date(year, month+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day()
selectedInCurrentMonth := false
if !app.selectedDate.IsZero() {
selectedYear, selectedMonth, _ := app.selectedDate.Date()
if selectedYear == year && selectedMonth == month {
selectedInCurrentMonth = true
}
}
dayNum := 1
row := 3 // Start on row 2 (after the header)
for weekRow := 0; weekRow < 6 && dayNum <= daysInMonth; weekRow++ {
segments := make([]vaxis.Segment, 0, 20)
for weekDay := range 7 {
if weekRow == 0 && weekDay < firstDayOfWeek {
segments = append(segments, vaxis.Segment{
Text: " ",
})
} else if dayNum <= daysInMonth {
isCursor := dayNum == app.cursorDay
isSelected := selectedInCurrentMonth && dayNum == app.selectedDay
now := time.Now()
isToday := now.Year() == year &&
now.Month() == month &&
now.Day() == dayNum
currentDate := time.Date(year, month, dayNum, 0, 0, 0, 0, app.currentMonth.Location())
isWeekend := currentDate.Weekday() == time.Saturday || currentDate.Weekday() == time.Sunday
style := vaxis.Style{}
if isWeekend {
style.Foreground = vaxis.IndexColor(250)
}
if isCursor && app.focusedWindow == WinCalendar {
style.Attribute = vaxis.AttrReverse
if isToday {
style.Foreground = vaxis.IndexColor(4) // Blue for selected day
}
} else if isSelected {
style.Attribute = vaxis.AttrBold
style.Foreground = vaxis.IndexColor(4) // Blue for selected day
} else if isToday {
style.Attribute = vaxis.AttrBold
// style.Foreground = vaxis.IndexColor(15)
}
dayText := fmt.Sprintf("%2d", dayNum)
segments = append(segments, vaxis.Segment{
Text: dayText,
Style: style,
})
dayNum++
} else {
segments = append(segments, vaxis.Segment{
Text: " ",
})
}
if weekDay < 6 {
segments = append(segments, vaxis.Segment{
Text: " ",
})
}
}
win.Println(row+weekRow, segments...)
}
}
func (app *App) handleCalendarKeys(key vaxis.Key) bool {
if app.showQuitConfirm {
return false
}
year, month, _ := app.currentMonth.Date()
daysInMonth := time.Date(year, month+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day()
if key.Matches('L') {
app.focusedWindow = WinTimer
} else if key.Matches('J') {
app.focusedWindow = WinEntries
} else if key.Matches('h') || key.Matches(vaxis.KeyLeft) {
if app.cursorDay > 1 {
app.cursorDay--
}
} else if key.Matches('l') || key.Matches(vaxis.KeyRight) {
if app.cursorDay < daysInMonth {
app.cursorDay++
}
} else if key.Matches('k') || key.Matches(vaxis.KeyUp) {
// Move up a week
if app.cursorDay > 7 {
app.cursorDay -= 7
}
} else if key.Matches('j') || key.Matches(vaxis.KeyDown) {
// Move down a week
if app.cursorDay+7 <= daysInMonth {
app.cursorDay += 7
}
} else if key.Matches('g') || key.Matches(vaxis.KeyHome) {
// First day of month
app.cursorDay = 1
} else if key.Matches('G') || key.Matches(vaxis.KeyEnd) {
// Last day of month
app.cursorDay = daysInMonth
} else if key.Matches('p') || key.Matches(vaxis.KeyPgUp) {
// Previous month
app.currentMonth = time.Date(year, month-1, 1, 0, 0, 0, 0, app.currentMonth.Location())
if app.cursorDay > time.Date(app.currentMonth.Year(), app.currentMonth.Month()+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day() {
app.cursorDay = time.Date(app.currentMonth.Year(), app.currentMonth.Month()+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day()
}
} else if key.Matches('n') || key.Matches(vaxis.KeyPgDown) {
// Next month
app.currentMonth = time.Date(year, month+1, 1, 0, 0, 0, 0, app.currentMonth.Location())
if app.cursorDay > time.Date(app.currentMonth.Year(), app.currentMonth.Month()+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day() {
app.cursorDay = time.Date(app.currentMonth.Year(), app.currentMonth.Month()+1, 0, 0, 0, 0, 0, app.currentMonth.Location()).Day()
}
} else if key.Matches('t') {
// Today
now := time.Now()
app.currentMonth = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
app.cursorDay = now.Day()
} else if key.Matches(vaxis.KeyEnter) || key.Matches(vaxis.KeySpace) {
app.selectedTask = -1
app.selectedDay = app.cursorDay
app.selectedDate = time.Date(year, month, app.selectedDay, 0, 0, 0, 0, app.currentMonth.Location())
app.fetchEntries(app.selectedDate)
app.fetchTimers()
}
return false
}