-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcal.go
147 lines (114 loc) · 2.57 KB
/
fcal.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
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
package main
import (
"flag"
"fmt"
"github.com/rfaulhaber/fdate"
"log"
"os"
"strconv"
"strings"
)
var (
stderr = log.New(os.Stderr, "fcal: ", 0)
)
func main() {
todayFlag := flag.Bool("t", false, "prints today's date instead of the month")
ruralFlag := flag.Bool("r", false, "prints the name of today's day on the Rural Calendar")
flag.Parse()
if *todayFlag && *ruralFlag {
stderr.Fatalln("cannot use -t and -r flags together")
os.Exit(1)
}
today := fdate.Today()
if *todayFlag {
output := ""
if today.Month() == 13 {
output += today.String() + " - " + fdate.CompDay(today.Day()-1).String() + "\n"
} else {
output = today.String() + "\n"
}
fmt.Println(output)
} else if *ruralFlag {
fmt.Println(today.RuralDay())
} else {
fmt.Println(CreateCalendar(today))
}
}
func CreateCalendar(date fdate.Date) string {
output := ""
month := date.Month().String()
year := date.RomanYear().String()
monthString := month + " " + year
spacesCount := 20 - len(monthString)/2
monthSpaces := ""
for i := 0; i < spacesCount; i++ {
monthSpaces += " "
}
output += monthSpaces + monthString + "\n"
if date.Month() < 13 {
for i := 0; i < 10; i++ {
weekday := fdate.Weekday(i).String()
abbr := strings.ToUpper(string(weekday[0])) + string(weekday[1:3])
// "é" is two bytes, so we need to grab the next one
if i == 9 {
abbr += string(weekday[3])
}
output += abbr + " "
}
output += "\n"
}
if date.Month() == 13 {
var days int
if date.IsLeapYear() {
days = 6
} else {
days = 5
}
for i := 0; i < days; i++ {
dayIndex := i + 1
if dayIndex == date.Day() {
output += highlightDate(strconv.Itoa(dayIndex))
} else {
output += " "
output += strconv.Itoa(dayIndex)
}
output += " - " + fdate.CompDay(i).String() + "\n"
}
} else {
monthDays := 31
for i := 1; i < monthDays; i++ {
spaces := ""
if i == 1 {
spaces = " "
} else if i < 10 {
spaces = " "
} else if i%10 == 1 {
spaces = " "
} else {
spaces = " "
}
dateStr := strconv.Itoa(i)
// this doesn't work in the command prompt but works in Powershell on Windows
if i == date.Day() {
dateStr = highlightDate(dateStr)
if strings.Contains(dateStr, " ") {
spaces = spaces[:len(spaces)-1]
}
}
output += spaces + dateStr
if i%10 == 0 {
output += "\n"
}
}
}
return output
}
func highlightDate(dateStr string) string {
var str string
if len(dateStr) == 1 {
str = "\033[7m " + dateStr + "\033[27m"
} else {
str = "\033[7m" + dateStr + "\033[27m"
}
return str
}