-
Notifications
You must be signed in to change notification settings - Fork 17
/
ConsoleReporter.cpp
116 lines (106 loc) · 3.04 KB
/
ConsoleReporter.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
110
111
112
113
114
115
116
#include "PendingCommand.h"
#include "Reporter.h"
#include <cstdio>
#include <iostream>
#include <string>
#ifndef _WIN32
# include <signal.h>
# include <sys/ioctl.h>
# include <unistd.h>
#endif
static size_t screenWidth = 80;
#ifndef _WIN32
static void fetchDisplaySize()
{
# ifdef TIOCGSIZE
struct ttysize ts;
ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
screenWidth = ts.ts_cols;
# elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
screenWidth = ts.ws_col;
// get a minimum set
if(screenWidth <= 0)
screenWidth = 10;
# endif /* TIOCGSIZE */
}
#endif
ConsoleReporter::ConsoleReporter()
{
#ifndef _WIN32
fetchDisplaySize();
signal(SIGWINCH, [](int) { fetchDisplaySize(); });
#endif
}
void ConsoleReporter::Redraw()
{
size_t w = screenWidth / activeProcesses.size();
size_t active = 0;
for(auto &t : activeProcesses)
if(t)
active++;
std::cout << activeProcesses.size() << " concurrent tasks, " << active << " active\n";
if(w == 0)
{
// If you have more cores than horizontal characters, we can't display one task per character. Instead make the horizontal line a "usage" bar representing proportional task use.
size_t activeCount = 0;
for(auto &t : activeProcesses)
{
if(t)
activeCount++;
}
std::cout << std::string((screenWidth - 1) * activeCount / activeProcesses.size(), '*') << std::string(screenWidth - (screenWidth - 1) * activeCount / activeProcesses.size() - 1, ' ');
}
else
{
for(auto &t : activeProcesses)
{
if(w >= 10)
{
std::string file;
if(t)
{
file = t->inputs[0]->path.generic_string();
if(file.size() > w - 2)
file.resize(w - 2);
}
while(file.size() < w - 2)
file.push_back(' ');
std::cout << "\033[1;37m[\033[0m" << file << "\033[1;37m]\033[0m";
}
else if(w >= 3)
{
std::cout << "\033[1;37m[\033[0m" << (t ? "*" : "_") << "\033[1;37m]\033[0m";
}
else if(w >= 1)
{
std::cout << "\033[1;37m" << (t ? "*" : "_") << "\033[0m";
}
}
}
std::cout << "\r\033[1A" << std::flush;
}
void ConsoleReporter::SetConcurrencyCount(size_t count)
{
activeProcesses.resize(count);
Redraw();
}
void ConsoleReporter::SetRunningCommand(size_t channel, std::shared_ptr<PendingCommand> command)
{
activeProcesses[channel] = command;
Redraw();
}
void ConsoleReporter::ReportCommand(size_t , std::shared_ptr<PendingCommand> cmd)
{
if (cmd) {
// Display error
if(cmd->result->errorcode)
{
std::cout << "\n\n"
<< "Error while running: " << cmd->commandToRun;
std::cout << "\n\n"
<< cmd->result->output << "\n\n";
}
}
}