-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (153 loc) · 4.3 KB
/
main.go
File metadata and controls
195 lines (153 loc) · 4.3 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"fmt"
"os"
"strconv"
"time"
)
func main() {
args := os.Args[1:]
printUsage := func() {
fmt.Fprintln(os.Stderr, "Usage: days [<from>] [<to>|<+-days>]")
}
if len(args) > 3 {
printUsage()
os.Exit(1)
}
now := time.Now()
from := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
to := from
if len(args) > 0 {
var err error
if len(args) == 2 {
from, err = time.ParseInLocation("2006-01-02", args[0], time.Local)
if err != nil {
fmt.Fprintln(os.Stderr, err)
printUsage()
os.Exit(1)
}
}
lastArg := args[len(args)-1]
to, err = time.ParseInLocation("2006-01-02", lastArg, time.Local)
if err != nil {
delta, err := parseDelta(lastArg)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing date or delta %q\n", lastArg)
printUsage()
os.Exit(1)
}
to = from.AddDate(0, 0, delta)
}
}
if to.Before(from) {
from, to = to, from
}
cStr := calendar(from, to, len(args) > 0)
fmt.Println(cStr)
if len(args) > 0 {
workday, weekend := days(from, to)
days := workday + weekend
fmt.Println()
fmt.Printf("%s - %s: ", from.Format("2006-01-02"), to.Format("2006-01-02"))
pl := func(n int) string {
if n == 1 {
return ""
}
return "s"
}
switch {
case workday > 0 && weekend > 0:
fmt.Printf("%d day%s (%d work day%s + %d weekend day%s)", days, pl(days), workday, pl(workday), weekend, pl(weekend))
case workday > 0 && weekend == 0:
fmt.Printf("%d work day%s", workday, pl(workday))
case workday == 0 && weekend > 0:
fmt.Printf("%d weekend day%s", weekend, pl(weekend))
}
fmt.Printf(" (%d night%s)\n", days-1, pl(days-1))
}
}
func parseDate(s string) (time.Time, error) {
t, err := time.ParseInLocation("2006-01-02", s, time.Local)
if err != nil {
return time.Time{}, fmt.Errorf("error parsing date %q: %w", s, err)
}
return t, nil
}
func parseDelta(s string) (days int, err error) {
return strconv.Atoi(s)
}
func days(from, to time.Time) (workday, weekend int) {
for d := from; !d.After(to); d = d.AddDate(0, 0, 1) {
switch d.Weekday() {
case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
workday++
case time.Saturday, time.Sunday:
weekend++
}
}
return workday, weekend
}
func calendar(from, to time.Time, highlightRange bool) string {
var buf bytes.Buffer
fmt.Fprint(&buf, fg(" Mon Tue Wed Thu Fri Sat Sun", 231))
first := time.Date(from.Year(), from.Month(), 1, 0, 0, 0, 0, from.Location())
last := time.Date(to.Year(), to.Month()+1, 1, 0, 0, 0, 0, to.Location()).AddDate(0, 0, -1)
var newMonth bool
for d := first; !d.After(last); d = d.AddDate(0, 0, 1) {
// New month
if d.Day() == 1 {
newMonth = true
buf.WriteByte('\n')
fmt.Fprintf(&buf, "%s ", fg(d.Format("Jan"), 231))
start := d
for start.Weekday() != time.Monday {
start = start.AddDate(0, 0, -1)
}
for blank := start.AddDate(0, 0, 1); !blank.After(d); blank = blank.AddDate(0, 0, 1) {
fmt.Fprint(&buf, " ")
}
} else if d.Weekday() == time.Monday {
newMonth = false
fmt.Fprint(&buf, " ")
}
dayFormatStr := "%3d"
// Highlight today
if d.Format("2006-01-02") == time.Now().Format("2006-01-02") {
dayFormatStr = invert(dayFormatStr)
}
if highlightRange && !d.Before(from) && !d.After(to) {
switch d.Weekday() {
case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
dayFormatStr = fg(dayFormatStr, 39) // Blue for weekdays
case time.Saturday, time.Sunday:
dayFormatStr = fg(dayFormatStr, 197) // Red for weekends
}
} else {
dayFormatStr = fg(dayFormatStr, 251) // Grey for out of range days
}
// Print day
fmt.Fprintf(&buf, dayFormatStr, d.Day())
// Space between days
if d.Weekday() != time.Sunday {
fmt.Fprint(&buf, " ")
}
// Print year if first week of span or January
if d.Weekday() == time.Sunday && newMonth {
if (d.Year() == first.Year() && d.Month() == first.Month()) || d.Month() == time.January {
fmt.Fprintf(&buf, " "+fg("%d", 231), d.Year())
}
}
// Newline after Sunday
if d.Weekday() == time.Sunday && !d.Equal(last) {
buf.WriteByte('\n')
}
}
return buf.String()
}
func invert(s string) string {
return "\033[7m" + s + "\033[0m"
}
func fg(s string, c int) string {
return fmt.Sprintf("\x1b[38;5;%dm%s\033[0m", c, s)
}