Spaces:
Running

File size: 4,499 Bytes
6f5f15e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { ClientIdentity } from '../../shared/api-types';

/**
 * Format an ISO timestamp relative to `nowMs` (e.g. "just now", "4s ago", "3m ago", "2h ago").
 */
export function relativeTime(iso: string | undefined | null, nowMs: number = Date.now()): string {
  if (!iso) return 'β€”';
  const ts = Date.parse(iso);
  if (Number.isNaN(ts)) return 'β€”';
  const diffSec = Math.round((nowMs - ts) / 1000);
  if (diffSec < 0) return 'just now';
  if (diffSec < 2) return 'just now';
  if (diffSec < 60) return `${diffSec}s ago`;
  const diffMin = Math.floor(diffSec / 60);
  if (diffMin < 60) return `${diffMin}m ago`;
  const diffHr = Math.floor(diffMin / 60);
  if (diffHr < 24) return `${diffHr}h ago`;
  const diffDay = Math.floor(diffHr / 24);
  return `${diffDay}d ago`;
}

/**
 * Format an elapsed duration from `startedIso` to `nowMs` (e.g. "12s", "4m 18s", "2h 10m").
 */
export function formatUptime(startedIso: string | undefined | null, nowMs: number = Date.now()): string {
  if (!startedIso) return 'β€”';
  const ts = Date.parse(startedIso);
  if (Number.isNaN(ts)) return 'β€”';
  const totalSec = Math.max(0, Math.floor((nowMs - ts) / 1000));
  if (totalSec < 60) return `${totalSec}s`;
  const mins = Math.floor(totalSec / 60);
  const secs = totalSec % 60;
  if (mins < 60) return secs > 0 ? `${mins}m ${secs}s` : `${mins}m`;
  const hrs = Math.floor(mins / 60);
  const remMins = mins % 60;
  return remMins > 0 ? `${hrs}h ${remMins}m` : `${hrs}h`;
}

/**
 * Format a compact local wall-clock time (HH:MM:SS).
 */
export function formatClock(iso: string | undefined | null): string {
  if (!iso) return 'β€”';
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return 'β€”';
  return d.toLocaleTimeString(undefined, {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  });
}

/**
 * Shorten an event/stream/tab identifier while keeping it recognizable.
 */
export function shortId(id: string | undefined | null, maxLen = 14): string {
  if (!id) return 'β€”';
  if (id.length <= maxLen) return id;
  const keepHead = Math.max(4, Math.ceil((maxLen - 1) * 0.6));
  const keepTail = Math.max(3, maxLen - 1 - keepHead);
  return `${id.slice(0, keepHead)}…${id.slice(-keepTail)}`;
}

/**
 * Human-readable byte size (e.g. "42.3 KB", "1.4 MB").
 */
export function formatBytes(bytes: number | undefined | null): string {
  if (bytes == null || !Number.isFinite(bytes) || bytes < 0) return '0 B';
  if (bytes < 1024) return `${Math.round(bytes)} B`;
  const kb = bytes / 1024;
  if (kb < 1024) return `${kb < 10 ? kb.toFixed(1) : Math.round(kb)} KB`;
  const mb = kb / 1024;
  return `${mb.toFixed(1)} MB`;
}

/**
 * Compact display for a cursor string (keeps head and trailing seq/suffix).
 */
export function prettyCursor(cursor: string | undefined | null): string {
  if (!cursor) return 'βˆ…';
  if (cursor.length <= 16) return cursor;
  return `${cursor.slice(0, 7)}…${cursor.slice(-6)}`;
}

/**
 * Format duration in milliseconds cleanly (e.g. "15s", "3s", "500ms").
 */
export function formatMs(ms: number | undefined | null): string {
  if (ms == null || !Number.isFinite(ms)) return 'β€”';
  if (ms < 1000) return `${Math.round(ms)}ms`;
  const sec = ms / 1000;
  return Number.isInteger(sec) ? `${sec}s` : `${sec.toFixed(1)}s`;
}

/**
 * Parse a concise browser/OS label from a User-Agent string.
 */
export function parseBrowser(ua: string | undefined | null): string {
  if (!ua) return 'Unknown browser';
  const browser = /Edg\//.test(ua)
    ? 'Edge'
    : /Firefox\//.test(ua)
      ? 'Firefox'
      : /Chrome\//.test(ua) && !/Chromium\//.test(ua)
        ? 'Chrome'
        : /Safari\//.test(ua) && !/Chrome\//.test(ua)
          ? 'Safari'
          : 'Browser';

  const os = /Mac OS X|Macintosh/.test(ua)
    ? 'macOS'
    : /Windows/.test(ua)
      ? 'Windows'
      : /Linux/.test(ua)
        ? 'Linux'
        : /iPhone|iPad/.test(ua)
          ? 'iOS'
          : /Android/.test(ua)
            ? 'Android'
            : '';

  return os ? `${browser} Β· ${os}` : browser;
}

/**
 * Display label for a ClientIdentity.
 */
export function formatClientName(client: ClientIdentity): string {
  if (client.title) return client.title;
  if (client.name && client.version) return `${client.name} ${client.version}`;
  if (client.name) return client.name;
  if (client.userAgent) return shortId(client.userAgent, 24);
  if (client.transport === 'stdio') return 'stdio client';
  return shortId(client.key, 18);
}