Skip to content

Commit f08fce0

Browse files
authored
Merge pull request #93 from henrypotgieter/nerdfont_graph
Add option to display CPU graph as vertical bar chart
2 parents 73b7f6c + 94aed03 commit f08fce0

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ The full usage::
144144
Use tmux colors in output
145145
-p, --powerline-left
146146
Use powerline left symbols throughout the output, enables --colors
147+
-v, --vertical-graph
148+
Use vertical bar chart for CPU graph
147149
-q, --powerline-right
148150
Use powerline right symbols throughout the output, enables --colors
149151
-i <value>, --interval <value>

common/graph.cc

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
#include <string>
2020
#include <cstring>
21+
#include <map>
2122

2223
#include "graph.h"
2324

24-
std::string get_graph_by_percentage( unsigned value, unsigned len )
25+
std::string get_graph_by_percentage( unsigned value, unsigned len )
2526
{
2627
unsigned step = 0;
2728
std::string bars;
@@ -60,3 +61,20 @@ std::string get_graph_by_value( unsigned value, unsigned max, unsigned len )
6061
return bars;
6162
}
6263

64+
std::string get_graph_vert( unsigned value )
65+
{
66+
static const std::map<unsigned, std::string> graph_chars = {
67+
{ 0, " " }, { 10, "" }, { 20, "" }, { 30, "" }, { 40, "" },
68+
{ 50, "" }, { 60, "" }, { 70, "" }, { 80, "" }, { 90, "" }
69+
};
70+
71+
for( auto it = graph_chars.rbegin(); it != graph_chars.rend(); ++it )
72+
{
73+
if( value >= it->first )
74+
{
75+
return it->second;
76+
}
77+
}
78+
79+
return " "; // default return in case value doesn't match map options
80+
}

common/graph.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323

2424
std::string get_graph_by_percentage( unsigned, unsigned len = 10 );
2525
std::string get_graph_by_value( unsigned, unsigned, unsigned len = 10 );
26+
std::string get_graph_vert( unsigned );
2627

2728
#endif

common/main.cc

+17-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines,
3939
bool use_colors = false,
40-
bool use_powerline_left = false, bool use_powerline_right = false )
40+
bool use_powerline_left = false, bool use_powerline_right = false, bool use_vert_graph = false)
4141
{
4242

4343
float percentage;
@@ -80,7 +80,13 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
8080
}
8181
}
8282

83-
if( graph_lines > 0)
83+
if( use_vert_graph )
84+
{
85+
oss << "";
86+
oss << get_graph_vert( unsigned( percentage ) );
87+
oss << "";
88+
}
89+
else if( graph_lines > 0)
8490
{
8591
oss << " [";
8692
oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
@@ -123,6 +129,8 @@ void print_help()
123129
<< "\tUse powerline left symbols throughout the output, enables --colors\n"
124130
<< "-q, --powerline-right\n"
125131
<< "\tUse powerline right symbols throughout the output, enables --colors\n"
132+
<< "-v, --vertical-graph\n"
133+
<< "\tUse vertical bar chart for CPU graph\n"
126134
<< "-i <value>, --interval <value>\n"
127135
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
128136
<< "-g <value>, --graph-lines <value>\n"
@@ -144,6 +152,7 @@ int main( int argc, char** argv )
144152
bool use_colors = false;
145153
bool use_powerline_left = false;
146154
bool use_powerline_right = false;
155+
bool use_vert_graph = false;
147156
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
148157
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
149158

@@ -157,6 +166,7 @@ int main( int argc, char** argv )
157166
{ "colors", no_argument, NULL, 'c' },
158167
{ "powerline-left", no_argument, NULL, 'p' },
159168
{ "powerline-right", no_argument, NULL, 'q' },
169+
{ "vertical-graph", no_argument, NULL, 'v' },
160170
{ "interval", required_argument, NULL, 'i' },
161171
{ "graph-lines", required_argument, NULL, 'g' },
162172
{ "mem-mode", required_argument, NULL, 'm' },
@@ -167,7 +177,7 @@ int main( int argc, char** argv )
167177

168178
int c;
169179
// while c != -1
170-
while( (c = getopt_long( argc, argv, "hi:cpqg:m:a:t:", long_options, NULL) ) != -1 )
180+
while( (c = getopt_long( argc, argv, "hi:cpqvg:m:a:t:", long_options, NULL) ) != -1 )
171181
{
172182
switch( c )
173183
{
@@ -186,6 +196,9 @@ int main( int argc, char** argv )
186196
use_colors = true;
187197
use_powerline_right = true;
188198
break;
199+
case 'v': // --vertical-graph
200+
use_vert_graph = true;
201+
break;
189202
case 'i': // --interval, -i
190203
if( atoi( optarg ) < 1 )
191204
{
@@ -247,7 +260,7 @@ int main( int argc, char** argv )
247260
MemoryStatus memory_status;
248261
mem_status( memory_status );
249262
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right )
250-
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right )
263+
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right, use_vert_graph )
251264
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
252265

253266
std::cout << std::endl;

0 commit comments

Comments
 (0)