Merge branch 'colors'

Add support for colored output.
This commit is contained in:
Matt McCormick 2013-06-05 11:55:18 -04:00
commit c13d4087b9
5 changed files with 465 additions and 35 deletions

@ -11,6 +11,8 @@ if(NOT CMAKE_BUILD_TYPE)
FORCE)
endif(NOT CMAKE_BUILD_TYPE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(tmux-mem-cpu-load tmux-mem-cpu-load.cpp)
install(TARGETS tmux-mem-cpu-load
RUNTIME

@ -37,6 +37,9 @@ Example output::
6. Load average for the past 5 minutes.
7. Load average for the past 15 minutes.
For `terminals with 256 color support`_, graded colors can be displayed by
passing the **--colors** flag.
Installation
============
@ -87,7 +90,7 @@ Edit ``$HOME/.tmux.conf`` to display the program's output in *status-left* or
*status-right*. For example::
set -g status-interval 2
set -g status-left "#S #[fg=green,bg=black,bright]#(tmux-mem-cpu-load 2)#[default]"
set -g status-left "#S #[fg=green,bg=black,bright]#(tmux-mem-cpu-load --colors 2)#[default]"
Note that the first argument to `tmux-mem-cpu-load` should be the same number
of seconds that *status-interval* is set at.
@ -106,3 +109,4 @@ Matt McCormick (thewtex) <matt@mmmccormick.com>
.. _tmux: http://tmux.sourceforge.net/
.. _cmake: http://www.cmake.org
.. _`project homepage`: http://github.com/thewtex/tmux-mem-cpu-load
.. _`terminals with 256 color support`: http://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility

43
generate-luts.py Executable file

@ -0,0 +1,43 @@
#!/usr/bin/env python
"""Create the color code lookup table header."""
import matplotlib.cm
def write_table(fp, colormap_name, stat_name, first_half_foreground, second_half_foreground):
fp.write('static const char ')
fp.write(stat_name)
fp.write('_lut[][32] = {\n')
colormap = matplotlib.cm.get_cmap(colormap_name)
for ii in range(101):
fp.write('"#[fg=')
if ii < 50:
fp.write(first_half_foreground)
else:
fp.write(second_half_foreground)
fp.write(',bg=colour')
rgba = colormap(ii * 0.01)
red = int(round(rgba[0] * 5))
green = int(round(rgba[1] * 5))
blue = int(round(rgba[2] * 5))
color = 16 + 36*red + 6*green + blue;
fp.write(str(color))
fp.write(']"')
if ii != 100:
fp.write(',')
fp.write('\n')
fp.write('}; // end ')
fp.write(stat_name)
fp.write('_lut\n\n')
with open('luts.h', 'w') as fp:
fp.write('#ifndef _luts_h\n')
fp.write('#define _luts_h\n\n')
# hot colormap with white fg for the first half
# and black fg for the second half
write_table(fp, 'hot', 'cpu_percentage', 'white', 'black')
write_table(fp, 'gist_earth', 'mem', 'white', 'black')
write_table(fp, 'bone', 'load', 'white', 'black')
fp.write('#endif\n')

316
luts.h Normal file

@ -0,0 +1,316 @@
#ifndef _luts_h
#define _luts_h
static const char cpu_percentage_lut[][32] = {
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour52]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour88]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour124]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour160]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour196]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour202]",
"#[fg=white,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour208]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour214]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour220]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour226]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour227]",
"#[fg=black,bg=colour228]",
"#[fg=black,bg=colour228]",
"#[fg=black,bg=colour228]",
"#[fg=black,bg=colour228]",
"#[fg=black,bg=colour228]",
"#[fg=black,bg=colour229]",
"#[fg=black,bg=colour229]",
"#[fg=black,bg=colour229]",
"#[fg=black,bg=colour229]",
"#[fg=black,bg=colour229]",
"#[fg=black,bg=colour230]",
"#[fg=black,bg=colour230]",
"#[fg=black,bg=colour230]",
"#[fg=black,bg=colour230]",
"#[fg=black,bg=colour230]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]"
}; // end cpu_percentage_lut
static const char mem_lut[][32] = {
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour17]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour18]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour24]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour66]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour72]",
"#[fg=white,bg=colour71]",
"#[fg=white,bg=colour71]",
"#[fg=white,bg=colour71]",
"#[fg=white,bg=colour107]",
"#[fg=white,bg=colour107]",
"#[fg=black,bg=colour107]",
"#[fg=black,bg=colour107]",
"#[fg=black,bg=colour108]",
"#[fg=black,bg=colour108]",
"#[fg=black,bg=colour108]",
"#[fg=black,bg=colour108]",
"#[fg=black,bg=colour108]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour144]",
"#[fg=black,bg=colour150]",
"#[fg=black,bg=colour150]",
"#[fg=black,bg=colour186]",
"#[fg=black,bg=colour186]",
"#[fg=black,bg=colour186]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour180]",
"#[fg=black,bg=colour181]",
"#[fg=black,bg=colour181]",
"#[fg=black,bg=colour181]",
"#[fg=black,bg=colour181]",
"#[fg=black,bg=colour187]",
"#[fg=black,bg=colour187]",
"#[fg=black,bg=colour187]",
"#[fg=black,bg=colour187]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour224]",
"#[fg=black,bg=colour224]",
"#[fg=black,bg=colour224]",
"#[fg=black,bg=colour224]",
"#[fg=black,bg=colour224]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]"
}; // end mem_lut
static const char load_lut[][32] = {
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour16]",
"#[fg=white,bg=colour17]",
"#[fg=white,bg=colour17]",
"#[fg=white,bg=colour17]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour59]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour60]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour102]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=white,bg=colour103]",
"#[fg=black,bg=colour103]",
"#[fg=black,bg=colour103]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour109]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour145]",
"#[fg=black,bg=colour146]",
"#[fg=black,bg=colour146]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour152]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour188]",
"#[fg=black,bg=colour195]",
"#[fg=black,bg=colour195]",
"#[fg=black,bg=colour195]",
"#[fg=black,bg=colour195]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]",
"#[fg=black,bg=colour231]"
}; // end load_lut
#endif

