-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.go
119 lines (98 loc) · 2.86 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
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"io"
"strings"
"time"
"github.com/fatih/color"
holiday "github.com/holiday-jp/holiday_jp-go"
"github.com/y-yagi/goext/timeext"
)
// Calendar struct
type Calendar struct {
DateHeader string
WeekHeader string
Body [6]string
}
var (
red = color.New(color.FgRed, color.Bold).SprintFunc()
blue = color.New(color.FgBlue, color.Bold).SprintFunc()
reverseVideo = color.New(color.ReverseVideo).SprintFunc()
daySpace = " "
)
func (calendar *Calendar) setHeader(date time.Time) {
calendar.DateHeader += fmt.Sprintf(" %d年 %02d月 ", date.Year(), date.Month())
calendar.WeekHeader += fmt.Sprintf("%s %s %s %s %s %s %s ", red("日"), "月", "火", "水", "木", "金", blue("土"))
}
func (calendar *Calendar) isNeedNewLine(date time.Time) bool {
return date.Weekday().String() == "Saturday"
}
func (calendar *Calendar) decoratedDate(date time.Time) string {
var decoratedDate string
space := ""
if date.Day() < 10 {
space = " "
}
if holiday.IsHoliday(date) {
decoratedDate = red(date.Day())
} else if date.Weekday().String() == "Sunday" {
decoratedDate = red(date.Day())
} else if date.Weekday().String() == "Saturday" {
decoratedDate = blue(date.Day())
} else {
decoratedDate = fmt.Sprintf("%d", date.Day())
}
today := time.Now()
if (date.Year() == today.Year()) && (date.Month() == today.Month()) && date.Day() == today.Day() {
decoratedDate = reverseVideo(decoratedDate)
}
return space + decoratedDate
}
func (calendar *Calendar) adjustSpace(line int) {
calendar.Body[line] += " "
}
// Generate calendar
func (calendar *Calendar) Generate(date time.Time) {
var calDate time.Time
calendar.setHeader(date)
firstDate := timeext.BeginningOfMonth(date)
lastDate := timeext.EndOfMonth(date)
wday := int(firstDate.Weekday())
calendar.Body[0] += strings.Repeat(daySpace, wday)
line := 0
for i := 1; i < lastDate.Day()+1; i++ {
calDate = time.Date(date.Year(), date.Month(), i, 0, 0, 0, 0, time.Local)
calendar.Body[line] += fmt.Sprintf("%2s ", calendar.decoratedDate(calDate))
if calendar.isNeedNewLine(calDate) {
calendar.adjustSpace(line)
line++
}
}
wday = int(lastDate.Weekday())
if wday == 6 && line == 4 {
calendar.Body[line] += strings.Repeat(daySpace, 7)
} else {
calendar.Body[line] += strings.Repeat(daySpace, 6-wday)
}
calendar.adjustSpace(line)
for line++; line < len(calendar.Body); line++ {
calendar.Body[line] += strings.Repeat(daySpace, 7)
}
calendar.adjustSpace(line - 1)
}
// Show calendar
func (calendar *Calendar) Show(w io.Writer) {
fmt.Fprint(w, calendar.DateHeader+"\n")
fmt.Fprint(w, calendar.WeekHeader+"\n")
for _, element := range calendar.Body {
fmt.Fprint(w, element+"\n")
}
}
// Clear calendar
func (calendar *Calendar) Clear() {
calendar.DateHeader = ""
calendar.WeekHeader = ""
for i := range calendar.Body {
calendar.Body[i] = ""
}
}