Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import chalk from 'chalk'
import type { DailyActivity } from './stats.js'
import { toDateString } from './statsCache.js'
export type HeatmapOptions = {
terminalWidth?: number // Terminal width in characters
showMonthLabels?: boolean
}
type Percentiles = {
p25: number
p50: number
p75: number
}
/**
* Pre-calculates percentiles from activity data for use in intensity calculations
*/
function calculatePercentiles(
dailyActivity: DailyActivity[],
): Percentiles | null {
const counts = dailyActivity
.map(a => a.messageCount)
.filter(c => c > 0)
.sort((a, b) => a - b)
if (counts.length === 0) return null
return {
p25: counts[Math.floor(counts.length * 0.25)]!,
p50: counts[Math.floor(counts.length * 0.5)]!,
p75: counts[Math.floor(counts.length * 0.75)]!,
}
}
/**
* Generates a GitHub-style activity heatmap for the terminal
*/
export function generateHeatmap(
dailyActivity: DailyActivity[],
options: HeatmapOptions = {},
): string {
const { terminalWidth = 80, showMonthLabels = true } = options
// Day labels take 4 characters ("Mon "), calculate weeks that fit
// Cap at 52 weeks (1 year) to match GitHub style
const dayLabelWidth = 4
const availableWidth = terminalWidth - dayLabelWidth
const width = Math.min(52, Math.max(10, availableWidth))
// Build activity map by date
const activityMap = new Map<string, DailyActivity>()
for (const activity of dailyActivity) {
activityMap.set(activity.date, activity)
}
// Pre-calculate percentiles once for all intensity lookups
const percentiles = calculatePercentiles(dailyActivity)
// Calculate date range - end at today, go back N weeks
const today = new Date()
today.setHours(0, 0, 0, 0)
// Find the Sunday of the current week (start of the week containing today)
const currentWeekStart = new Date(today)
currentWeekStart.setDate(today.getDate() - today.getDay())
// Go back (width - 1) weeks from the current week start
const startDate = new Date(currentWeekStart)
startDate.setDate(startDate.getDate() - (width - 1) * 7)
// Generate grid (7 rows for days of week, width columns for weeks)
// Also track which week each month starts for labels
const grid: string[][] = Array.from({ length: 7 }, () =>
Array(width).fill(''),
)
const monthStarts: { month: number; week: number }[] = []
let lastMonth = -1
const currentDate = new Date(startDate)
for (let week = 0; week < width; week++) {
for (let day = 0; day < 7; day++) {
// Don't show future dates
if (currentDate > today) {
grid[day]![week] = ' '
currentDate.setDate(currentDate.getDate() + 1)
continue
}
const dateStr = toDateString(currentDate)
const activity = activityMap.get(dateStr)
// Track month changes (on day 0 = Sunday of each week)
if (day === 0) {
const month = currentDate.getMonth()
if (month !== lastMonth) {
monthStarts.push({ month, week })
lastMonth = month
}
}
// Determine intensity level based on message count
const intensity = getIntensity(activity?.messageCount || 0, percentiles)
grid[day]![week] = getHeatmapChar(intensity)
currentDate.setDate(currentDate.getDate() + 1)
}
}
// Build output
const lines: string[] = []
// Month labels - evenly spaced across the grid
if (showMonthLabels) {
const monthNames = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
// Build label line with fixed-width month labels
const uniqueMonths = monthStarts.map(m => m.month)
const labelWidth = Math.floor(width / Math.max(uniqueMonths.length, 1))
const monthLabels = uniqueMonths
.map(month => monthNames[month]!.padEnd(labelWidth))
.join('')
// 4 spaces for day label column prefix
lines.push(' ' + monthLabels)
}
// Day labels
const dayLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
// Grid
for (let day = 0; day < 7; day++) {
// Only show labels for Mon, Wed, Fri
const label = [1, 3, 5].includes(day) ? dayLabels[day]!.padEnd(3) : ' '
const row = label + ' ' + grid[day]!.join('')
lines.push(row)
}
// Legend
lines.push('')
lines.push(
' Less ' +
[
clawdBlue('░'),
clawdBlue('▒'),
clawdBlue('▓'),
clawdBlue('█'),
].join(' ') +
' More',
)
return lines.join('\n')
}
function getIntensity(
messageCount: number,
percentiles: Percentiles | null,
): number {
if (messageCount === 0 || !percentiles) return 0
if (messageCount >= percentiles.p75) return 4
if (messageCount >= percentiles.p50) return 3
if (messageCount >= percentiles.p25) return 2
return 1
}
// Mervelas blue (hex #3b82f6)
const clawdBlue = chalk.hex('#3b82f6')
function getHeatmapChar(intensity: number): string {
switch (intensity) {
case 0:
return chalk.gray('·')
case 1:
return clawdBlue('░')
case 2:
return clawdBlue('▒')
case 3:
return clawdBlue('▓')
case 4:
return clawdBlue('█')
default:
return chalk.gray('·')
}
}