-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
108 lines (91 loc) · 2.68 KB
/
calendar.go
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
package main
import (
"fmt"
"github.com/gdamore/tcell"
"strconv"
"time"
)
// Функция для отрисовки календаря
func drawCalendar(screen tcell.Screen, x, y int, date time.Time) {
printCalendar(screen, x, y, date)
}
// Функция для печати календаря текущего месяца
func printCalendar(screen tcell.Screen, x, y int, date time.Time) {
screen.Clear()
weekdays := [7]string{"Mo", "Tu", "We", "Th", "Fr", "St", "Su"}
color := tcell.ColorDefault
bgColor := tcell.ColorDefault
for col, weekday := range weekdays {
x, y := x+col*3, y+1
color = tcell.ColorGreen
for i, c := range weekday {
style := tcell.StyleDefault.Background(bgColor).Foreground(color)
screen.SetContent(x+i, y, c, nil, style)
}
}
monthCalendar := getMonthCalendar(date)
//monthRow := 0
for row, week := range monthCalendar {
//monthRow = row
for col, day := range week {
color = tcell.ColorDefault
bgColor = tcell.ColorDefault
x, y := col*3, row+2
if day == date.Day() {
color = tcell.ColorWhite
bgColor = tcell.ColorBlue
}
style := tcell.StyleDefault.Background(bgColor).Foreground(color)
str := strconv.Itoa(day)
if day < 10 {
str = "0" + str
}
if day == 0 {
str = " "
}
for i, c := range str {
screen.SetContent(x+i, y, c, nil, style)
}
}
}
monthName := date.Format("January")
year := date.Year()
w, _ := screen.Size()
msg := monthName + " " + fmt.Sprintf("%d", year)
xPos, yPos := 0, 0
printCoor(screen, xPos, yPos, tcell.ColorRed, tcell.ColorDefault, msg)
msg = "Events"
printCoor(screen, w/2-len(msg)+7*3, yPos, tcell.ColorRed, tcell.ColorDefault, msg)
screen.Show()
}
// Функция для получения календаря месяца
func getMonthCalendar(date time.Time) [][]int {
var monthCalendar [][]int
year, month, _ := date.Date()
firstDay := time.Date(year, month, 1, 0, 0, 0, 0, date.Location())
lastDay := time.Date(year, month+1, 0, 0, 0, 0, 0, date.Location())
numDays := lastDay.Day()
firstWeekday := int(firstDay.Weekday())
if firstWeekday == 0 {
firstWeekday = 7 // Переход от воскресенья (0) к понедельнику (7)
}
week := make([]int, 7)
day := 1
for i := 1; i <= numDays+firstWeekday-1; i++ {
if i >= firstWeekday {
week[(i-1)%7] = day
day++
}
if i%7 == 0 || i == numDays+firstWeekday-1 {
monthCalendar = append(monthCalendar, week)
week = make([]int, 7)
}
}
return monthCalendar
}
func printCoor(screen tcell.Screen, x int, y int, fg, bg tcell.Color, msg string) {
style := tcell.StyleDefault.Background(bg).Foreground(fg)
for i, c := range msg {
screen.SetContent(x+i, y, c, nil, style)
}
}