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 1/4] 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 From 159051079b919f03c29d1e8cd5b6115fda8abf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Goli=C5=84ski?= Date: Sat, 5 Mar 2016 19:34:37 +0100 Subject: [PATCH 2/4] Changes in README.rst --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index f0e7709..3a98ae9 100644 --- a/README.rst +++ b/README.rst @@ -118,6 +118,8 @@ The full usage:: Prints this help message --colors Use tmux colors in output + --powerline + Use powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors -i , --interval Set tmux status refresh interval in seconds. Default: 1 second -g , --graph-lines From e9a3726de73a7d686d84bc3113ec1bb08c22fca3 Mon Sep 17 00:00:00 2001 From: golinski Date: Mon, 7 Mar 2016 15:14:32 +0100 Subject: [PATCH 3/4] Change CMakeLists.txt, so it builds on all systems, not only on Linux --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7a44bc..512516c 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" "common/powerline.cc") + set(METER_SOURCES "linux/memory.cc" "linux/cpu.cc" "common/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") message(STATUS "Darwin detected") set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "common/load.cc") @@ -80,7 +80,7 @@ else() endif() # set common source files -set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc") +set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc""common/powerline.cc") # add binary tree so we find version.h include_directories("${PROJECT_BINARY_DIR}") From bac831cd69b8a5c92cbce74bdcaab567e7647bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Goli=C5=84ski?= Date: Thu, 14 Apr 2016 12:18:10 +0200 Subject: [PATCH 4/4] Code refactoring for merging - license header in new files - changed "powerline" into "powerline-right" - added braces around one-liners - small fixes concerning whitespace --- CMakeLists.txt | 2 +- README.rst | 2 +- common/load.cc | 6 +++++- common/main.cc | 10 +++++++--- common/powerline.cc | 30 ++++++++++++++++++++++++++---- common/powerline.h | 18 ++++++++++++++++++ 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 512516c..0fa3854 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,7 @@ else() endif() # set common source files -set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc""common/powerline.cc") +set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc" "common/powerline.cc") # add binary tree so we find version.h include_directories("${PROJECT_BINARY_DIR}") diff --git a/README.rst b/README.rst index 3a98ae9..188ee6f 100644 --- a/README.rst +++ b/README.rst @@ -118,7 +118,7 @@ The full usage:: Prints this help message --colors Use tmux colors in output - --powerline + --powerline-right Use powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors -i , --interval Set tmux status refresh interval in seconds. Default: 1 second diff --git a/common/load.cc b/common/load.cc index 1ed1ebd..926ada8 100644 --- a/common/load.cc +++ b/common/load.cc @@ -77,10 +77,14 @@ std::string load_string( bool use_colors, bool use_powerline ) if( use_colors ) { - if(use_powerline) + if( use_powerline ) + { ss << ' '; + } else + { ss << "#[fg=default,bg=default]"; + } } } diff --git a/common/main.cc b/common/main.cc index df79865..7d3f265 100644 --- a/common/main.cc +++ b/common/main.cc @@ -67,9 +67,13 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, if( use_colors ) { if( use_powerline ) + { oss << ' '; + } else + { oss << "#[fg=default,bg=default]"; + } } return oss.str(); @@ -88,7 +92,7 @@ void print_help() << "-c, --colors\n" << "--colors\n" << "\tUse tmux colors in output\n" - << "-p, --powerline\n" + << "-p, --powerline-right\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" @@ -115,7 +119,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' }, + { "powerline-right", no_argument, NULL, 'p' }, { "interval", required_argument, NULL, 'i' }, { "graph-lines", required_argument, NULL, 'g' }, { "mem-mode", required_argument, NULL, 'm' }, @@ -135,7 +139,7 @@ int main( int argc, char** argv ) case 'c': // --colors use_colors = true; break; - case 'p': // --powerline + case 'p': // --powerline-right use_colors = true; use_powerline = true; break; diff --git a/common/powerline.cc b/common/powerline.cc index 6825eb2..85bac7b 100644 --- a/common/powerline.cc +++ b/common/powerline.cc @@ -1,3 +1,21 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2016 Michał Goliński + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "powerline.h" #include @@ -18,10 +36,14 @@ const char * bg2fg(const char s[]) void powerline(std::ostringstream &oss, const char s[], bool use_powerline) { - if (use_powerline) - oss << bg2fg(s) - << PWL_RIGHT_FILLED - << s << ' '; + if( use_powerline ) + { + oss << bg2fg(s) + << PWL_RIGHT_FILLED + << s << ' '; + } else + { oss << s; + } } diff --git a/common/powerline.h b/common/powerline.h index 60b45e8..caf6d93 100644 --- a/common/powerline.h +++ b/common/powerline.h @@ -1,3 +1,21 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2016 Michał Goliński + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef POWERLINE_H #define POWERLINE_H