Skip to content

Commit

Permalink
Merge pull request #54 from siddharthkoli/empty-grid-fix
Browse files Browse the repository at this point in the history
Fix empty contribution showing high-level blocks
  • Loading branch information
chrisreddington authored Dec 28, 2024
2 parents c5698a0 + 10a6ff5 commit bdd428f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ascii/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func GenerateASCII(contributionGrid [][]types.ContributionDay, username string,
if day.ContributionCount == -1 {
asciiGrid[dayIdx][weekIdx] = FutureBlock
} else {
normalized := float64(day.ContributionCount) / float64(maxContributions)
normalized := 0.0
if maxContributions != 0 {
normalized = float64(day.ContributionCount) / float64(maxContributions)
}
asciiGrid[dayIdx][weekIdx] = getBlock(normalized, dayIdx, nonZeroCount)
}
}
Expand Down
60 changes: 60 additions & 0 deletions ascii/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,63 @@ func TestGetBlock(t *testing.T) {
})
}
}

// TestGenerateASCIIZeroContributions tests the GenerateASCII function with zero contributions.
// It verifies that the skyline consists of empty blocks and appropriately handles the header and footer.
func TestGenerateASCIIZeroContributions(t *testing.T) {
tests := []struct {
name string
includeHeaderAndFooter bool
}{
{
name: "Zero contributions without header",
includeHeaderAndFooter: false,
},
{
name: "Zero contributions with header",
includeHeaderAndFooter: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a test grid with zero contributions
grid := makeTestGrid(3, 7)
for i := range grid {
for j := range grid[i] {
grid[i][j].ContributionCount = 0
}
}

// Generate ASCII art
result, err := GenerateASCII(grid, "testuser", 2023, tt.includeHeaderAndFooter, tt.includeHeaderAndFooter)
if err != nil {
t.Fatalf("GenerateASCII() returned an error: %v", err)
}

lines := strings.Split(result, "\n")

// Determine the starting line of the skyline
skylineStart := 0
if tt.includeHeaderAndFooter {
// Assuming HeaderTemplate has a fixed number of lines
headerLines := strings.Count(HeaderTemplate, "\n")
skylineStart = headerLines + 1 // +1 for the additional newline after header
}

// Verify the skyline has at least 7 lines
if len(lines) < skylineStart+7 {
t.Fatalf("Expected at least %d lines for skyline, got %d", skylineStart+7, len(lines))
}

// Check each line of the skyline for empty blocks
for i := skylineStart; i < skylineStart+7; i++ {
for _, ch := range lines[i] {
if ch != EmptyBlock {
t.Errorf("Expected empty block in skyline, got '%c' on line %d", ch, i+1)
}
}
}
})
}
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ func generateSkyline(startYear, endYear int, targetUser string, full bool) error
lines := strings.Split(asciiArt, "\n")
gridStart := 0
for i, line := range lines {
if strings.Contains(line, string(ascii.EmptyBlock)) ||
strings.Contains(line, string(ascii.FoundationLow)) {
containsEmptyBlock := strings.Contains(line, string(ascii.EmptyBlock))
containsFoundationLow := strings.Contains(line, string(ascii.FoundationLow))
isNotOnlyEmptyBlocks := strings.Trim(line, string(ascii.EmptyBlock)) != ""

if (containsEmptyBlock || containsFoundationLow) && isNotOnlyEmptyBlocks {
gridStart = i
break
}
Expand Down

0 comments on commit bdd428f

Please sign in to comment.