-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (124 loc) · 5.42 KB
/
main.cpp
File metadata and controls
156 lines (124 loc) · 5.42 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
#include "SysInfo.h"
#include "ProcessContainer.h"
#include "Process.h"
#include "ProcessParser.h"
#include "util.h"
#include <ncurses.h>
#include <thread>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
class NCursesDisplay {
public:
static void Display(SysInfo& sys, ProcessContainer& procs, int maxProcesses = 10) {
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
nodelay(stdscr, TRUE);
timeout(100);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_WHITE, COLOR_BLACK);
init_pair(4, COLOR_YELLOW, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
int maxY, maxX, prevMaxY = 0, prevMaxX = 0;
getmaxyx(stdscr, maxY, maxX);
WINDOW* sysWin = newwin(12, maxX - 4, 1, 2);
wbkgd(sysWin, COLOR_PAIR(1));
box(sysWin, ACS_VLINE, ACS_HLINE);
mvwprintw(sysWin, 0, 2, " System Overview ");
WINDOW* procWin = newwin(maxY - 15, maxX - 4, 14, 2);
wbkgd(procWin, COLOR_PAIR(2));
box(procWin, ACS_VLINE, ACS_HLINE);
mvwprintw(procWin, 0, 2, " Process List ");
WINDOW* timeWin = newwin(2, 20, 1, maxX - 22);
wbkgd(timeWin, COLOR_PAIR(5));
box(timeWin, ACS_VLINE, ACS_HLINE);
mvwprintw(timeWin, 0, 2, " Current Time ");
int loopCount = 0;
while (true) {
getmaxyx(stdscr, maxY, maxX);
if (maxY != prevMaxY || maxX != prevMaxX) {
wresize(sysWin, 12, maxX - 4);
mvwin(sysWin, 1, 2);
wresize(procWin, maxY - 15, maxX - 4);
mvwin(procWin, 14, 2);
wresize(timeWin, 2, 20);
mvwin(timeWin, 1, maxX - 22);
prevMaxY = maxY;
prevMaxX = maxX;
clear();
}
procs.refreshList();
sys.setAttributes();
std::cerr << "CPU: [" << sys.getCpuPercent() << "]\n";
std::cerr << "Mem: [" << sys.getMemPercent() << "]\n";
wclear(sysWin);
box(sysWin, ACS_VLINE, ACS_HLINE);
mvwprintw(sysWin, 0, 2, " System Overview ");
wattron(sysWin, COLOR_PAIR(1));
mvwprintw(sysWin, 2, 2, "OS: %s", sys.getOsName().c_str());
mvwprintw(sysWin, 3, 2, "Kernel: %s", sys.getKernelVersion().c_str());
mvwprintw(sysWin, 4, 2, "CPU: %s", Util::getProgressBar(sys.getCpuPercent()).c_str());
mvwprintw(sysWin, 5, 2, "Memory: %s", Util::getProgressBar(sys.getMemPercent()).c_str());
mvwprintw(sysWin, 6, 2, "Total Processes: %s", sys.getTotalProc().c_str());
mvwprintw(sysWin, 7, 2, "Running Processes: %s", sys.getRunningProc().c_str());
mvwprintw(sysWin, 8, 2, "Threads: %s", sys.getThreads().c_str());
mvwprintw(sysWin, 9, 2, "Uptime: %s", Util::convertTime(sys.getUpTime()).c_str());
wattroff(sysWin, COLOR_PAIR(1));
wclear(procWin);
box(procWin, ACS_VLINE, ACS_HLINE);
mvwprintw(procWin, 0, 2, " Process List ");
wattron(procWin, COLOR_PAIR(2));
mvwprintw(procWin, 2, 2, "PID USER CPU%% RAM(MB) UPTIME COMMAND");
wattroff(procWin, COLOR_PAIR(2));
auto processes = procs.getList();
std::cerr << "Loop " << loopCount++ << ": Displaying " << std::min(maxProcesses, static_cast<int>(processes.size())) << " processes\n";
int row = 3;
for (int i = 0; i < std::min(maxProcesses, static_cast<int>(processes.size())); ++i) {
wattron(procWin, COLOR_PAIR(3));
if (processes[i].find("[Terminated]") != std::string::npos) {
wattron(procWin, COLOR_PAIR(4));
}
mvwprintw(procWin, row++, 2, "%s", processes[i].c_str());
wattroff(procWin, COLOR_PAIR(3));
wattroff(procWin, COLOR_PAIR(4));
}
time_t now = time(nullptr);
struct tm* timeInfo = localtime(&now);
wclear(timeWin);
box(timeWin, ACS_VLINE, ACS_HLINE);
mvwprintw(timeWin, 0, 2, " EGYPT Time ");
wattron(timeWin, COLOR_PAIR(5));
int hour12 = timeInfo->tm_hour % 12;
if (hour12 == 0) hour12 = 12;
const char* ampm = (timeInfo->tm_hour >= 12) ? "PM" : "AM";
mvwprintw(timeWin, 1, 2, "%02d:%02d:%02d %s",
hour12, timeInfo->tm_min, timeInfo->tm_sec, ampm);
wattroff(timeWin, COLOR_PAIR(5));
wrefresh(sysWin);
wrefresh(procWin);
wrefresh(timeWin);
refresh();
int ch = getch();
if (ch == 'q' || ch == 'Q') break;
if (ch != ERR) std::cerr << "Key pressed: " << ch << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
delwin(sysWin);
delwin(procWin);
delwin(timeWin);
endwin();
}
};
int main() {
ProcessContainer procs;
SysInfo sys;
std::cerr << "Starting monitor with " << procs.getList().size() << " initial processes\n";
NCursesDisplay::Display(sys, procs);
return 0;
}