Add options to allow embedding tmux-mem-cpu-load segments within/beside other segments

This commit is contained in:
henry.potgieter 2023-03-27 11:11:55 -04:00
parent dc298e162a
commit f9663c8f2f
7 changed files with 111 additions and 10 deletions

@ -26,6 +26,7 @@
#include <stdlib.h> // getloadavg()
#include <cmath> // floorf()
#include <sys/types.h>
#include <stdio.h>
#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 );
}
}
}

@ -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

@ -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 <value>, --segments-left <value>\n"
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n"
<< "-r <value>, --segments-right <value>\n"
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n"
<< "-i <value>, --interval <value>\n"
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\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;

@ -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 );

@ -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

@ -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;
}
}

@ -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