Skip to content

Commit b06c049

Browse files
authored
Merge pull request #94 from henrypotgieter/improved_segment_adjacency
Improved segment adjacency
2 parents a991497 + aa8fe65 commit b06c049

File tree

10 files changed

+141
-12
lines changed

10 files changed

+141
-12
lines changed

README.rst

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,18 @@ The full usage::
144144
Use tmux colors in output
145145
-p, --powerline-left
146146
Use powerline left symbols throughout the output, enables --colors
147-
-v, --vertical-graph
148-
Use vertical bar chart for CPU graph
149147
-q, --powerline-right
150148
Use powerline right symbols throughout the output, enables --colors
149+
-v, --vertical-graph
150+
Use vertical bar chart for CPU graph
151+
-l <value>, --segments-left <value>
152+
Enable blending bg/fg color (depending on -p or -q use) with segment to left
153+
Provide color to be used depending on -p or -q option for seamless segment blending
154+
Color is an integer value which uses the standard tmux color palette values
155+
-r <value>, --segments-right <value>
156+
Enable blending bg/fg color (depending on -p or -q use) with segment to right
157+
Provide color to be used depending on -p or -q option for seamless segment blending
158+
Color is an integer value which uses the standard tmux color palette values
151159
-i <value>, --interval <value>
152160
Set tmux status refresh interval in seconds. Default: 1 second
153161
-g <value>, --graph-lines <value>
@@ -159,7 +167,37 @@ The full usage::
159167
-a <value>, --averages-count <value>
160168
Set how many load-averages should be drawn. Default: 3
161169

170+
Blending Dynamic Colors Tmux Powerline Segments
171+
===============================================
172+
173+
The -l and -r options when used in conjunction with a recent version of Tmux Powerline
174+
that has the ability to selectively disable spacing and separators between segments allow
175+
for seamless blending of tmux-mem-cpu-load output with other adjacent segments. The end
176+
result is dynamic changing of appropriate foreground and background colors as the start
177+
and end of the tmux-mem-cpu-load output string that is aggregated with other Tmux
178+
Powerline output to produce a more polished status line in Tmux.
179+
180+
Segment Adjaceny before this feature:
181+
182+
.. image:: seg-adj1.png
183+
184+
Segment Adjaceny after this feature:
185+
186+
.. image:: seg-adj2.png
187+
188+
Note that the values for the -l and -r options will be the standard Tmux integer color
189+
values. They set the appropriate background and foreground colors used for the separator
190+
character when used with the poweline-left or powerline-right options so it is easy to
191+
match coloring to adjacent segments. An example from the segment script that calls
192+
tmux-mem-cpu-load is as follows::
193+
194+
tmux-mem-cpu-load -q -v -l 52 -r 33
195+
196+
This combines with theme options available to tmux-powerline, such as the following::
162197

198+
"disk_usage_cust 52 123 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 52 123 right_disable" \
199+
"tmux_mem_cpu_load_cust 52 234 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 52 234 both_disable separator_disable" \
200+
"batt_cust 33 154 ${TMUX_POWERLINE_SEPARATOR_LEFT_BOLD} 16 33 N separator_disable" \
163201

164202
Authors
165203
=======

common/load.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <stdlib.h> // getloadavg()
2727
#include <cmath> // floorf()
2828
#include <sys/types.h>
29+
#include <stdio.h>
2930

3031
#include "cpu.h"
3132
#include "load.h"
@@ -35,8 +36,8 @@
3536

