added averages-count option

This commit is contained in:
Compilenix 2016-04-28 22:05:43 +02:00
parent 34b9d39b57
commit 1d1a516eda
3 changed files with 67 additions and 48 deletions

@ -3,6 +3,7 @@
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com> * Copyright 2013 Justin Crawford <Justasic@gmail.com>
* Copyright 2015 Pawel 'l0ner' Soltys * Copyright 2015 Pawel 'l0ner' Soltys
* Copyright 2016 Compilenix
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,15 +34,15 @@
#include "powerline.h" #include "powerline.h"
// Load Averages // 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; std::ostringstream ss;
// Get only 3 load averages double averages[num_averages];
const int nelem = 3;
double averages[nelem];
// based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c
if( getloadavg( averages, nelem ) < 0 ) if( num_averages > 0)
{
if( getloadavg( averages, num_averages ) < 0 )
{ {
ss << " 0.00 0.00 0.00"; // couldn't get averages. ss << " 0.00 0.00 0.00"; // couldn't get averages.
} }
@ -60,12 +61,12 @@ std::string load_string( bool use_colors, bool use_powerline )
} }
ss << ' '; ss << ' ';
for( int i = 0; i < nelem; ++i ) for( int i = 0; i < num_averages; ++i )
{ {
// Round to nearest, make sure this is only a 0.00 value not a 0.0000 // Round to nearest, make sure this is only a 0.00 value not a 0.0000
float avg = floorf( static_cast<float>( averages[i] ) * 100 + 0.5 ) / 100; float avg = floorf( static_cast<float>( averages[i] ) * 100 + 0.5 ) / 100;
// Don't print trailing whitespace for last element // Don't print trailing whitespace for last element
if ( i == nelem-1 ) if ( i == num_averages-1 )
{ {
ss << avg; ss << avg;
} }
@ -87,6 +88,11 @@ std::string load_string( bool use_colors, bool use_powerline )
} }
} }
} }
}
else
{
ss << (char) 0;
}
return ss.str(); return ss.str();
} }

@ -2,6 +2,7 @@
* *
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2015 Pawel 'l0ner' Soltys * Copyright 2015 Pawel 'l0ner' Soltys
* Copyright 2016 Compilenix
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +22,6 @@
#include <string> #include <string>
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 #endif

@ -100,12 +100,15 @@ void print_help()
<< "\tSet how many lines should be drawn in a graph. Default: 10\n" << "\tSet how many lines should be drawn in a graph. Default: 10\n"
<< "-m <value>, --mem-mode <value>\n" << "-m <value>, --mem-mode <value>\n"
<< "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n" << "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n"
<< "-a <value>, --averages-count <value>\n"
<< "\tSet how many load-averages should be drawn. Default: 3\n"
<< endl; << endl;
} }
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
unsigned cpu_usage_delay = 990000; unsigned cpu_usage_delay = 990000;
short averages_count = 3;
short graph_lines = 10; // max 32767 should be enough short graph_lines = 10; // max 32767 should be enough
bool use_colors = false; bool use_colors = false;
bool use_powerline = false; bool use_powerline = false;
@ -123,12 +126,13 @@ int main( int argc, char** argv )
{ "interval", required_argument, NULL, 'i' }, { "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' }, { "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' }, { "mem-mode", required_argument, NULL, 'm' },
{ "averages-count", required_argument, NULL, 'a' },
{ 0, 0, 0, 0 } // used to handle unknown long options { 0, 0, 0, 0 } // used to handle unknown long options
}; };
int c; int c;
// while c != -1 // 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 ) switch( c )
{ {
@ -167,6 +171,14 @@ int main( int argc, char** argv )
} }
mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) ); mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) );
break; 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 '?': case '?':
// getopt_long prints error message automatically // getopt_long prints error message automatically
return EXIT_FAILURE; return EXIT_FAILURE;
@ -189,7 +201,7 @@ int main( int argc, char** argv )
mem_status( memory_status ); mem_status( memory_status );
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline ) std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline )
<< cpu_string( cpu_usage_delay, graph_lines, 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; return EXIT_SUCCESS;
} }