From 6178ba9aa304e8a588cea2d15b2e8d71025c497b Mon Sep 17 00:00:00 2001 From: "henry.potgieter" Date: Sat, 25 Mar 2023 11:07:55 -0400 Subject: [PATCH 1/4] Add option to display CPU graph as vertical bar chart --- README.rst | 2 ++ common/graph.cc | 20 +++++++++++++++++++- common/graph.h | 1 + common/main.cc | 21 +++++++++++++++++---- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 2e8df39..566594d 100644 --- a/README.rst +++ b/README.rst @@ -144,6 +144,8 @@ The full usage:: Use tmux colors in output -p, --powerline-left Use powerline left symbols throughout the output, enables --colors + -n, --nerd-graph + Use NerdFont characters to render CPU graph as vertical bar chart -q, --powerline-right Use powerline right symbols throughout the output, enables --colors -i , --interval diff --git a/common/graph.cc b/common/graph.cc index a8694e5..1e37d54 100644 --- a/common/graph.cc +++ b/common/graph.cc @@ -18,10 +18,11 @@ #include #include +#include #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_nerd( unsigned value ) +{ + static const std::map 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 +} diff --git a/common/graph.h b/common/graph.h index 4034161..ed97eb7 100644 --- a/common/graph.h +++ b/common/graph.h @@ -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_nerd( unsigned ); #endif diff --git a/common/main.cc b/common/main.cc index bab7597..b3eb6e3 100644 --- a/common/main.cc +++ b/common/main.cc @@ -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 nerd_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( nerd_graph ) + { + oss << "▕"; + oss << get_graph_nerd( 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" + << "-n, --nerd-graph\n" + << "\tUse NerdFont symbols to render CPU graph as vertical bar chart\n" << "-i , --interval \n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "-g , --graph-lines \n" @@ -146,6 +154,7 @@ int main( int argc, char** argv ) bool use_powerline_right = false; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; CPU_MODE cpu_mode = CPU_MODE_DEFAULT; + bool nerd_graph = false; static struct option long_options[] = { @@ -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' }, + { "nerd-graph", no_argument, NULL, 'n' }, { "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:cpqng: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 'n': // --nerd-graph + nerd_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, nerd_graph ) << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count ); std::cout << std::endl; From dc298e162a636630a3cd4e81370901cf56540d13 Mon Sep 17 00:00:00 2001 From: "henry.potgieter" Date: Sun, 26 Mar 2023 08:52:59 -0400 Subject: [PATCH 2/4] Prepend nerd_graph boolean with use_ so it doesn't look odd --- common/main.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/main.cc b/common/main.cc index b3eb6e3..37dfc6f 100644 --- a/common/main.cc +++ b/common/main.cc @@ -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 nerd_graph = false) + bool use_powerline_left = false, bool use_powerline_right = false, bool use_nerd_graph = false) { float percentage; @@ -80,7 +80,7 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne } } - if( nerd_graph ) + if( use_nerd_graph ) { oss << "▕"; oss << get_graph_nerd( unsigned( percentage ) ); @@ -152,9 +152,9 @@ int main( int argc, char** argv ) bool use_colors = false; bool use_powerline_left = false; bool use_powerline_right = false; + bool use_nerd_graph = false; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; CPU_MODE cpu_mode = CPU_MODE_DEFAULT; - bool nerd_graph = false; static struct option long_options[] = { @@ -197,7 +197,7 @@ int main( int argc, char** argv ) use_powerline_right = true; break; case 'n': // --nerd-graph - nerd_graph = true; + use_nerd_graph = true; break; case 'i': // --interval, -i if( atoi( optarg ) < 1 ) @@ -260,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, nerd_graph ) + << cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right, use_nerd_graph ) << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count ); std::cout << std::endl; From e232eef156d507d1b6148dbb2a5ab795e7d0f4a1 Mon Sep 17 00:00:00 2001 From: "henry.potgieter" Date: Mon, 27 Mar 2023 11:57:01 -0400 Subject: [PATCH 3/4] Change naming from --nerd-graph to --vertical-graph --- common/graph.cc | 2 +- common/graph.h | 2 +- common/main.cc | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/common/graph.cc b/common/graph.cc index 1e37d54..3659a0c 100644 --- a/common/graph.cc +++ b/common/graph.cc @@ -61,7 +61,7 @@ std::string get_graph_by_value( unsigned value, unsigned max, unsigned len ) return bars; } -std::string get_graph_nerd( unsigned value ) +std::string get_graph_vert( unsigned value ) { static const std::map graph_chars = { { 0, " " }, { 10, "▁" }, { 20, "▂" }, { 30, "▃" }, { 40, "▄" }, diff --git a/common/graph.h b/common/graph.h index ed97eb7..1f77c3a 100644 --- a/common/graph.h +++ b/common/graph.h @@ -23,6 +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_nerd( unsigned ); +std::string get_graph_vert( unsigned ); #endif diff --git a/common/main.cc b/common/main.cc index 37dfc6f..41536ba 100644 --- a/common/main.cc +++ b/common/main.cc @@ -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_nerd_graph = false) + bool use_powerline_left = false, bool use_powerline_right = false, bool use_vert_graph = false) { float percentage; @@ -80,10 +80,10 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne } } - if( use_nerd_graph ) + if( use_vert_graph ) { oss << "▕"; - oss << get_graph_nerd( unsigned( percentage ) ); + oss << get_graph_vert( unsigned( percentage ) ); oss << "▏"; } else if( graph_lines > 0) @@ -129,8 +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" - << "-n, --nerd-graph\n" - << "\tUse NerdFont symbols to render CPU graph as vertical bar chart\n" + << "-n, --vertical-graph\n" + << "\tUse vertical bar chart for CPU graph\n" << "-i , --interval \n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "-g , --graph-lines \n" @@ -152,7 +152,7 @@ int main( int argc, char** argv ) bool use_colors = false; bool use_powerline_left = false; bool use_powerline_right = false; - bool use_nerd_graph = false; + bool use_vert_graph = false; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; CPU_MODE cpu_mode = CPU_MODE_DEFAULT; @@ -166,7 +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' }, - { "nerd-graph", no_argument, NULL, 'n' }, + { "vertical-graph", no_argument, NULL, 'v' }, { "interval", required_argument, NULL, 'i' }, { "graph-lines", required_argument, NULL, 'g' }, { "mem-mode", required_argument, NULL, 'm' }, @@ -177,7 +177,7 @@ int main( int argc, char** argv ) int c; // while c != -1 - while( (c = getopt_long( argc, argv, "hi:cpqng:m:a:t:", long_options, NULL) ) != -1 ) + while( (c = getopt_long( argc, argv, "hi:cpqvg:m:a:t:", long_options, NULL) ) != -1 ) { switch( c ) { @@ -196,8 +196,8 @@ int main( int argc, char** argv ) use_colors = true; use_powerline_right = true; break; - case 'n': // --nerd-graph - use_nerd_graph = true; + case 'v': // --vertical-graph + use_vert_graph = true; break; case 'i': // --interval, -i if( atoi( optarg ) < 1 ) @@ -260,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, use_nerd_graph ) + << 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; From 94aed0352cbfa63091d341b07ebc6d8b33ba6cac Mon Sep 17 00:00:00 2001 From: "henry.potgieter" Date: Mon, 27 Mar 2023 12:21:36 -0400 Subject: [PATCH 4/4] Corrected a character in usage output and updated README.rst --- README.rst | 4 ++-- common/main.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 566594d..b4370d1 100644 --- a/README.rst +++ b/README.rst @@ -144,8 +144,8 @@ The full usage:: Use tmux colors in output -p, --powerline-left Use powerline left symbols throughout the output, enables --colors - -n, --nerd-graph - Use NerdFont characters to render CPU graph as vertical bar chart + -v, --vertical-graph + Use vertical bar chart for CPU graph -q, --powerline-right Use powerline right symbols throughout the output, enables --colors -i , --interval diff --git a/common/main.cc b/common/main.cc index 41536ba..e684467 100644 --- a/common/main.cc +++ b/common/main.cc @@ -129,7 +129,7 @@ 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" - << "-n, --vertical-graph\n" + << "-v, --vertical-graph\n" << "\tUse vertical bar chart for CPU graph\n" << "-i , --interval \n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n"