diff --git a/CMakeLists.txt b/CMakeLists.txt index cfa4780..cbd2825 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/generate-luts.py b/generate-luts.py new file mode 100755 index 0000000..477c6e0 --- /dev/null +++ b/generate-luts.py @@ -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') diff --git a/luts.h b/luts.h new file mode 100644 index 0000000..1bf2945 --- /dev/null +++ b/luts.h @@ -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 diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index bd9d0d5..fd6b994 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -22,6 +22,8 @@ #include // sleep +#include "luts.h" + float cpu_percentage( unsigned int cpu_usage_delay ) { std::string stat_line; @@ -81,7 +83,9 @@ float cpu_percentage( unsigned int cpu_usage_delay ) return static_cast(diff_user + diff_system + diff_nice)/static_cast(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, ' ' ); meter[0] = '['; @@ -95,21 +99,29 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines ) 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( percentage )]; + } oss << meter; oss.width( 5 ); oss << percentage; oss << "%"; + if( use_colors ) + { + oss << "#[fg=default,bg=default]"; + } return oss.str(); } -std::string mem_string() +std::string mem_string( bool use_colors = false ) { unsigned int total_mem; unsigned int used_mem; @@ -142,17 +154,58 @@ std::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(); } -std::string load_string() +std::string load_string( bool use_colors = false ) { 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 ); } @@ -197,8 +250,7 @@ int main(int argc, char** argv) return 1; } - std::cout << use_colors << " " << cpu_usage_delay << " " << graph_lines << std::endl; - 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; }