@ -14,17 +14,19 @@
* limitations under the License.
* */
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include <unistd.h> // sleep
#include "luts.h"
float cpu_percentage( unsigned int cpu_usage_delay )
{
string stat_line;
std::string stat_line;
size_t line_start_pos;
size_t line_end_pos;
unsigned long long current_user;
@ -39,9 +41,9 @@ float cpu_percentage( unsigned int cpu_usage_delay )
unsigned long long diff_system;
unsigned long long diff_nice;
unsigned long long diff_idle;
istringstream iss;
std::istringstream iss;
ifstream stat_file("/proc/stat");
std::ifstream stat_file("/proc/stat");
getline(stat_file, stat_line);
stat_file.close();
@ -81,46 +83,56 @@ 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, unsigned int graph_lines )
std::string cpu_string( unsigned int cpu_usage_delay,
unsigned int graph_lines,
bool use_colors = false )
{
string meter( graph_lines + 2, ' ' );
std::string meter( graph_lines + 2, ' ' );
meter[0] = '[';
meter[meter.length() - 1] = ']';
int meter_count = 0;
float percentage;
ostringstream oss;
std::ostringstream oss;
oss.precision( 1 );
oss.setf( ios::fixed | ios::right );
oss.setf( std::ios::fixed | std::ios::right );
percentage = cpu_percentage( cpu_usage_delay );
float meter_step = 99.9 / graph_lines;
meter_count = 1;
while(meter_count*meter_step < percentage)
while( meter_count*meter_step < percentage )
{
meter[meter_count] = '|';
meter_count++;
}
if( use_colors )
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
}
oss << meter;
oss.width( 5 );
oss << percentage;
oss << "%";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}
string mem_string()
std::string mem_string( bool use_colors = false )
{
unsigned int total_mem;
unsigned int used_mem;
unsigned int unused_mem;
size_t line_start_pos;
size_t line_end_pos;
istringstream iss;
ostringstream oss;
string mem_line;
std::istringstream iss;
std::ostringstream oss;
std::string mem_line;
ifstream meminfo_file( "/proc/meminfo" );
std::ifstream meminfo_file( "/proc/meminfo" );
getline( meminfo_file, mem_line );
line_start_pos = mem_line.find_first_of( ':' );
line_start_pos++;
@ -142,17 +154,58 @@ string mem_string()
}
meminfo_file.close();
if( use_colors )
{
oss << mem_lut[(100 * used_mem) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str();
}
string load_string()
std::string load_string( bool use_colors = false )
{
ifstream loadavg_file( "/proc/loadavg" );
string load_line;
getline( loadavg_file, load_line );
std::ifstream loadavg_file( "/proc/loadavg" );
std::string load_line;
std::getline( loadavg_file, load_line );
loadavg_file.close();
if( use_colors )
{
std::ostringstream oss;
std::ifstream stat_file( "/proc/stat" );
std::string stat_line;
std::getline( stat_file, stat_line );
unsigned int number_of_cpus = 0;
std::getline( stat_file, stat_line );
do
{
++number_of_cpus;
std::getline( stat_file, stat_line );
}
while( stat_line.compare( 0, 3, "cpu" ) == 0 && stat_file.good() );
stat_file.close();
std::istringstream iss( load_line.substr( 0, 4 ) );
float recent_load;
iss >> recent_load;
// colors range from zero to twice the number of cpu's for the most recent
// load metric
unsigned int load_percent = static_cast< unsigned int >( recent_load / number_of_cpus * 0.5f * 100.0f );
if( load_percent > 100 )
{
load_percent = 100;
}
oss << load_lut[load_percent];
oss << load_line.substr( 0, 14 );
oss << "#[fg=default,bg=default]";
return oss.str();
}
return load_line.substr( 0, 14 );
}
@ -161,31 +214,43 @@ int main(int argc, char** argv)
{
unsigned int cpu_usage_delay = 900000;
unsigned int graph_lines = 10;
bool use_colors = false;
try
{
istringstream iss;
iss.exceptions ( ifstream::failbit | ifstream::badbit );
if( argc > 1 )
{
iss.str( argv[1] );
std::istringstream iss;
iss.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
std::string current_arg;
unsigned int arg_index = 1;
if( argc > arg_index )
{
if( strcmp( argv[arg_index], "--colors" ) == 0 )
{
use_colors = true;
++arg_index;
}
}
if( argc > arg_index )
{
iss.str( argv[arg_index] );
unsigned int status_interval;
iss >> status_interval;
cpu_usage_delay = status_interval * 1000000 - 100000;
++arg_index;
}
if( argc > arg_index )
{
iss.str( argv[arg_index] );
iss.clear();
iss >> graph_lines;
}
}
if( argc > 2 )
catch(const std::exception &e)
{
iss.str( argv[2] );
iss.clear();
iss >> graph_lines;
}
}
catch(const exception &e)
{
cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << endl;
std::cerr << "Usage: " << argv[0] << " [--colors] [tmux_status-interval(seconds)] [graph lines]" << std::endl;
return 1;
}
}
std::cout << mem_string() << ' ' << cpu_string( cpu_usage_delay, graph_lines ) << ' ' << load_string();
std::cout << mem_string( use_colors ) << ' ' << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors );
return 0;
}