-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
133 lines (119 loc) · 2.85 KB
/
stats.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
package main
import (
"fmt"
"sort"
"time"
)
func getKeyList(dates map[int]int) []int {
var keys []int
for key := range dates {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] > keys[j]
})
return keys
}
func determineDayOfWeek(currentDayOfWeek int, daysAgo int) int {
dayOfTheWeek := (currentDayOfWeek - daysAgo) % 7
if dayOfTheWeek < 0 {
dayOfTheWeek = (dayOfTheWeek + 7) % 7
}
return int(dayOfTheWeek)
}
func handleCutoffs(cols map[int]column, cutoff, start time.Weekday, week int) {
if len(cols[week]) > 0 {
for start < cutoff {
cols[week][start+1] = -1
start = start + 1
}
} else {
cols[week] = make(column, 7)
for start < cutoff {
cols[week][start+1] = -1
start = start + 1
}
}
}
func calculateOffset() int {
var offset int
switch time.Now().Weekday() {
case time.Sunday:
offset = 6
case time.Monday:
offset = 5
case time.Tuesday:
offset = 4
case time.Wednesday:
offset = 3
case time.Thursday:
offset = 2
case time.Friday:
offset = 1
case time.Saturday:
offset = 0
}
return offset
}
// TODO: Figure out a way to get the first and last day of consideration and mark them a negative(invalid value) so that once they are found when rendering it can be switched out
type column []int
func genCols(keys []int, dates map[int]int) map[int]column {
cols := make(map[int]column)
today := time.Now().Weekday()
offset := calculateOffset()
for _, k := range keys {
week := int((k + offset) / 7)
if len(cols[week]) == 0 {
cols[week] = make(column, 7)
}
day := determineDayOfWeek(int(today), k)
cols[week][day] += dates[k]
}
handleCutoffs(cols, time.Saturday, today, 0)
handleCutoffs(cols, time.Weekday(determineDayOfWeek(int(today), 183))-1, time.Sunday-1, 26)
return cols
}
func determineAndPrintColour(commit int) {
if commit >= 10 {
fmt.Print("\033[48;5;34m \033[0m")
} else if commit >= 5 {
fmt.Print("\033[48;5;28m \033[0m")
} else if commit >= 1 {
fmt.Print("\033[48;5;22m \033[0m")
} else if commit == 0 {
fmt.Print("\033[48;5;17m \033[0m")
} else {
fmt.Print(" ")
}
}
func printStats(cols map[int]column) {
for i := 0; i < 7; i++ {
switch i {
case 1:
fmt.Print("Mon ")
case 3:
fmt.Print("Wed ")
case 5:
fmt.Print("Fri ")
default:
fmt.Print(" ")
}
for j := 26; j >= 0; j-- {
if len(cols[j]) == 0 {
determineAndPrintColour(0)
} else {
determineAndPrintColour(cols[j][i])
}
}
fmt.Println()
}
}
// NOTE: to get the week mod the key with 26(weeks in 6 months) or maybe 27 (to adjust for the days of the week) to get the first day mod the
// TODO: Add a mapping to read store all the commit dates to generate image of contributions
func GetStats(email string, repo string) {
dates := make(map[int]int)
dates = genDatesMap(email, repo, dates)
keys := getKeyList(dates)
cols := genCols(keys, dates)
printStats(cols)
}