From 1d1a516eda3177d4c18e6837499f07a845009e76 Mon Sep 17 00:00:00 2001 From: Compilenix Date: Thu, 28 Apr 2016 22:05:43 +0200 Subject: [PATCH 1/4] added averages-count option --- common/load.cc | 96 +++++++++++++++++++++++++++----------------------- common/load.h | 3 +- common/main.cc | 16 +++++++-- 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/common/load.cc b/common/load.cc index 926ada8..cbc1cf0 100644 --- a/common/load.cc +++ b/common/load.cc @@ -3,6 +3,7 @@ * Copyright 2012 Matthew McCormick * Copyright 2013 Justin Crawford * Copyright 2015 Pawel 'l0ner' Soltys + * Copyright 2016 Compilenix * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,59 +34,64 @@ #include "powerline.h" // Load Averages -std::string load_string( bool use_colors, bool use_powerline ) +std::string load_string( bool use_colors, bool use_powerline, short num_averages ) { std::ostringstream ss; - // Get only 3 load averages - const int nelem = 3; - double averages[nelem]; + double averages[num_averages]; // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c - if( getloadavg( averages, nelem ) < 0 ) + if( num_averages > 0) { - ss << " 0.00 0.00 0.00"; // couldn't get averages. + if( getloadavg( averages, num_averages ) < 0 ) + { + ss << " 0.00 0.00 0.00"; // couldn't get averages. + } + else + { + if( use_colors ) + { + unsigned load_percent = static_cast( averages[0] / + get_cpu_count() * 0.5f * 100.0f ); + + if( load_percent > 100 ) + { + load_percent = 100; + } + powerline(ss, load_lut[load_percent], use_powerline); + } + + ss << ' '; + for( int i = 0; i < num_averages; ++i ) + { + // Round to nearest, make sure this is only a 0.00 value not a 0.0000 + float avg = floorf( static_cast( averages[i] ) * 100 + 0.5 ) / 100; + // Don't print trailing whitespace for last element + if ( i == num_averages-1 ) + { + ss << avg; + } + else + { + ss << avg << " "; + } + } + + if( use_colors ) + { + if( use_powerline ) + { + ss << ' '; + } + else + { + ss << "#[fg=default,bg=default]"; + } + } + } } else { - if( use_colors ) - { - unsigned load_percent = static_cast( averages[0] / - get_cpu_count() * 0.5f * 100.0f ); - - if( load_percent > 100 ) - { - load_percent = 100; - } - powerline(ss, load_lut[load_percent], use_powerline); - } - - ss << ' '; - for( int i = 0; i < nelem; ++i ) - { - // Round to nearest, make sure this is only a 0.00 value not a 0.0000 - float avg = floorf( static_cast( averages[i] ) * 100 + 0.5 ) / 100; - // Don't print trailing whitespace for last element - if ( i == nelem-1 ) - { - ss << avg; - } - else - { - ss << avg << " "; - } - } - - if( use_colors ) - { - if( use_powerline ) - { - ss << ' '; - } - else - { - ss << "#[fg=default,bg=default]"; - } - } + ss << (char) 0; } return ss.str(); diff --git a/common/load.h b/common/load.h index b8d50f9..17df7fa 100644 --- a/common/load.h +++ b/common/load.h @@ -2,6 +2,7 @@ * * Copyright 2012 Matthew McCormick * Copyright 2015 Pawel 'l0ner' Soltys + * Copyright 2016 Compilenix * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,6 @@ #include -std::string load_string( bool use_colors = false, bool use_powerline = false ); +std::string load_string( bool use_colors = false, bool use_powerline = false, short num_averages = 3 ); #endif diff --git a/common/main.cc b/common/main.cc index 7d3f265..5d1e910 100644 --- a/common/main.cc +++ b/common/main.cc @@ -100,12 +100,15 @@ void print_help() << "\tSet how many lines should be drawn in a graph. Default: 10\n" << "-m , --mem-mode \n" << "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n" + << "-a , --averages-count \n" + << "\tSet how many load-averages should be drawn. Default: 3\n" << endl; } int main( int argc, char** argv ) { unsigned cpu_usage_delay = 990000; + short averages_count = 3; short graph_lines = 10; // max 32767 should be enough bool use_colors = false; bool use_powerline = false; @@ -123,12 +126,13 @@ int main( int argc, char** argv ) { "interval", required_argument, NULL, 'i' }, { "graph-lines", required_argument, NULL, 'g' }, { "mem-mode", required_argument, NULL, 'm' }, + { "averages-count", required_argument, NULL, 'a' }, { 0, 0, 0, 0 } // used to handle unknown long options }; int c; // while c != -1 - while( (c = getopt_long( argc, argv, "hi:g:m:", long_options, NULL) ) != -1 ) + while( (c = getopt_long( argc, argv, "hi:g:m:a:", long_options, NULL) ) != -1 ) { switch( c ) { @@ -167,6 +171,14 @@ int main( int argc, char** argv ) } mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) ); break; + case 'a': // --averages-count, -a + if( atoi( optarg ) < 0 || atoi( optarg ) > 3 ) + { + std::cerr << "Valid averages-count arguments are: 0, 1, 2, 3\n"; + return EXIT_FAILURE; + } + averages_count = atoi( optarg ); + break; case '?': // getopt_long prints error message automatically return EXIT_FAILURE; @@ -189,7 +201,7 @@ int main( int argc, char** argv ) mem_status( memory_status ); 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 ); + << load_string( use_colors, use_powerline, averages_count ); return EXIT_SUCCESS; } From cbe3074c6bffe751231b78a96595e88fe4efe31f Mon Sep 17 00:00:00 2001 From: Compilenix Date: Thu, 28 Apr 2016 22:13:05 +0200 Subject: [PATCH 2/4] Update README.rst and AUTHORS --- AUTHORS | 1 + README.rst | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/AUTHORS b/AUTHORS index 5a619af..0486dbb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -9,3 +9,4 @@ Contributors (in alphabetical order): krieiter Mark Palmeri Pawel 'l0ner' Soltys + Compilenix diff --git a/README.rst b/README.rst index 188ee6f..c64bda9 100644 --- a/README.rst +++ b/README.rst @@ -126,6 +126,9 @@ The full usage:: Set how many lines should be drawn in a graph. Default: 10 -m , --mem-mode Set memory display mode. 0: Default, 1: Free memory, 2: Usage percent. + -a , --averages-count + Set how many load-averages should be drawn. Default: 3 + Authors @@ -143,6 +146,7 @@ Contributions from: * `Pawel 'l0ner' Soltys`_ * Travil Heller * Tony Narlock +* Compilenix .. _tmux: http://tmux.sourceforge.net/ From 97a655c856cba7ce53adb8f8a884150e053f5635 Mon Sep 17 00:00:00 2001 From: Compilenix Date: Thu, 28 Apr 2016 22:27:19 +0200 Subject: [PATCH 3/4] cleanup --- common/load.cc | 93 +++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/common/load.cc b/common/load.cc index cbc1cf0..d0729c2 100644 --- a/common/load.cc +++ b/common/load.cc @@ -40,58 +40,57 @@ std::string load_string( bool use_colors, bool use_powerline, short num_averages double averages[num_averages]; // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c - if( num_averages > 0) + if( num_averages <= 0 || num_averages > 3) { - if( getloadavg( averages, num_averages ) < 0 ) - { - ss << " 0.00 0.00 0.00"; // couldn't get averages. - } - else - { - if( use_colors ) - { - unsigned load_percent = static_cast( averages[0] / - get_cpu_count() * 0.5f * 100.0f ); + ss << (char) 0; + return ss.str(); + } - if( load_percent > 100 ) - { - load_percent = 100; - } - powerline(ss, load_lut[load_percent], use_powerline); - } - - ss << ' '; - for( int i = 0; i < num_averages; ++i ) - { - // Round to nearest, make sure this is only a 0.00 value not a 0.0000 - float avg = floorf( static_cast( averages[i] ) * 100 + 0.5 ) / 100; - // Don't print trailing whitespace for last element - if ( i == num_averages-1 ) - { - ss << avg; - } - else - { - ss << avg << " "; - } - } - - if( use_colors ) - { - if( use_powerline ) - { - ss << ' '; - } - else - { - ss << "#[fg=default,bg=default]"; - } - } - } + if( getloadavg( averages, num_averages ) < 0 ) + { + ss << " 0.00 0.00 0.00"; // couldn't get averages. } else { - ss << (char) 0; + if( use_colors ) + { + unsigned load_percent = static_cast( averages[0] / + get_cpu_count() * 0.5f * 100.0f ); + + if( load_percent > 100 ) + { + load_percent = 100; + } + powerline(ss, load_lut[load_percent], use_powerline); + } + + ss << ' '; + for( int i = 0; i < num_averages; ++i ) + { + // Round to nearest, make sure this is only a 0.00 value not a 0.0000 + float avg = floorf( static_cast( averages[i] ) * 100 + 0.5 ) / 100; + // Don't print trailing whitespace for last element + if ( i == num_averages-1 ) + { + ss << avg; + } + else + { + ss << avg << " "; + } + } + + if( use_colors ) + { + if( use_powerline ) + { + ss << ' '; + } + else + { + ss << "#[fg=default,bg=default]"; + } + } } return ss.str(); From 710be9e3557f01ac1c1967b9210149cad6bd527d Mon Sep 17 00:00:00 2001 From: Compilenix Date: Fri, 6 May 2016 09:03:18 +0200 Subject: [PATCH 4/4] Added tests for "averages-count" --- CMakeLists.txt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd051dd..c6c334a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ elseif(COMPILER_SUPPORTS_CXX0X) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") else() message(FATAL_ERROR -"Compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION} has no C++11 support.") + "Compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION} has no C++11 support.") endif() # generate header file to handle version @@ -135,6 +135,22 @@ if(BUILD_TESTING) COMMAND tmux-mem-cpu-load -m 2 ) + add_test(NAME averages_count_0 + COMMAND tmux-mem-cpu-load -a 0 + ) + + add_test(NAME averages_count_1 + COMMAND tmux-mem-cpu-load -a 1 + ) + + add_test(NAME averages_count_2 + COMMAND tmux-mem-cpu-load -a 2 + ) + + add_test(NAME averages_count_3 + COMMAND tmux-mem-cpu-load -a 3 + ) + set_tests_properties(usage invalid_status_interval invalid_graph_lines