3637
// Load Averages
3738
std::string load_string( bool use_colors,
38-
bool use_powerline_left, bool use_powerline_right,
39-
short num_averages )
39+
bool use_powerline_left, bool use_powerline_right, short num_averages,
40+
bool segments_to_right, short right_color )
4041
{
4142
std::ostringstream ss;
4243
ss.setf( std::ios::fixed, std::ios::floatfield );
@@ -97,7 +98,12 @@ std::string load_string( bool use_colors,
9798

9899
if( use_colors )
99100
{
100-
if( use_powerline_left )
101+
if( use_powerline_left && segments_to_right )
102+
{
103+
powerline( ss, load_lut[load_percent], POWERLINE_LEFT, true );
104+
powerline_char( ss, load_lut[load_percent], right_color, POWERLINE_LEFT, true );
105+
}
106+
else if( use_powerline_left && !segments_to_right )
101107
{
102108
powerline( ss, load_lut[load_percent], POWERLINE_LEFT, true );
103109
powerline( ss, "#[fg=default,bg=default]", POWERLINE_LEFT );
@@ -106,6 +112,10 @@ std::string load_string( bool use_colors,
106112
{
107113
ss << "#[fg=default,bg=default]";
108114
}
115+
else if ( segments_to_right && use_powerline_right )
116+
{
117+
powerline_char( ss, load_lut[load_percent], right_color, POWERLINE_RIGHT, true );
118+
}
109119
}
110120
}
111121

common/load.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
std::string load_string( bool use_colors = false,
2626
bool use_powerline_left = false, bool use_powerline_right = false,
27-
short num_averages = 3 );
27+
short num_averages = 3, bool segments_to_right = false,
28+
short right_color = 0 );
2829

2930
#endif

common/main.cc

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ void print_help()
131131
<< "\tUse powerline right symbols throughout the output, enables --colors\n"
132132
<< "-v, --vertical-graph\n"
133133
<< "\tUse vertical bar chart for CPU graph\n"
134+
<< "-l <value>, --segments-left <value>\n"
135+
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to left\n"
136+
<< "\tProvide color to be used depending on -p or -q option for seamless segment blending\n"
137+
<< "\tColor is an integer value which uses the standard tmux color palette values\n"
138+
<< "-r <value>, --segments-right <value>\n"
139+
<< "\tEnable blending bg/fg color (depending on -p or -q use) with segment to right\n"
140+
<< "\tProvide color to be used depending on -p or -q option for seamless segment blending\n"
141+
<< "\tColor is an integer value which uses the standard tmux color palette values\n"
134142
<< "-i <value>, --interval <value>\n"
135143
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
136144
<< "-g <value>, --graph-lines <value>\n"
@@ -149,10 +157,14 @@ int main( int argc, char** argv )
149157
unsigned cpu_usage_delay = 990000;
150158
short averages_count = 3;
151159
short graph_lines = 10; // max 32767 should be enough
160+
short left_color = 0;
161+
short right_color = 0;
152162
bool use_colors = false;
153163
bool use_powerline_left = false;
154164
bool use_powerline_right = false;
155165
bool use_vert_graph = false;
166+
bool segments_to_left = false;
167+
bool segments_to_right= false;
156168
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
157169
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
158170

@@ -172,12 +184,14 @@ int main( int argc, char** argv )
172184
{ "mem-mode", required_argument, NULL, 'm' },
173185
{ "cpu-mode", required_argument, NULL, 't' },
174186
{ "averages-count", required_argument, NULL, 'a' },
187+
{ "segments-left", required_argument, NULL, 'l' },
188+
{ "segments-right", required_argument, NULL, 'r' },
175189
{ 0, 0, 0, 0 } // used to handle unknown long options
176190
};
177191

178192
int c;
179193
// while c != -1
180-
while( (c = getopt_long( argc, argv, "hi:cpqvg:m:a:t:", long_options, NULL) ) != -1 )
194+
while( (c = getopt_long( argc, argv, "hi:cpqvl:r:g:m:a:t:", long_options, NULL) ) != -1 )
181195
{
182196
switch( c )
183197
{
@@ -199,6 +213,24 @@ int main( int argc, char** argv )
199213
case 'v': // --vertical-graph
200214
use_vert_graph = true;
201215
break;
216+
case 'l': // --segments-left
217+
segments_to_left = true;
218+
if( atoi( optarg ) < 0 || atoi( optarg ) > 255 )
219+
{
220+
std::cerr << "Valid color vaues are from 0 to 255.\n";
221+
return EXIT_FAILURE;
222+
}
223+
left_color = atoi( optarg ) ;
224+
break;
225+
case 'r': // --segments-right
226+
segments_to_right= true;
227+
if( atoi( optarg ) < 0 || atoi( optarg ) > 255 )
228+
{
229+
std::cerr << "Valid color vaues are from 0 to 255.\n";
230+
return EXIT_FAILURE;
231+
}
232+
right_color = atoi( optarg ) ;
233+
break;
202234
case 'i': // --interval, -i
203235
if( atoi( optarg ) < 1 )
204236
{
@@ -259,9 +291,9 @@ int main( int argc, char** argv )
259291

260292
MemoryStatus memory_status;
261293
mem_status( memory_status );
262-
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right )
294+
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right, segments_to_left, left_color )
263295
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right, use_vert_graph )
264-
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
296+
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count, segments_to_right, right_color );
265297

