// 렌더러 공용 유틸 (포맷 · 병합) export function deepMerge(base: T, over: unknown): T { if (over === undefined || over === null || typeof base !== 'object' || base === null || Array.isArray(base)) { return (over ?? base) as T; } const out: Record = { ...(base as Record) }; for (const [k, v] of Object.entries(over as Record)) { const b = out[k]; out[k] = b && typeof b === 'object' && !Array.isArray(b) ? deepMerge(b, v) : v; } return out as T; } export function errMsg(e: unknown): string { const m = e instanceof Error ? e.message : String(e); // IPC 오류는 "Error invoking remote method 'gvr:x': Error: 메시지" 로 온다 return m.replace(/^Error invoking remote method '[^']+': (?:\w*Error: )?/, ''); } /** "3분 전" · "2시간 전" · "4일 전" */ export function fmtAgo(ts: number | null | undefined): string { if (!ts) return '-'; const s = Math.max(0, (Date.now() - ts) / 1000); if (s < 60) return '방금'; if (s < 3600) return `${Math.floor(s / 60)}분 전`; if (s < 86400) return `${Math.floor(s / 3600)}시간 전`; return `${Math.floor(s / 86400)}일 전`; } /** MB → "1.6GB" / "464MB" */ export function fmtMb(mb: number, digits = 1): string { if (mb >= 1000) return `${(mb / 1024).toFixed(digits)}GB`; return `${Math.round(mb)}MB`; } export function fmtBytes(b: number | null | undefined): string { if (b === null || b === undefined) return '-'; if (b >= 1024 ** 3) return `${(b / 1024 ** 3).toFixed(1)}GB`; if (b >= 1024 ** 2) return `${(b / 1024 ** 2).toFixed(0)}MB`; if (b >= 1024) return `${(b / 1024).toFixed(0)}KB`; return `${b}B`; } export function fmtEta(sec: number | undefined): string { if (sec === undefined || !isFinite(sec)) return ''; const s = Math.max(0, Math.round(sec)); if (s < 60) return `${s}초`; const m = Math.floor(s / 60); if (m < 60) return `${m}분 ${s % 60}초`; return `${Math.floor(m / 60)}시간 ${m % 60}분`; } export function fmtClock(ts: number): string { const d = new Date(ts); return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`; } export function fmtDuration(ms: number): string { const s = Math.max(0, Math.floor(ms / 1000)); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); return h ? `${h}시간 ${m}분` : `${m}분 ${s % 60}초`; } export function fmtDate(ts: number): string { const d = new Date(ts); return `${d.getMonth() + 1}/${d.getDate()} ${fmtClock(ts)}`; } export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); export function go(hash: string) { if (location.hash !== hash) location.hash = hash; }