From a2df46dae53cb0b5fe540f492f4921918b7dabf2 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 11 Jan 2015 18:27:08 +0100 Subject: [PATCH] colorized output on *BSD systems --- bsd/load.cc | 22 ++++++++++++++++++++++ bsd/memory_freebsd.cc | 7 +++++++ bsd/memory_openbsd.cc | 15 ++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/bsd/load.cc b/bsd/load.cc index f53d470..695ca96 100644 --- a/bsd/load.cc +++ b/bsd/load.cc @@ -22,8 +22,11 @@ #include #include // getloadavg() #include // floorf() +#include +#include "common.h" #include "load.h" +#include "../luts.h" // Load Averages std::string load_string( bool use_colors = false ) { @@ -32,14 +35,33 @@ std::string load_string( bool use_colors = false ) { int nelem = 3; double averages[3]; // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c + if(getloadavg(averages, nelem) < 0) ss << "0.00 0.00 0.00"; // couldn't get averages. else { + if( use_colors ) { + // may not work + int32_t cpu_count = 0; + GETSYSCTL("hw.ncpu", cpu_count); + + unsigned load_percent = static_cast( + averages[0] / cpu_count * 0.5f * 100.0f); + + if( load_percent > 100 ) + load_percent = 100; + + ss << load_lut[load_percent]; + } + for(int i = 0; i < nelem; ++i) { // Round to nearest, make sure this is only a 0.00 value not a 0.0000 float avg = floorf(static_cast(averages[i]) * 100 + 0.5) / 100; ss << avg << " "; } + + if( use_colors ) + ss << "#[fg=default,bg=default]"; + } return ss.str(); diff --git a/bsd/memory_freebsd.cc b/bsd/memory_freebsd.cc index 9651fa6..ce7fe56 100644 --- a/bsd/memory_freebsd.cc +++ b/bsd/memory_freebsd.cc @@ -24,6 +24,7 @@ #include "common.h" #include "memory.h" +#include "../luts.h" std::string mem_string( bool use_colors = false ) { // These values are in bytes @@ -54,8 +55,14 @@ std::string mem_string( bool use_colors = false ) { static_cast(active_mem) + static_cast(inactive_mem) + static_cast(wired_mem)) * static_cast(page_size); + if( use_colors ) + oss << mem_lut[(100 * used_mem) / total_mem]; + oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; + if( use_colors ) + oss << "#[fg=default,bg=default]"; + return oss.str(); } diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc index c2cc3a9..8264054 100644 --- a/bsd/memory_openbsd.cc +++ b/bsd/memory_openbsd.cc @@ -25,6 +25,7 @@ #include "common.h" #include "memory.h" +#include "../luts.h" std::string mem_string( bool use_colors = false ) { // These values are in bytes @@ -50,14 +51,18 @@ std::string mem_string( bool use_colors = false ) { // Get all memory which can be allocated //unused_mem = (inactive_mem + cache_mem + free_mem) * page_size; - used_mem = ( - static_cast(active_mem) + - static_cast(wired_mem) + - static_cast(inactive_mem)) * - static_cast(page_size); + used_mem = ( + static_cast(active_mem) + static_cast(wired_mem) + + static_cast(inactive_mem)) * static_cast(page_size); + + if( use_colors ) + oss << mem_lut[(100 * used_mem) / total_mem]; oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; + if( use_colors ) + oss << "#[fg=default,bg=default]"; + return oss.str(); }