Powerline support

This commit is contained in:
Michał Goliński 2016-03-05 11:19:31 +01:00
parent 68fd497108
commit cc59b4952f
8 changed files with 86 additions and 16 deletions

@ -59,7 +59,7 @@ endif(NOT CMAKE_BUILD_TYPE)
# detect system type
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
message(STATUS "Linux detected")
set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "common/load.cc")
set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "common/load.cc" "common/powerline.cc")
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
message(STATUS "Darwin detected")
set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "common/load.cc")
@ -111,6 +111,10 @@ if(BUILD_TESTING)
COMMAND tmux-mem-cpu-load --colors
)
add_test(NAME powerline
COMMAND tmux-mem-cpu-load --powerline
)
add_test(NAME invalid_status_interval
COMMAND tmux-mem-cpu-load -i -1
)

@ -30,10 +30,12 @@
#include "load.h"
#include "luts.h"
#include "powerline.h"
// Load Averages
std::string load_string( bool use_colors = false )
std::string load_string( bool use_colors, bool use_powerline )
{
std::stringstream ss;
std::ostringstream ss;
// Get only 3 load averages
const int nelem = 3;
double averages[nelem];
@ -54,7 +56,7 @@ std::string load_string( bool use_colors = false )
{
load_percent = 100;
}
ss << load_lut[load_percent];
powerline(ss, load_lut[load_percent], use_powerline);
}
ss << ' ';
@ -75,7 +77,10 @@ std::string load_string( bool use_colors = false )
if( use_colors )
{
ss << "#[fg=default,bg=default]";
if(use_powerline)
ss << ' ';
else
ss << "#[fg=default,bg=default]";
}
}

@ -21,6 +21,6 @@
#include <string>
std::string load_string( bool );
std::string load_string( bool use_colors = false, bool use_powerline = false );
#endif

@ -33,8 +33,10 @@
#include "memory.h"
#include "load.h"
#include "powerline.h"
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false )
bool use_colors = false, bool use_powerline = false )
{
float percentage;
@ -49,7 +51,8 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
if( use_colors )
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
unsigned int percent = static_cast<unsigned int>( percentage );
powerline(oss, cpu_percentage_lut[percent], use_powerline);
}
if( graph_lines > 0)
@ -63,7 +66,10 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
oss << "%";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
if( use_powerline )
oss << ' ';
else
oss << "#[fg=default,bg=default]";
}
return oss.str();
@ -79,8 +85,11 @@ void print_help()
<< "Available options:\n"
<< "-h, --help\n"
<< "\t Prints this help message\n"
<< "-c, --colors\n"
<< "--colors\n"
<< "\tUse tmux colors in output\n"
<< "-p, --powerline\n"
<< "\tUse powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors\n"
<< "-i <value>, --interval <value>\n"
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\n"
@ -95,6 +104,7 @@ int main( int argc, char** argv )
unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough
bool use_colors = false;
bool use_powerline = false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
static struct option long_options[] =
@ -105,6 +115,7 @@ 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", no_argument, NULL, 'p' },
{ "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
@ -124,6 +135,10 @@ int main( int argc, char** argv )
case 'c': // --colors
use_colors = true;
break;
case 'p': // --powerline
use_colors = true;
use_powerline = true;
break;
case 'i': // --interval, -i
if( atoi( optarg ) < 1 )
{
@ -168,9 +183,9 @@ int main( int argc, char** argv )
MemoryStatus memory_status;
mem_status( memory_status );
std::cout << mem_string( memory_status, mem_mode, use_colors )
<< cpu_string( cpu_usage_delay, graph_lines, use_colors )
<< load_string( use_colors );
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline )
<< cpu_string( cpu_usage_delay, graph_lines, use_colors, use_powerline )
<< load_string( use_colors, use_powerline );
return EXIT_SUCCESS;
}

@ -21,10 +21,12 @@
#include "memory.h"
#include "luts.h"
#include "conversions.h"
#include "powerline.h"
std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode,
bool use_colors )
bool use_colors,
bool use_powerline )
{
std::ostringstream oss;
// Change the percision for floats, for a pretty output
@ -33,7 +35,8 @@ std::string mem_string( const MemoryStatus & mem_status,
if( use_colors )
{
oss << mem_lut[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);
powerline(oss, mem_lut[color], use_powerline);
}
switch( mode )
@ -70,7 +73,14 @@ std::string mem_string( const MemoryStatus & mem_status,
if( use_colors )
{
oss << "#[fg=default,bg=default]";
if( use_powerline )
{
oss << " ";
}
else
{
oss << "#[fg=default,bg=default]";
}
}
return oss.str();

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

27
common/powerline.cc Normal file

@ -0,0 +1,27 @@
#include "powerline.h"
#include <cstring>
#include <sstream>
#define PWL_RIGHT_FILLED ""
const char * bg2fg(const char s[])
{
static char buf[40] = {0};
const char *substr = index(s, ',');
buf[0] = '#';
buf[1] = '[';
buf[2] = 'f';
strcpy(buf+3, substr+2);
return buf;
}
void powerline(std::ostringstream &oss, const char s[], bool use_powerline)
{
if (use_powerline)
oss << bg2fg(s)
<< PWL_RIGHT_FILLED
<< s << ' ';
else
oss << s;
}

8
common/powerline.h Normal file

@ -0,0 +1,8 @@
#ifndef POWERLINE_H
#define POWERLINE_H
#include <sstream>
void powerline(std::ostringstream &oss, const char s[], bool);
#endif // POWERLINE_H