Make number of bar graph lines optional.

The default is still 10.  If you have a machine with many cores, this can be
used to show how many cores are active.
This commit is contained in:
Matt McCormick (thewtex) 2010-08-01 10:17:42 -05:00
parent 457834ee1b
commit b1109a31d9
2 changed files with 30 additions and 16 deletions

@ -17,8 +17,7 @@ line of **tmux**.
The memory monitor displays the used and available memory.
The CPU usage monitor outputs a percent CPU usage over all processors. It also
displays a textual bar graph of the current percent usage where every '|'
character represents 10% usage.
displays a textual bar graph of the current percent usage.
The system load average is also displayed.
@ -93,6 +92,10 @@ Edit ``$HOME/.tmux.conf`` to display the program's output in *status-left* or
Note that the first argument to `tmux-mem-cpu-load` should be the same number
of seconds that *status-interval* is set at.
An optional second argument is the number of bars in the bar graph, which
defaults to 10. This can, for instance, be set to the number of cores in a
multi-core system.
Author
======

@ -66,9 +66,11 @@ float cpu_percentage( unsigned int cpu_usage_delay )
return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0;
}
string cpu_string( unsigned int cpu_usage_delay )
string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines )
{
string meter = "[ ]";
string meter( graph_lines + 2, ' ' );
meter[0] = '[';
meter[meter.length() - 1] = ']';
int meter_count = 0;
float percentage;
ostringstream oss;
@ -76,8 +78,9 @@ string cpu_string( unsigned int cpu_usage_delay )
oss.setf( ios::fixed | ios::right );
percentage = cpu_percentage( cpu_usage_delay );
float meter_step = 99.9 / graph_lines;
meter_count = 1;
while(meter_count*9.99 < percentage)
while(meter_count*meter_step < percentage)
{
meter[meter_count] = '|';
meter_count++;
@ -142,24 +145,32 @@ string load_string()
int main(int argc, char** argv)
{
unsigned int cpu_usage_delay = 900000;
unsigned int graph_lines = 10;
try
{
istringstream iss;
iss.exceptions ( ifstream::failbit | ifstream::badbit );
if( argc > 1 )
{
try
{
istringstream iss( argv[1] );
iss.exceptions ( ifstream::failbit | ifstream::badbit );
iss.str( argv[1] );
unsigned int status_interval;
iss >> status_interval;
cpu_usage_delay = status_interval * 1000000 - 100000;
}
catch(const exception &e)
if( argc > 2 )
{
cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)]" << endl;
return 1;
}
iss.str( argv[2] );
iss.clear();
iss >> graph_lines;
}
}
catch(const exception &e)
{
cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << endl;
return 1;
}
std::cout << mem_string() << ' ' << cpu_string( cpu_usage_delay ) << ' ' << load_string();
std::cout << mem_string() << ' ' << cpu_string( cpu_usage_delay, graph_lines ) << ' ' << load_string();
return 0;
}