From f9663c8f2fb955e0475d2896576a4f5901be160c Mon Sep 17 00:00:00 2001 From: "henry.potgieter" Date: Mon, 27 Mar 2023 11:11:55 -0400 Subject: [PATCH] Add options to allow embedding tmux-mem-cpu-load segments within/beside other segments --- common/load.cc | 16 +++++++++++++--- common/load.h | 3 ++- common/main.cc | 34 ++++++++++++++++++++++++++++++--- common/memory.cc | 16 ++++++++++++++-- common/memory.h | 4 +++- common/powerline.cc | 46 +++++++++++++++++++++++++++++++++++++++++++++ common/powerline.h | 2 ++ 7 files changed, 111 insertions(+), 10 deletions(-) diff --git a/common/load.cc b/common/load.cc index 092ba8f..3d3d92c 100644 --- a/common/load.cc +++ b/common/load.cc @@ -26,6 +26,7 @@ #include // getloadavg() #include // floorf() #include +#include #include "cpu.h" #include "load.h" @@ -35,8 +36,8 @@ // Load Averages std::string load_string( bool use_colors, - bool use_powerline_left, bool use_powerline_right, - short num_averages ) + bool use_powerline_left, bool use_powerline_right, short num_averages, + bool segments_to_right, short right_color ) { std::ostringstream ss; ss.setf( std::ios::fixed, std::ios::floatfield ); @@ -97,7 +98,12 @@ std::string load_string( bool use_colors, if( use_colors ) { - if( use_powerline_left ) + if( use_powerline_left && segments_to_right ) + { + powerline( ss, load_lut[load_percent], POWERLINE_LEFT, true ); + powerline_char( ss, load_lut[load_percent], right_color, POWERLINE_LEFT, true ); + } + else if( use_powerline_left && !segments_to_right ) { powerline( ss, load_lut[load_percent], POWERLINE_LEFT, true ); powerline( ss, "#[fg=default,bg=default]", POWERLINE_LEFT ); @@ -106,6 +112,10 @@ std::string load_string( bool use_colors, { ss << "#[fg=default,bg=default]"; } + else if ( segments_to_right && use_powerline_right ) + { + powerline_char( ss, load_lut[load_percent], right_color, POWERLINE_RIGHT, true ); + } } } diff --git a/common/load.h b/common/load.h index 4b57015..5f1f98b 100644 --- a/common/load.h +++ b/common/load.h @@ -24,6 +24,7 @@ std::string load_string( bool use_colors = false, bool use_powerline_left = false, bool use_powerline_right = false, - short num_averages = 3 ); + short num_averages = 3, bool segments_to_right = false, + short right_color = 0 ); #endif diff --git a/common/main.cc b/common/main.cc index 37dfc6f..90fe1bf 100644 --- a/common/main.cc +++ b/common/main.cc @@ -131,6 +131,10 @@ void print_help() << "\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" + << "-l , --segments-left \n" + << "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n" + << "-r , --segments-right \n" + << "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n" << "-i , --interval \n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "-g , --graph-lines \n" @@ -149,10 +153,14 @@ int main( int argc, char** argv ) unsigned cpu_usage_delay = 990000; short averages_count = 3; short graph_lines = 10; // max 32767 should be enough + short left_color = 0; + short right_color = 0; bool use_colors = false; bool use_powerline_left = false; bool use_powerline_right = false; bool use_nerd_graph = false; + bool segments_to_left = false; + bool segments_to_right= false; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; CPU_MODE cpu_mode = CPU_MODE_DEFAULT; @@ -172,12 +180,14 @@ int main( int argc, char** argv ) { "mem-mode", required_argument, NULL, 'm' }, { "cpu-mode", required_argument, NULL, 't' }, { "averages-count", required_argument, NULL, 'a' }, + { "segments-left", required_argument, NULL, 'l' }, + { "segments-right", required_argument, NULL, 'r' }, { 0, 0, 0, 0 } // used to handle unknown long options }; 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:cpqnl:r:g:m:a:t:", long_options, NULL) ) != -1 ) { switch( c ) { @@ -199,6 +209,24 @@ int main( int argc, char** argv ) case 'n': // --nerd-graph use_nerd_graph = true; break; + case 'l': // --segments-left + segments_to_left = true; + if( atoi( optarg ) < 0 || atoi( optarg ) > 255 ) + { + std::cerr << "Valid color vaues are from 0 to 255.\n"; + return EXIT_FAILURE; + } + left_color = atoi( optarg ) ; + break; + case 'r': // --segments-right + segments_to_right= true; + if( atoi( optarg ) < 0 || atoi( optarg ) > 255 ) + { + std::cerr << "Valid color vaues are from 0 to 255.\n"; + return EXIT_FAILURE; + } + right_color = atoi( optarg ) ; + break; case 'i': // --interval, -i if( atoi( optarg ) < 1 ) { @@ -259,9 +287,9 @@ 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 ) + std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right, segments_to_left, left_color ) << 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 ); + << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count, segments_to_right, right_color ); std::cout << std::endl; diff --git a/common/memory.cc b/common/memory.cc index ed84ba7..1af23e4 100644 --- a/common/memory.cc +++ b/common/memory.cc @@ -28,7 +28,9 @@ std::string mem_string( const MemoryStatus & mem_status, MEMORY_MODE mode, bool use_colors, bool use_powerline_left, - bool use_powerline_right ) + bool use_powerline_right, + bool segments_to_left, + short left_color ) { std::ostringstream oss; // Change the percision for floats, for a pretty output @@ -38,12 +40,22 @@ std::string mem_string( const MemoryStatus & mem_status, unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem); if( use_colors ) { - if( use_powerline_right ) + if( use_powerline_right && segments_to_left ) + { + powerline_char( oss, mem_lut[color], left_color, POWERLINE_RIGHT, false); + oss << ' '; + } + else if( use_powerline_right && !segments_to_left ) { oss << "#[bg=default]"; powerline( oss, mem_lut[color], POWERLINE_RIGHT ); oss << ' '; } + else if( use_powerline_left && segments_to_left ) + { + powerline_char( oss, mem_lut[color], left_color, POWERLINE_LEFT, false); + oss << ' '; + } else if( use_powerline_left ) { //powerline( oss, mem_lut[color], POWERLINE_LEFT ); diff --git a/common/memory.h b/common/memory.h index 5e11025..e702784 100644 --- a/common/memory.h +++ b/common/memory.h @@ -51,6 +51,8 @@ std::string mem_string( const MemoryStatus & mem_status, MEMORY_MODE mode = MEMORY_MODE_DEFAULT, bool use_colors = false, bool use_powerline_left = false, - bool use_powerline_right = false ); + bool use_powerline_right = false, + bool segments_to_left = false, + short left_color = 0 ); #endif diff --git a/common/powerline.cc b/common/powerline.cc index d4e5cf8..537b634 100644 --- a/common/powerline.cc +++ b/common/powerline.cc @@ -66,3 +66,49 @@ void powerline( std::ostringstream & oss, const char color[], break; }; } + +void powerline_char( std::ostringstream & oss, const char dynamic_color[], + short static_color, POWERLINE_DIRECTION direction, bool eol ) +{ + char write_color[7]; + sprintf(write_color, "%d", static_color); + switch( direction ) + { + case POWERLINE_LEFT: + if ( eol ) + { + oss << bg2fg( dynamic_color ) + << "#[bg=colour" + << write_color + << "]"; + } + else + { + oss << dynamic_color + << "#[fg=colour" + << write_color + << "]"; + } + oss << PWL_LEFT_FILLED + << dynamic_color; + break; + case POWERLINE_RIGHT: + if ( eol ) + { + oss << dynamic_color + << "#[fg=colour" + << write_color + << "] "; + } + else + { + oss << bg2fg(dynamic_color) + << " #[bg=colour" + << write_color + << "]"; + } + oss << PWL_RIGHT_FILLED + << dynamic_color; + break; + } +} diff --git a/common/powerline.h b/common/powerline.h index 367d86d..e6ad299 100644 --- a/common/powerline.h +++ b/common/powerline.h @@ -34,5 +34,7 @@ enum POWERLINE_DIRECTION * in the next entr. */ void powerline( std::ostringstream & oss, const char color[], POWERLINE_DIRECTION direction, bool background_only = false ); +void powerline_char( std::ostringstream & oss, const char dynamic_color[], + short static_color, POWERLINE_DIRECTION direction, bool eol = false ); #endif // POWERLINE_H