(
dailyActivity: DailyActivity[],
options: HeatmapOptions = {},
)
| 37 | * Generates a GitHub-style activity heatmap for the terminal |
| 38 | */ |
| 39 | export function generateHeatmap( |
| 40 | dailyActivity: DailyActivity[], |
| 41 | options: HeatmapOptions = {}, |
| 42 | ): string { |
| 43 | const { terminalWidth = 80, showMonthLabels = true } = options |
| 44 | |
| 45 | // Day labels take 4 characters ("Mon "), calculate weeks that fit |
| 46 | // Cap at 52 weeks (1 year) to match GitHub style |
| 47 | const dayLabelWidth = 4 |
| 48 | const availableWidth = terminalWidth - dayLabelWidth |
| 49 | const width = Math.min(52, Math.max(10, availableWidth)) |
| 50 | |
| 51 | // Build activity map by date |
| 52 | const activityMap = new Map<string, DailyActivity>() |
| 53 | for (const activity of dailyActivity) { |
| 54 | activityMap.set(activity.date, activity) |
| 55 | } |
| 56 | |
| 57 | // Pre-calculate percentiles once for all intensity lookups |
| 58 | const percentiles = calculatePercentiles(dailyActivity) |
| 59 | |
| 60 | // Calculate date range - end at today, go back N weeks |
| 61 | const today = new Date() |
| 62 | today.setHours(0, 0, 0, 0) |
| 63 | |
| 64 | // Find the Sunday of the current week (start of the week containing today) |
| 65 | const currentWeekStart = new Date(today) |
| 66 | currentWeekStart.setDate(today.getDate() - today.getDay()) |
| 67 | |
| 68 | // Go back (width - 1) weeks from the current week start |
| 69 | const startDate = new Date(currentWeekStart) |
| 70 | startDate.setDate(startDate.getDate() - (width - 1) * 7) |
| 71 | |
| 72 | // Generate grid (7 rows for days of week, width columns for weeks) |
| 73 | // Also track which week each month starts for labels |
| 74 | const grid: string[][] = Array.from({ length: 7 }, () => |
| 75 | Array(width).fill(''), |
| 76 | ) |
| 77 | const monthStarts: { month: number; week: number }[] = [] |
| 78 | let lastMonth = -1 |
| 79 | |
| 80 | const currentDate = new Date(startDate) |
| 81 | for (let week = 0; week < width; week++) { |
| 82 | for (let day = 0; day < 7; day++) { |
| 83 | // Don't show future dates |
| 84 | if (currentDate > today) { |
| 85 | grid[day]![week] = ' ' |
| 86 | currentDate.setDate(currentDate.getDate() + 1) |
| 87 | continue |
| 88 | } |
| 89 | |
| 90 | const dateStr = toDateString(currentDate) |
| 91 | const activity = activityMap.get(dateStr) |
| 92 | |
| 93 | // Track month changes (on day 0 = Sunday of each week) |
| 94 | if (day === 0) { |
| 95 | const month = currentDate.getMonth() |
| 96 | if (month !== lastMonth) { |
no test coverage detected