Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty contribution showing high-level blocks #54

Merged
merged 6 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading