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_
#define CONVERSIONS_H_
enum
enum BYTE_UNITS
{
BYTES = 0,
KILOBYTES = 1,

@ -34,7 +34,9 @@
#include "powerline.h"
// 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;
double averages[num_averages];
@ -51,8 +53,6 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages
ss << " 0.00 0.00 0.00"; // couldn't get averages.
}
else
{
if( use_colors )
{
unsigned load_percent = static_cast<unsigned int>( averages[0] /
get_cpu_count() * 0.5f * 100.0f );
@ -61,7 +61,20 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages
{
load_percent = 100;
}
powerline(ss, load_lut[load_percent], use_powerline);
if( use_colors )
{
if( use_powerline_right )
{
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 );
}
}
ss << ' ';
@ -82,11 +95,12 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages
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]";
}

@ -22,6 +22,8 @@
#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

@ -36,7 +36,8 @@
#include "powerline.h"
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;
@ -62,10 +63,21 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
oss.precision( 0 );
}
unsigned int percent = static_cast<unsigned int>( percentage );
if( use_colors )
{
unsigned int percent = static_cast<unsigned int>( percentage );
powerline(oss, cpu_percentage_lut[percent], use_powerline);
if( use_powerline_right )
{
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)
@ -79,11 +91,11 @@ std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigne
oss << "%";
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]";
}
@ -125,7 +137,8 @@ int main( int argc, char** argv )
short averages_count = 3;
short graph_lines = 10; // max 32767 should be enough
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;
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
{ "help", no_argument, NULL, 'h' },
{ "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' },
{ "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
@ -148,7 +162,7 @@ int main( int argc, char** argv )
int c;
// 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 )
{
@ -159,9 +173,13 @@ int main( int argc, char** argv )
case 'c': // --colors
use_colors = true;
break;
case 'p': // --powerline-right
case 'p': // --powerline-left
use_colors = true;
use_powerline = true;
use_powerline_left = true;
break;
case 'q': // --powerline-right
use_colors = true;
use_powerline_right = true;
break;
case 'i': // --interval, -i
if( atoi( optarg ) < 1 )
@ -223,9 +241,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 )
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline )
<< load_string( use_colors, use_powerline, averages_count );
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_left, use_powerline_right )
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
return EXIT_SUCCESS;
}

@ -26,17 +26,32 @@
std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode,
bool use_colors,
bool use_powerline )
bool use_powerline_left,
bool use_powerline_right )
{
std::ostringstream oss;
// Change the percision for floats, for a pretty output
oss.precision( 2 );
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 )
{
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
powerline(oss, mem_lut[color], use_powerline);
if( use_powerline_right )
{
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 )
@ -73,11 +88,11 @@ std::string mem_string( const MemoryStatus & mem_status,
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]";
}

@ -50,6 +50,7 @@ enum MEMORY_MODE
std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
bool use_colors = false,
bool use_powerline = false );
bool use_powerline_left = false,
bool use_powerline_right = false );
#endif

@ -18,9 +18,11 @@
#include "powerline.h"
#include <string>
#include <cstring>
#include <sstream>
#define PWL_LEFT_FILLED ""
#define PWL_RIGHT_FILLED ""
const char * bg2fg( const char s[] )
@ -34,16 +36,35 @@ const char * bg2fg(const char s[])
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)
<< PWL_RIGHT_FILLED
<< s << ' ';
case NONE:
oss << color;
break;
case POWERLINE_LEFT:
if( background_only )
{
oss << ' ' << bg2fg( color );
}
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>
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