From cc59b4952f90d6181217f3d928fab8456a3dae0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Goli=C5=84ski?= Date: Sat, 5 Mar 2016 11:19:31 +0100 Subject: [PATCH] Powerline support --- CMakeLists.txt | 6 +++++- common/load.cc | 13 +++++++++---- common/load.h | 2 +- common/main.cc | 27 +++++++++++++++++++++------ common/memory.cc | 16 +++++++++++++--- common/memory.h | 3 ++- common/powerline.cc | 27 +++++++++++++++++++++++++++ common/powerline.h | 8 ++++++++ 8 files changed, 86 insertions(+), 16 deletions(-) create mode 100644 common/powerline.cc create mode 100644 common/powerline.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9109314..a7a44bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/common/load.cc b/common/load.cc index 67c6d08..1ed1ebd 100644 --- a/common/load.cc +++ b/common/load.cc @@ -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]"; } } diff --git a/common/load.h b/common/load.h index f1bbc58..b8d50f9 100644 --- a/common/load.h +++ b/common/load.h @@ -21,6 +21,6 @@ #include -std::string load_string( bool ); +std::string load_string( bool use_colors = false, bool use_powerline = false ); #endif diff --git a/common/main.cc b/common/main.cc index 80ae922..df79865 100644 --- a/common/main.cc +++ b/common/main.cc @@ -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( percentage )]; + unsigned int percent = static_cast( 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 , --interval \n" << "\tSet tmux status refresh interval in seconds. Default: 1 second\n" << "-g , --graph-lines \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; } diff --git a/common/memory.cc b/common/memory.cc index 23a4ea5..c517ef0 100644 --- a/common/memory.cc +++ b/common/memory.cc @@ -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(); diff --git a/common/memory.h b/common/memory.h index 8fac41a..a815312 100644 --- a/common/memory.h +++ b/common/memory.h @@ -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 diff --git a/common/powerline.cc b/common/powerline.cc new file mode 100644 index 0000000..6825eb2 --- /dev/null +++ b/common/powerline.cc @@ -0,0 +1,27 @@ +#include "powerline.h" + +#include +#include + +#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; +} diff --git a/common/powerline.h b/common/powerline.h new file mode 100644 index 0000000..60b45e8 --- /dev/null +++ b/common/powerline.h @@ -0,0 +1,8 @@ +#ifndef POWERLINE_H +#define POWERLINE_H + +#include + +void powerline(std::ostringstream &oss, const char s[], bool); + +#endif // POWERLINE_H