Skip to content

Commit 95e1d4f

Browse files
committed
fix: right-align times and bracket suffixes in breakdown table
1 parent 2bc60d9 commit 95e1d4f

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/index.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ function formatMinutes(minutes: number): string {
232232
return `${hours}h ${mins}m`;
233233
}
234234

235+
/**
236+
* Format a project field for display by wrapping the suffix in brackets.
237+
* If the field already contains '[', it's returned as-is.
238+
* Otherwise, everything after the first space is treated as a suffix.
239+
* E.g. "test WSL: Ubuntu-24.04" → "test [WSL: Ubuntu-24.04]"
240+
*/
241+
function formatProjectName(field: string): string {
242+
if (field.includes("[")) return field;
243+
const spaceIdx = field.indexOf(" ");
244+
if (spaceIdx === -1) return field;
245+
const name = field.substring(0, spaceIdx);
246+
const suffix = field.substring(spaceIdx + 1);
247+
return `${name} [${suffix}]`;
248+
}
249+
235250
// ---- Plugin entry point ----
236251

237252
export const plugin: Plugin = async (ctx) => {
@@ -404,20 +419,34 @@ export const plugin: Plugin = async (ctx) => {
404419
0,
405420
);
406421

407-
// Find the longest project name for alignment
422+
// Pre-compute display names and formatted times
423+
const displayNames = projects.map((p) =>
424+
formatProjectName(p.field),
425+
);
426+
const formattedTimes = projects.map((p) =>
427+
formatMinutes(p.minutes),
428+
);
429+
const totalTime = formatMinutes(totalMinutes);
430+
431+
// Find the longest display name and time for alignment
408432
const maxNameLen = Math.max(
409-
...projects.map((p) => p.field.length),
433+
...displayNames.map((n) => n.length),
410434
"Total".length,
411435
);
436+
const maxTimeLen = Math.max(
437+
...formattedTimes.map((t) => t.length),
438+
totalTime.length,
439+
);
412440

413441
const lines = ["Today's coding time by project:", ""];
414-
for (const p of projects) {
415-
const name = p.field.padEnd(maxNameLen + 2);
416-
lines.push(` ${name}${formatMinutes(p.minutes)}`);
442+
for (let i = 0; i < projects.length; i++) {
443+
const name = displayNames[i].padEnd(maxNameLen + 2);
444+
const time = formattedTimes[i].padStart(maxTimeLen);
445+
lines.push(` ${name}${time}`);
417446
}
418-
lines.push(` ${"─".repeat(maxNameLen + 2 + 8)}`);
447+
lines.push(` ${"─".repeat(maxNameLen + 2 + maxTimeLen)}`);
419448
lines.push(
420-
` ${"Total".padEnd(maxNameLen + 2)}${formatMinutes(totalMinutes)}`,
449+
` ${"Total".padEnd(maxNameLen + 2)}${totalTime.padStart(maxTimeLen)}`,
421450
);
422451

423452
return lines.join("\n");

0 commit comments

Comments
 (0)