diff --git a/ascii/generator.go b/ascii/generator.go index fa72b6c..87954dc 100644 --- a/ascii/generator.go +++ b/ascii/generator.go @@ -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) } } diff --git a/ascii/generator_test.go b/ascii/generator_test.go index 8e547d5..4ee56c9 100644 --- a/ascii/generator_test.go +++ b/ascii/generator_test.go @@ -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) + } + } + } + }) + } +} diff --git a/main.go b/main.go index 2f3be30..f4c1f49 100644 --- a/main.go +++ b/main.go @@ -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 }