-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncurses_display.cpp
109 lines (99 loc) · 3.71 KB
/
ncurses_display.cpp
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
#include <curses.h>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
#include "format.h"
#include "ncurses_display.h"
#include "system.h"
using std::string;
using std::to_string;
// 50 bars uniformly displayed from 0 - 100 %
// 2% is one bar(|)
std::string NCursesDisplay::ProgressBar(float percent) {
std::string result{"0%"};
int size{50};
float bars{percent * size};
for (int i{0}; i < size; ++i) {
result += i <= bars ? '|' : ' ';
}
string display{to_string(percent * 100).substr(0, 4)};
if (percent < 0.1 || percent == 1.0)
display = " " + to_string(percent * 100).substr(0, 3);
return result + " " + display + "/100%";
}
void NCursesDisplay::DisplaySystem(System& system, WINDOW* window) {
int row{0};
mvwprintw(window, ++row, 2, ("OS: " + system.OperatingSystem()).c_str());
mvwprintw(window, ++row, 2, ("Kernel: " + system.Kernel()).c_str());
mvwprintw(window, ++row, 2, "CPU: ");
wattron(window, COLOR_PAIR(1));
mvwprintw(window, row, 10, "");
wprintw(window, ProgressBar(system.Cpu().Utilization()).c_str());
wattroff(window, COLOR_PAIR(1));
mvwprintw(window, ++row, 2, "Memory: ");
wattron(window, COLOR_PAIR(1));
mvwprintw(window, row, 10, "");
wprintw(window, ProgressBar(system.MemoryUtilization()).c_str());
wattroff(window, COLOR_PAIR(1));
mvwprintw(window, ++row, 2,
("Total Processes: " + to_string(system.TotalProcesses())).c_str());
mvwprintw(
window, ++row, 2,
("Running Processes: " + to_string(system.RunningProcesses())).c_str());
mvwprintw(window, ++row, 2,
("Up Time: " + Format::ElapsedTime(system.UpTime())).c_str());
wrefresh(window);
}
void NCursesDisplay::DisplayProcesses(std::vector<Process>& processes,
WINDOW* window, int n) {
int row{0};
int const pid_column{2};
int const user_column{9};
int const cpu_column{19};
int const ram_column{29};
int const time_column{39};
int const command_column{54};
wattron(window, COLOR_PAIR(2));
mvwprintw(window, ++row, pid_column, "PID");
mvwprintw(window, row, user_column, "USER");
mvwprintw(window, row, cpu_column, "CPU[%%]");
mvwprintw(window, row, ram_column, "RAM[MB]");
mvwprintw(window, row, time_column, "TIME+");
mvwprintw(window, row, command_column, "COMMAND");
wattroff(window, COLOR_PAIR(2));
for (int i = 0; i < n; ++i) {
mvwprintw(window, ++row, pid_column, to_string(processes[i].Pid()).c_str());
mvwprintw(window, row, user_column, processes[i].User().c_str());
float cpu = processes[i].CpuUtilization() * 100;
mvwprintw(window, row, cpu_column, to_string(cpu).substr(0, 4).c_str());
mvwprintw(window, row, ram_column, processes[i].Ram().c_str());
mvwprintw(window, row, time_column,
Format::ElapsedTime(processes[i].UpTime()).c_str());
mvwprintw(window, row, command_column,
processes[i].Command().substr(0, window->_maxx - 46).c_str());
}
}
void NCursesDisplay::Display(System& system, int n) {
initscr(); // start ncurses
noecho(); // do not print input values
cbreak(); // terminate ncurses on ctrl + c
start_color(); // enable color
int x_max{getmaxx(stdscr)};
WINDOW* system_window = newwin(9, x_max - 1, 0, 0);
WINDOW* process_window =
newwin(3 + n, x_max - 1, system_window->_maxy + 1, 0);
while (1) {
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
box(system_window, 0, 0);
box(process_window, 0, 0);
DisplaySystem(system, system_window);
DisplayProcesses(system.Processes(), process_window, n);
wrefresh(system_window);
wrefresh(process_window);
refresh();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
endwin();
}