-
Notifications
You must be signed in to change notification settings - Fork 11
/
print.go
77 lines (67 loc) · 1.68 KB
/
print.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
package main
import (
"fmt"
"strings"
"time"
"github.com/gookit/color"
)
var sixEmptySpaces = strings.Repeat(" ", 6)
func buildHeader(start, end time.Time) string {
s := strings.Builder{}
for current := start; current.Before(end) || current.Equal(end); current = current.AddDate(0, 1, 0) {
s.WriteString(fmt.Sprintf("%-16s", current.Month().String()[:3]))
}
return s.String()
}
func getDay(i int) string {
switch i {
case 1:
return "Mon"
case 3:
return "Wed"
case 5:
return "Fri"
}
return strings.Repeat(" ", 3)
}
func printTable(commits map[int]int, b Boundary) {
for b.Since.Weekday() != time.Sunday {
b.Since = b.Since.AddDate(0, 0, -1)
}
for b.Until.Weekday() != time.Saturday {
b.Until = b.Until.AddDate(0, 0, 1)
}
fmt.Printf("%s %s\n", sixEmptySpaces, buildHeader(b.Since, b.Until))
max := getMaxValue(commits)
s := strings.Builder{}
s1 := b.Since
for i := 0; i < 7; i++ {
s.WriteString(fmt.Sprintf("%-5s", getDay(i)))
sn2 := s1
for !sn2.After(b.Until) {
d := daysAgo(sn2)
s.WriteString(printCell(commits[d], max))
sn2 = sn2.AddDate(0, 0, 7)
}
s1 = s1.AddDate(0, 0, 1)
fmt.Println(s.String())
s.Reset()
}
}
func printCell(val, maxValue int) string {
var colorFunc color.Style
if val == 0 {
colorFunc = color.New(color.FgWhite, color.BgBlack)
return colorFunc.Sprintf(" - ")
}
if val <= maxValue/8 {
colorFunc = color.New(color.FgBlack, color.BgLightCyan)
} else if val <= maxValue/4 {
colorFunc = color.New(color.FgBlack, color.BgHiCyan)
} else if val < maxValue/2 {
colorFunc = color.New(color.FgBlack, color.BgHiBlue)
} else {
colorFunc = color.New(color.FgBlack, color.BgBlue)
}
return colorFunc.Sprintf(" %2d ", val)
}