Add support for colors.

Colors are added for terminals that have 256 color support when the
--colors flag is enabled.  The background colors are varied with different color
maps derived from their matplotlib versions.  The foreground color is a
contrasting black or white.
This commit is contained in:
Matt McCormick 2013-04-30 06:38:39 -04:00
parent 816a26bab5
commit b2f26bab3f
4 changed files with 419 additions and 6 deletions

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

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

@ -22,6 +22,8 @@
#include <unistd.h> // sleep #include <unistd.h> // sleep
#include "luts.h"
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
std::string stat_line; std::string stat_line;
@ -81,7 +83,9 @@ 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; return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0;
} }
std::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 )
{ {
std::string meter( graph_lines + 2, ' ' ); std::string meter( graph_lines + 2, ' ' );
meter[0] = '['; meter[0] = '[';
@ -101,15 +105,23 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines )
meter_count++; meter_count++;
} }
if( use_colors )
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
}
oss << meter; oss << meter;
oss.width( 5 ); oss.width( 5 );
oss << percentage; oss << percentage;
oss << "%"; oss << "%";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }
std::string mem_string() std::string mem_string( bool use_colors = false )
{ {
unsigned int total_mem; unsigned int total_mem;
unsigned int used_mem; unsigned int used_mem;
@ -142,17 +154,58 @@ std::string mem_string()
} }
meminfo_file.close(); meminfo_file.close();
if( use_colors )
{
oss << mem_lut[(100 * used_mem) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB"; oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors )
{
oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }
std::string load_string() std::string load_string( bool use_colors = false )
{ {
std::ifstream loadavg_file( "/proc/loadavg" ); std::ifstream loadavg_file( "/proc/loadavg" );
std::string load_line; std::string load_line;
std::getline( loadavg_file, load_line ); std::getline( loadavg_file, load_line );
loadavg_file.close(); 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 ); return load_line.substr( 0, 14 );
} }
@ -197,8 +250,7 @@ int main(int argc, char** argv)
return 1; return 1;
} }
std::cout << use_colors << " " << cpu_usage_delay << " " << graph_lines << std::endl; std::cout << mem_string( use_colors ) << ' ' << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors );
std::cout << mem_string() << ' ' << cpu_string( cpu_usage_delay, graph_lines ) << ' ' << load_string();
return 0; return 0;
} }