Merge pull request #94 from henrypotgieter/improved_segment_adjacency

Improved segment adjacency
This commit is contained in:
Matt McCormick 2023-05-08 10:29:04 -04:00 committed by GitHub
commit b06c049d0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 141 additions and 12 deletions

@ -144,10 +144,18 @@ The full usage::
Use tmux colors in output Use tmux colors in output
-p, --powerline-left -p, --powerline-left
Use powerline left symbols throughout the output, enables --colors Use powerline left symbols throughout the output, enables --colors
-v, --vertical-graph
Use vertical bar chart for CPU graph
-q, --powerline-right -q, --powerline-right
Use powerline right symbols throughout the output, enables --colors Use powerline right symbols throughout the output, enables --colors
-v, --vertical-graph
Use vertical bar chart for CPU graph
-l <value>, --segments-left <value>
Enable blending bg/fg color (depending on -p or -q use) with segment to left
Provide color to be used depending on -p or -q option for seamless segment blending
Color is an integer value which uses the standard tmux color palette values
-r <value>, --segments-right <value>
Enable blending bg/fg color (depending on -p or -q use) with segment to right
Provide color to be used depending on -p or -q option for seamless segment blending
Color is an integer value which uses the standard tmux color palette values
-i <value>, --interval <value> -i <value>, --interval <value>
Set tmux status refresh interval in seconds. Default: 1 second Set tmux status refresh interval in seconds. Default: 1 second
-g <value>, --graph-lines <value> -g <value>, --graph-lines <value>
@ -159,7 +167,37 @@ The full usage::
-a <value>, --averages-count <value> -a <value>, --averages-count <value>
Set how many load-averages should be drawn. Default: 3 Set how many load-averages should be drawn. Default: 3
Blending Dynamic Colors Tmux Powerline Segments
===============================================
The -l and -r options when used in conjunction with a recent version of Tmux Powerline
that has the ability to selectively disable spacing and separators between segments allow
for seamless blending of tmux-mem-cpu-load output with other adjacent segments. The end
result is dynamic changing of appropriate foreground and background colors as the start
and end of the tmux-mem-cpu-load output string that is aggregated with other Tmux
Powerline output to produce a more polished status line in Tmux.
Segment Adjaceny before this feature:
.. image:: seg-adj1.png
Segment Adjaceny after this feature:
.. image:: seg-adj2.png
Note that the values for the -l and -r options will be the standard Tmux integer color
values. They set the appropriate background and foreground colors used for the separator
character when used with the poweline-left or powerline-right options so it is easy to
match coloring to adjacent segments. An example from the segment script that calls
tmux-mem-cpu-load is as follows::
tmux-mem-cpu-load -q -v -l 52 -r 33
This combines with theme options available to tmux-powerline, such as the following::
"disk_usage_cust 52 123 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 52 123 right_disable" \
"tmux_mem_cpu_load_cust 52 234 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 52 234 both_disable separator_disable" \
"batt_cust 33 154 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 16 33 N separator_disable" \
Authors Authors
======= =======

