Implement --powerline-left

This commit is contained in:
Matt McCormick 2016-07-31 21:57:56 -04:00
parent a26f0ae59d
commit 6960e27dc4
8 changed files with 127 additions and 44 deletions

@ -19,7 +19,7 @@
#ifndef CONVERSIONS_H_ #ifndef CONVERSIONS_H_
#define CONVERSIONS_H_ #define CONVERSIONS_H_
enum enum BYTE_UNITS
{ {
BYTES = 0, BYTES = 0,
KILOBYTES = 1, KILOBYTES = 1,

@ -34,7 +34,9 @@
#include "powerline.h" #include "powerline.h"
// Load Averages // Load Averages
std::string load_string( bool use_colors, bool use_powerline, short num_averages ) std::string load_string( bool use_colors,
bool use_powerline_left, bool use_powerline_right,
short num_averages )
{ {
std::ostringstream ss; std::ostringstream ss;
double averages[num_averages]; double averages[num_averages];
@ -52,16 +54,27 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages
} }
else else
{ {
unsigned load_percent = static_cast<unsigned int>( averages[0] /
get_cpu_count() * 0.5f * 100.0f );
if( load_percent > 100 )
{
load_percent = 100;
}
if( use_colors ) if( use_colors )
{ {
unsigned load_percent = static_cast<unsigned int>( averages[0] / if( use_powerline_right )
get_cpu_count() * 0.5f * 100.0f );
if( load_percent > 100 )
{ {
load_percent = 100; powerline( ss, load_lut[load_percent], POWERLINE_RIGHT );
}
else if( use_powerline_left )
{
powerline( ss, load_lut[load_percent], POWERLINE_LEFT );
}
else
{
powerline( ss, load_lut[load_percent], NONE );
} }
powerline(ss, load_lut[load_percent], use_powerline);
} }
ss << ' '; ss << ' ';
@ -82,11 +95,12 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages
if( use_colors ) if( use_colors )
{ {
if( use_powerline ) if( use_powerline_left )
{ {
ss << ' '; powerline( ss, load_lut[load_percent], POWERLINE_LEFT, true );
powerline( ss, "#[fg=default,bg=default]", POWERLINE_LEFT );
} }
else else if( !use_powerline_right )
{ {
ss << "#[fg=default,bg=default]"; ss << "#[fg=default,bg=default]";
} }

@ -22,6 +22,8 @@
#include <string> #include <string>
std::string load_string( bool use_colors = false, bool use_powerline = false, short num_averages = 3 ); std::string load_string( bool use_colors = false,
bool use_powerline_left = false, bool use_powerline_right = false,
short num_averages = 3 );
#endif #endif

@ -36,7 +36,8 @@
#include "powerline.h" #include "powerline.h"
std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines, std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false, bool use_powerline = false ) bool use_colors = false,
bool use_powerline_left = false, bool use_powerline_right = false )
{ {
float percentage; float percentage;
@ -62,10 +63,21 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
oss.precision( 0 ); oss.precision( 0 );
} }
unsigned int percent = static_cast<unsigned int>( percentage );
if( use_colors ) if( use_colors )
{ {
unsigned int percent = static_cast<unsigned int>( percentage ); if( use_powerline_right )
powerline(oss, cpu_percentage_lut[percent], use_powerline); {
powerline( oss, cpu_percentage_lut[percent], POWERLINE_RIGHT );
}
else if( use_powerline_left )
{
powerline( oss, cpu_percentage_lut[percent], POWERLINE_LEFT );
}
else
{
powerline( oss, cpu_percentage_lut[percent], NONE );
}
} }
if( graph_lines > 0) if( graph_lines > 0)
@ -79,11 +91,11 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
oss << "%"; oss << "%";
if( use_colors ) if( use_colors )
{ {
if( use_powerline ) if( use_powerline_left )
{ {
oss << ' '; powerline( oss, cpu_percentage_lut[percent], POWERLINE_LEFT, true );
} }
else else if( !use_powerline_right )
{ {
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
} }
@ -125,7 +137,8 @@ int main( int argc, char** argv )
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
bool use_colors = false; bool use_colors = false;
bool use_powerline = false; bool use_powerline_left = false;
bool use_powerline_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;
@ -137,7 +150,8 @@ int main( int argc, char** argv )
// otherwise it's a value to set the variable *flag points to // otherwise it's a value to set the variable *flag points to
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "colors", no_argument, NULL, 'c' }, { "colors", no_argument, NULL, 'c' },
{ "powerline-right", no_argument, NULL, 'p' }, { "powerline-left", no_argument, NULL, 'p' },
{ "powerline-right", no_argument, NULL, 'q' },
{ "interval", required_argument, NULL, 'i' }, { "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' }, { "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' }, { "mem-mode", required_argument, NULL, 'm' },
@ -148,7 +162,7 @@ int main( int argc, char** argv )
int c; int c;
// while c != -1 // while c != -1
while( (c = getopt_long( argc, argv, "hi:cpg:m:a:t:", long_options, NULL) ) != -1 ) while( (c = getopt_long( argc, argv, "hi:cpqg:m:a:t:", long_options, NULL) ) != -1 )
{ {
switch( c ) switch( c )
{ {
@ -159,9 +173,13 @@ int main( int argc, char** argv )
case 'c': // --colors case 'c': // --colors
use_colors = true; use_colors = true;
break; break;
case 'p': // --powerline-right case 'p': // --powerline-left
use_colors = true; use_colors = true;
use_powerline = true; use_powerline_left = true;
break;
case 'q': // --powerline-right
use_colors = true;
use_powerline_right = true;
break; break;
case 'i': // --interval, -i case 'i': // --interval, -i
if( atoi( optarg ) < 1 ) if( atoi( optarg ) < 1 )
@ -223,9 +241,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 ) 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 ) << cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right )
<< load_string( use_colors, use_powerline, averages_count ); << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

@ -26,17 +26,32 @@
std::string mem_string( const MemoryStatus & mem_status, std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode, MEMORY_MODE mode,
bool use_colors, bool use_colors,
bool use_powerline ) bool use_powerline_left,
bool use_powerline_right )
{ {
std::ostringstream oss; std::ostringstream oss;
// Change the percision for floats, for a pretty output // Change the percision for floats, for a pretty output
oss.precision( 2 ); oss.precision( 2 );
oss.setf( std::ios::fixed | std::ios::right ); oss.setf( std::ios::fixed | std::ios::right );
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
if( use_colors ) if( use_colors )
{ {
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem); if( use_powerline_right )
powerline(oss, mem_lut[color], use_powerline); {
powerline( oss, mem_lut[color], POWERLINE_RIGHT );
}
else if( use_powerline_left )
{
//powerline( oss, mem_lut[color], POWERLINE_LEFT );
// We do not know how to invert the default background color
powerline( oss, mem_lut[color], NONE );
oss << ' ';
}
else
{
powerline( oss, mem_lut[color], NONE );
}
} }
switch( mode ) switch( mode )
@ -73,11 +88,11 @@ std::string mem_string( const MemoryStatus & mem_status,
if( use_colors ) if( use_colors )
{ {
if( use_powerline ) if( use_powerline_left )
{ {
oss << " "; powerline( oss, mem_lut[color], POWERLINE_LEFT, true );
} }
else else if( !use_powerline_right )
{ {
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
} }

@ -50,6 +50,7 @@ enum MEMORY_MODE
std::string mem_string( const MemoryStatus & mem_status, 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 = false ); bool use_powerline_left = false,
bool use_powerline_right = false );
#endif #endif

@ -18,12 +18,14 @@
#include "powerline.h" #include "powerline.h"
#include <string>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#define PWL_LEFT_FILLED ""
#define PWL_RIGHT_FILLED "" #define PWL_RIGHT_FILLED ""
const char * bg2fg(const char s[]) const char * bg2fg( const char s[] )
{ {
static char buf[40] = {0}; static char buf[40] = {0};
const char *substr = index(s, ','); const char *substr = index(s, ',');
@ -34,16 +36,35 @@ const char * bg2fg(const char s[])
return buf; return buf;
} }
void powerline(std::ostringstream &oss, const char s[], bool use_powerline)
void powerline( std::ostringstream & oss, const char color[],
POWERLINE_DIRECTION direction, bool background_only )
{ {
if( use_powerline ) switch( direction )
{ {
oss << bg2fg(s) case NONE:
<< PWL_RIGHT_FILLED oss << color;
<< s << ' '; break;
} case POWERLINE_LEFT:
if( background_only )
{
oss << ' ' << bg2fg( color );
}
else else
{ {
oss << s; std::string colorstr( color );
} oss << "#[" << colorstr.substr( colorstr.find( "," ) + 1 )
<< PWL_LEFT_FILLED
<< color
<< ' ';
}
break;
case POWERLINE_RIGHT:
oss << ' '
<< bg2fg( color )
<< PWL_RIGHT_FILLED
<< color
<< ' ';
break;
};
} }

@ -21,6 +21,18 @@
#include <sstream> #include <sstream>
void powerline(std::ostringstream &oss, const char s[], bool); enum POWERLINE_DIRECTION
{
NONE,
POWERLINE_LEFT,
POWERLINE_RIGHT
};
/** Print out a powerline left character inverted version of the given
* color. In the case of of using powerline left, the background color needs
* to be inverted to the foreground before the powerline character is printed
* in the next entr. */
void powerline( std::ostringstream & oss, const char color[],
POWERLINE_DIRECTION direction, bool background_only = false );
#endif // POWERLINE_H #endif // POWERLINE_H