266298
std::cout << std::endl;
267299

common/memory.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ std::string mem_string( const MemoryStatus & mem_status,
2828
MEMORY_MODE mode,
2929
bool use_colors,
3030
bool use_powerline_left,
31-
bool use_powerline_right )
31+
bool use_powerline_right,
32+
bool segments_to_left,
33+
short left_color )
3234
{
3335
std::ostringstream oss;
3436
// Change the percision for floats, for a pretty output
@@ -38,12 +40,22 @@ std::string mem_string( const MemoryStatus & mem_status,
3840
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
3941
if( use_colors )
4042
{
41-
if( use_powerline_right )
43+
if( use_powerline_right && segments_to_left )
44+
{
45+
powerline_char( oss, mem_lut[color], left_color, POWERLINE_RIGHT, false);
46+
oss << ' ';
47+
}
48+
else if( use_powerline_right && !segments_to_left )
4249
{
4350
oss << "#[bg=default]";
4451
powerline( oss, mem_lut[color], POWERLINE_RIGHT );
4552
oss << ' ';
4653
}
54+
else if( use_powerline_left && segments_to_left )
55+
{
56+
powerline_char( oss, mem_lut[color], left_color, POWERLINE_LEFT, false);
57+
oss << ' ';
58+
}
4759
else if( use_powerline_left )
4860
{
4961
//powerline( oss, mem_lut[color], POWERLINE_LEFT );

common/memory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ std::string mem_string( const MemoryStatus & mem_status,
5151
MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
5252
bool use_colors = false,
5353
bool use_powerline_left = false,
54-
bool use_powerline_right = false );
54+
bool use_powerline_right = false,
55+
bool segments_to_left = false,
56+
short left_color = 0 );
5557

5658
#endif

common/powerline.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,35 @@ void powerline( std::ostringstream & oss, const char color[],
6666
break;
6767
};
6868
}
69+
70+
void powerline_char( std::ostringstream & oss, const char dynamic_color[],
71+
short static_color, POWERLINE_DIRECTION direction, bool eol )
72+
{
73+
char write_color[7];
74+
sprintf(write_color, "%d", static_color);
75+
switch( direction )
76+
{
77+
case POWERLINE_LEFT:
78+
if ( eol )
79+
{
80+
oss << bg2fg( dynamic_color ) << "#[bg=colour" << write_color << "]";
81+
}
82+
else
83+
{
84+
oss << dynamic_color << "#[fg=colour" << write_color << "]";
85+
}
86+
oss << PWL_LEFT_FILLED << dynamic_color;
87+
break;
88+
case POWERLINE_RIGHT:
89+
if ( eol )
90+
{
91+
oss << dynamic_color << "#[fg=colour" << write_color << "] ";
92+
}
93+
else
94+
{
95+
oss << bg2fg(dynamic_color) << " #[bg=colour" << write_color << "]";
96+
}
97+
oss << PWL_RIGHT_FILLED << dynamic_color;
98+
break;
99+
}
100+
}

common/powerline.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ enum POWERLINE_DIRECTION
3434
* in the next entr. */
3535
void powerline( std::ostringstream & oss, const char color[],
3636
POWERLINE_DIRECTION direction, bool background_only = false );
37+
void powerline_char( std::ostringstream & oss, const char dynamic_color[],
38+
short static_color, POWERLINE_DIRECTION direction, bool eol = false );
3739

3840
#endif // POWERLINE_H

seg-adj1.png

4.85 KB
Loading

seg-adj2.png

4.42 KB
Loading

0 commit comments

Comments
 (0)