Skip to content

Commit

Permalink
use Boundary struct instead of 2 separate time.Time values
Browse files Browse the repository at this point in the history
  • Loading branch information
knbr13 committed Mar 2, 2024
1 parent 61583da commit c9d6532
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func main() {
os.Exit(1)
}

commits := processRepos(repos, email, b.Since, b.Until)
commits := processRepos(repos, email, *b)
fmt.Print("\n\n")
printTable(commits, b.Since, b.Until)
printTable(commits, *b)
fmt.Print("\n\n")
}

Expand Down
16 changes: 8 additions & 8 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ func getDay(i int) string {
return strings.Repeat(" ", 3)
}

func printTable(commits map[int]int, since, until time.Time) {
for since.Weekday() != time.Sunday {
since = since.AddDate(0, 0, -1)
func printTable(commits map[int]int, b Boundary) {
for b.Since.Weekday() != time.Sunday {
b.Since = b.Since.AddDate(0, 0, -1)
}
for until.Weekday() != time.Saturday {
until = until.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(since, until))
fmt.Printf("%s %s\n", sixEmptySpaces, buildHeader(b.Since, b.Until))
max := getMaxValue(commits)

s := strings.Builder{}
s1 := since
s1 := b.Since

for i := 0; i < 7; i++ {
s.WriteString(fmt.Sprintf("%-5s", getDay(i)))
sn2 := s1
for !sn2.After(until) {
for !sn2.After(b.Until) {
d := daysAgo(sn2)
s.WriteString(printCell(commits[d], max))
sn2 = sn2.AddDate(0, 0, 7)
Expand Down
22 changes: 12 additions & 10 deletions print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@ func TestPrintTable(t *testing.T) {
13: 0,
}

since := time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC)
until := time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC)
b := Boundary{
Since: time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC),
Until: time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC),
}

oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

printTable(commits, since, until)
printTable(commits, b)
w.Close()

dat, err := io.ReadAll(r)
Expand All @@ -173,23 +175,23 @@ func TestPrintTable(t *testing.T) {
var buf strings.Builder
_, _ = fmt.Fprint(&buf, string(dat))

for since.Weekday() != time.Sunday {
since = since.AddDate(0, 0, -1)
for b.Since.Weekday() != time.Sunday {
b.Since = b.Since.AddDate(0, 0, -1)
}
for until.Weekday() != time.Saturday {
until = until.AddDate(0, 0, 1)
for b.Until.Weekday() != time.Saturday {
b.Until = b.Until.AddDate(0, 0, 1)
}

s := strings.Builder{}
s1 := since
s1 := b.Since

s.WriteString(fmt.Sprintf("%s %s\n", sixEmptySpaces, buildHeader(since, until)))
s.WriteString(fmt.Sprintf("%s %s\n", sixEmptySpaces, buildHeader(b.Since, b.Until)))

max := getMaxValue(commits)
for i := 0; i < 7; i++ {
s.WriteString(fmt.Sprintf("%-5s", getDay(i)))
sn2 := s1
for !sn2.After(until) {
for !sn2.After(b.Until) {
d := daysAgo(sn2)
s.WriteString(printCell(commits[d], max))
sn2 = sn2.AddDate(0, 0, 7)
Expand Down
8 changes: 4 additions & 4 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const sixMonthsInDays int = 182

var now = time.Now()

func fillCommits(path, email string, commits map[int]int, since, until time.Time) error {
func fillCommits(path, email string, commits map[int]int, b Boundary) error {
repo, err := git.PlainOpen(path)
if err != nil {
return err
}

commitIterator, err := repo.Log(&git.LogOptions{Since: &since, Until: &until})
commitIterator, err := repo.Log(&git.LogOptions{Since: &b.Since, Until: &b.Until})
if err != nil {
return err
}
Expand All @@ -38,11 +38,11 @@ func fillCommits(path, email string, commits map[int]int, since, until time.Time
return err
}

func processRepos(repos []string, email string, since, until time.Time) map[int]int {
func processRepos(repos []string, email string, b Boundary) map[int]int {
m := map[int]int{}
var err error
for _, repo := range repos {
err = fillCommits(repo, email, m, since, until)
err = fillCommits(repo, email, m, b)
if err != nil {
fmt.Printf("failed to fill commits in %q: %v", repo, err)
}
Expand Down
16 changes: 10 additions & 6 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ func TestFillCommits(t *testing.T) {
},
}

since := time.Now().AddDate(0, 0, -1)
until := time.Now().AddDate(0, 0, 1)
b := Boundary{
Since: time.Now().AddDate(0, 0, -1),
Until: time.Now().AddDate(0, 0, 1),
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
commits := map[int]int{}
err = fillCommits(tt.Path, tt.Email, commits, since, until)
err = fillCommits(tt.Path, tt.Email, commits, b)
if err != nil {
t.Fatalf("failed to fill commits in %q: %v", tt.Path, err)
}
Expand Down Expand Up @@ -97,12 +99,14 @@ func TestProcessRepos(t *testing.T) {
},
}

since := time.Now().AddDate(0, 0, -1)
until := time.Now().AddDate(0, 0, 1)
b := Boundary{
Since: time.Now().AddDate(0, 0, -1),
Until: time.Now().AddDate(0, 0, 1),
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
commits := processRepos(tt.Repos, tt.Email, since, until)
commits := processRepos(tt.Repos, tt.Email, b)
if len(commits) != len(tt.Expected) {
t.Errorf("processRepos11() = %v, want %v", commits, tt.Expected)
}
Expand Down

0 comments on commit c9d6532

Please sign in to comment.