@ -26,6 +26,7 @@
#include <stdlib.h> // getloadavg() #include <stdlib.h> // getloadavg()
#include <cmath> // floorf() #include <cmath> // floorf()
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h>
#include "cpu.h" #include "cpu.h"
#include "load.h" #include "load.h"
@ -35,8 +36,8 @@
// Load Averages // Load Averages
std::string load_string( bool use_colors, std::string load_string( bool use_colors,
bool use_powerline_left, bool use_powerline_right, bool use_powerline_left, bool use_powerline_right, short num_averages,
short num_averages ) bool segments_to_right, short right_color )
{ {
std::ostringstream ss; std::ostringstream ss;
ss.setf( std::ios::fixed, std::ios::floatfield ); ss.setf( std::ios::fixed, std::ios::floatfield );
@ -97,7 +98,12 @@ std::string load_string( bool use_colors,
if( 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, load_lut[load_percent], POWERLINE_LEFT, true );
powerline( ss, "#[fg=default,bg=default]", POWERLINE_LEFT ); powerline( ss, "#[fg=default,bg=default]", POWERLINE_LEFT );
@ -106,6 +112,10 @@ std::string load_string( bool use_colors,
{ {
ss << "#[fg=default,bg=default]"; 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, std::string load_string( bool use_colors = false,
bool use_powerline_left = false, bool use_powerline_right = 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 #endif

@ -131,6 +131,14 @@ void print_help()
<< "\tUse powerline right symbols throughout the output, enables --colors\n" << "\tUse powerline right symbols throughout the output, enables --colors\n"
<< "-v, --vertical-graph\n" << "-v, --vertical-graph\n"
<< "\tUse vertical bar chart for CPU graph\n" << "\tUse vertical bar chart for CPU graph\n"
<< "-l <value>, --segments-left <value>\n"
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n"
<< "\tProvide color to be used depending on -p or -q option for seamless segment blending\n"
<< "\tColor is an integer value which uses the standard tmux color palette values\n"
<< "-r <value>, --segments-right <value>\n"
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to right\n"
<< "\tProvide color to be used depending on -p or -q option for seamless segment blending\n"
<< "\tColor is an integer value which uses the standard tmux color palette values\n"
<< "-i <value>, --interval <value>\n" << "-i <value>, --interval <value>\n"
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\n" << "-g <value>, --graph-lines <value>\n"
@ -149,10 +157,14 @@ int main( int argc, char** argv )
unsigned cpu_usage_delay = 990000; unsigned cpu_usage_delay = 990000;
short averages_count = 3; short averages_count = 3;
short graph_lines = 10; // max 32767 should be enough short graph_lines = 10; // max 32767 should be enough
short left_color = 0;
short right_color = 0;
bool use_colors = false; bool use_colors = false;
bool use_powerline_left = false; bool use_powerline_left = false;
bool use_powerline_right = false; bool use_powerline_right = false;
bool use_vert_graph = false; bool use_vert_graph = false;
bool segments_to_left = false;
bool segments_to_right= false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
CPU_MODE cpu_mode = CPU_MODE_DEFAULT; CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
@ -172,12 +184,14 @@ int main( int argc, char** argv )
{ "mem-mode", required_argument, NULL, 'm' }, { "mem-mode", required_argument, NULL, 'm' },
{ "cpu-mode", required_argument, NULL, 't' }, { "cpu-mode", required_argument, NULL, 't' },
{ "averages-count", required_argument, NULL, 'a' }, { "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 { 0, 0, 0, 0 } // used to handle unknown long options
}; };
int c; int c;
// while c != -1 // while c != -1
while( (c = getopt_long( argc, argv, "hi:cpqvg:m:a:t:", long_options, NULL) ) != -1 ) while( (c = getopt_long( argc, argv, "hi:cpqvl:r:g:m:a:t:", long_options, NULL) ) != -1 )
{ {
switch( c ) switch( c )
{ {
@ -199,6 +213,24 @@ int main( int argc, char** argv )
case 'v': // --vertical-graph case 'v': // --vertical-graph
use_vert_graph = true; use_vert_graph = true;
break; 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 case 'i': // --interval, -i
if( atoi( optarg ) < 1 ) if( atoi( optarg ) < 1 )
{ {
@ -259,9 +291,9 @@ int main( int argc, char** argv )
MemoryStatus memory_status; MemoryStatus memory_status;
mem_status( 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_vert_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 ); << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count, segments_to_right, right_color );
std::cout << std::endl; std::cout << std::endl;

@ -28,7 +28,9 @@ std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode, MEMORY_MODE mode,
bool use_colors, bool use_colors,
bool use_powerline_left, bool use_powerline_left,
bool use_powerline_right ) bool use_powerline_right,
bool segments_to_left,
short left_color )
{ {
std::ostringstream oss; std::ostringstream oss;
// Change the percision for floats, for a pretty output // 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); unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
if( use_colors ) 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]"; oss << "#[bg=default]";
powerline( oss, mem_lut[color], POWERLINE_RIGHT ); powerline( oss, mem_lut[color], POWERLINE_RIGHT );
oss << ' '; 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 ) else if( use_powerline_left )
{ {
//powerline( oss, mem_lut[color], 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, MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
bool use_colors = false, bool use_colors = false,
bool use_powerline_left = 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 #endif

@ -66,3 +66,35 @@ void powerline( std::ostringstream & oss, const char color[],
break; 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. */ * in the next entr. */
void powerline( std::ostringstream & oss, const char color[], void powerline( std::ostringstream & oss, const char color[],
POWERLINE_DIRECTION direction, bool background_only = false ); 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 #endif // POWERLINE_H

BIN
seg-adj1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
seg-adj2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB