Merge pull request #93 from henrypotgieter/nerdfont_graph

Add option to display CPU graph as vertical bar chart
This commit is contained in:
Matt McCormick 2023-03-27 21:36:46 -04:00 committed by GitHub
commit f08fce0f83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 5 deletions

@ -144,6 +144,8 @@ The full usage::
Use tmux colors in output
-p, --powerline-left
Use powerline left symbols throughout the output, enables --colors
-v, --vertical-graph
Use vertical bar chart for CPU graph
-q, --powerline-right
Use powerline right symbols throughout the output, enables --colors
-i <value>, --interval <value>

@ -18,10 +18,11 @@
#include <string>
#include <cstring>
#include <map>
#include "graph.h"
std::string get_graph_by_percentage( unsigned value, unsigned len )
std::string get_graph_by_percentage( unsigned value, unsigned len )
{
unsigned step = 0;
std::string bars;
@ -60,3 +61,20 @@ std::string get_graph_by_value( unsigned value, unsigned max, unsigned len )
return bars;
}
std::string get_graph_vert( unsigned value )
{
static const std::map<unsigned, std::string> graph_chars = {
{ 0, " " }, { 10, "" }, { 20, "" }, { 30, "" }, { 40, "" },
{ 50, "" }, { 60, "" }, { 70, "" }, { 80, "" }, { 90, "" }
};
for( auto it = graph_chars.rbegin(); it != graph_chars.rend(); ++it )
{
if( value >= it->first )
{
return it->second;
}
}
return " "; // default return in case value doesn't match map options
}

@ -23,5 +23,6 @@
std::string get_graph_by_percentage( unsigned, unsigned len = 10 );
std::string get_graph_by_value( unsigned, unsigned, unsigned len = 10 );
std::string get_graph_vert( unsigned );
#endif

@ -37,7 +37,7 @@
std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false,
bool use_powerline_left = false, bool use_powerline_right = false )
bool use_powerline_left = false, bool use_powerline_right = false, bool use_vert_graph = false)
{
float percentage;
@ -80,7 +80,13 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
}
}
if( graph_lines > 0)
if( use_vert_graph )
{
oss << "";
oss << get_graph_vert( unsigned( percentage ) );
oss << "";
}
else if( graph_lines > 0)
{
oss << " [";
oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
@ -123,6 +129,8 @@ void print_help()
<< "\tUse powerline left symbols throughout the output, enables --colors\n"
<< "-q, --powerline-right\n"
<< "\tUse powerline right symbols throughout the output, enables --colors\n"
<< "-v, --vertical-graph\n"
<< "\tUse vertical bar chart for CPU graph\n"
<< "-i <value>, --interval <value>\n"
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\n"
@ -144,6 +152,7 @@ int main( int argc, char** argv )
bool use_colors = false;
bool use_powerline_left = false;
bool use_powerline_right = false;
bool use_vert_graph = false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
@ -157,6 +166,7 @@ int main( int argc, char** argv )
{ "colors", no_argument, NULL, 'c' },
{ "powerline-left", no_argument, NULL, 'p' },
{ "powerline-right", no_argument, NULL, 'q' },
{ "vertical-graph", no_argument, NULL, 'v' },
{ "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
@ -167,7 +177,7 @@ int main( int argc, char** argv )
int c;
// while c != -1
while( (c = getopt_long( argc, argv, "hi:cpqg:m:a:t:", long_options, NULL) ) != -1 )
while( (c = getopt_long( argc, argv, "hi:cpqvg:m:a:t:", long_options, NULL) ) != -1 )
{
switch( c )
{
@ -186,6 +196,9 @@ int main( int argc, char** argv )
use_colors = true;
use_powerline_right = true;
break;
case 'v': // --vertical-graph
use_vert_graph = true;
break;
case 'i': // --interval, -i
if( atoi( optarg ) < 1 )
{
@ -247,7 +260,7 @@ int main( int argc, char** argv )
MemoryStatus memory_status;
mem_status( memory_status );
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right )
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right )
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right, use_vert_graph )
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
std::cout << std::endl;