-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (131 loc) · 4.87 KB
/
Copy pathProgram.cs
File metadata and controls
158 lines (131 loc) · 4.87 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
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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Pulse;
static class Program
{
static void Main(string[] args)
{
bool once = args.Contains("--once") || args.Contains("-1");
int interval = 1000;
for (int i = 0; i < args.Length; i++)
if ((args[i] == "-n" || args[i] == "--interval") && i + 1 < args.Length && int.TryParse(args[i + 1], out int ms))
interval = ms * 1000;
Console.OutputEncoding = System.Text.Encoding.UTF8;
try { Console.CursorVisible = false; } catch { }
// prime CPU counter — first read is always 0
_ = GetCpuPercent();
Thread.Sleep(500);
try
{
while (true)
{
if (!once) Console.Clear();
Render();
if (once) break;
Thread.Sleep(interval);
}
}
finally
{
try { Console.CursorVisible = true; } catch { }
}
}
static void Render()
{
float cpu = GetCpuPercent();
var (totalRam, usedRam) = GetRam();
var drives = GetDrives();
int uptime = GetUptimeSeconds();
Console.WriteLine();
Console.WriteLine($" {Bold("pulse")} {Dim(DateTime.Now.ToString("HH:mm:ss"))} {Dim("uptime " + FormatUptime(uptime))}");
Console.WriteLine();
// CPU
Console.Write($" {"cpu",-6} ");
PrintBar(cpu / 100f, 30);
Console.WriteLine($" {cpu,5:F1}%");
// RAM
float ramPct = totalRam > 0 ? (float)usedRam / totalRam : 0;
Console.Write($" {"ram",-6} ");
PrintBar(ramPct, 30);
Console.WriteLine($" {ramPct * 100,5:F1}% {FormatBytes(usedRam)} / {FormatBytes(totalRam)}");
Console.WriteLine();
// Drives
if (drives.Count > 0)
{
Console.WriteLine($" {Dim("disk")}");
foreach (var d in drives)
{
float pct = d.total > 0 ? (float)(d.total - d.free) / d.total : 0;
Console.Write($" {d.name,-6} ");
PrintBar(pct, 30);
Console.WriteLine($" {pct * 100,5:F1}% {FormatBytes(d.total - d.free)} / {FormatBytes(d.total)}");
}
}
Console.WriteLine();
Console.WriteLine($" {Dim("ctrl+c to exit")}");
}
static void PrintBar(float pct, int width)
{
int filled = (int)(width * Math.Clamp(pct, 0, 1));
string fill = new string('█', filled);
string empty = new string('░', width - filled);
// green → yellow → red based on pct
string color = pct < 0.6f ? "\x1b[38;2;80;220;120m"
: pct < 0.85f ? "\x1b[38;2;255;200;60m"
: "\x1b[38;2;255;80;80m";
Console.Write($"[{color}{fill}\x1b[0m\x1b[2m{empty}\x1b[0m]");
}
static string FormatBytes(long bytes)
{
if (bytes >= 1L << 30) return $"{bytes / (1 << 30):F1} GB";
if (bytes >= 1L << 20) return $"{bytes / (1 << 20):F0} MB";
return $"{bytes / (1 << 10):F0} KB";
}
static string FormatUptime(int seconds)
{
int h = seconds / 3600;
int m = (seconds % 3600) / 60;
return h > 0 ? $"{h}h {m}m" : $"{m}m";
}
static string Bold(string s) => $"\x1b[1m{s}\x1b[0m";
static string Dim(string s) => $"\x1b[2m{s}\x1b[0m";
// ---- win32 ----
[DllImport("kernel32")] static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
[StructLayout(LayoutKind.Sequential)]
struct MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys, ullAvailPhys;
public ulong ullTotalPageFile, ullAvailPageFile;
public ulong ullTotalVirtual, ullAvailVirtual, ullAvailExtendedVirtual;
}
[DllImport("kernel32")] static extern ulong GetTickCount64();
static PerformanceCounter? cpuCounter;
static float GetCpuPercent()
{
cpuCounter ??= new PerformanceCounter("Processor", "% Processor Time", "_Total");
return cpuCounter.NextValue();
}
static (long total, long used) GetRam()
{
var mem = new MEMORYSTATUSEX { dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>() };
if (!GlobalMemoryStatusEx(ref mem)) return (0, 0);
return ((long)mem.ullTotalPhys, (long)(mem.ullTotalPhys - mem.ullAvailPhys));
}
static List<(string name, long total, long free)> GetDrives()
{
var result = new List<(string, long, long)>();
foreach (var d in DriveInfo.GetDrives())
{
try
{
if (d.IsReady && d.DriveType == DriveType.Fixed)
result.Add((d.Name.TrimEnd('\\'), d.TotalSize, d.AvailableFreeSpace));
}
catch { }
}
return result;
}
static int GetUptimeSeconds() => (int)(GetTickCount64() / 1000);
}