-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryBar.tsx
More file actions
50 lines (46 loc) · 1.48 KB
/
MemoryBar.tsx
File metadata and controls
50 lines (46 loc) · 1.48 KB
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
interface MemoryBarProps {
label: string;
used: number;
total: number;
percent: number;
}
function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
export function MemoryBar({ label, used, total, percent }: MemoryBarProps) {
const getBarColor = (usage: number): string => {
if (usage < 50) return 'var(--color-green)';
if (usage < 75) return 'var(--color-yellow)';
if (usage < 90) return 'var(--color-orange)';
return 'var(--color-red)';
};
const renderBar = (usage: number, maxBlocks: number = 30): string => {
const filled = Math.round((usage / 100) * maxBlocks);
const blocks = '|'.repeat(filled);
const empty = ' '.repeat(maxBlocks - filled);
return blocks + empty;
};
return (
<div className="memory-bar">
<span className="mem-label">{label}</span>
<span className="bar-bracket">[</span>
<span
className="bar-fill"
style={{ color: getBarColor(percent) }}
>
{renderBar(percent)}
</span>
<span className="bar-bracket">]</span>
<span className="mem-info">
{formatBytes(used)}/{formatBytes(total)}
</span>
<span className="mem-percent" style={{ color: getBarColor(percent) }}>
{percent.toString().padStart(3, ' ')}%
</span>
</div>
);
}