colorized output on *BSD systems

This commit is contained in:
Pawel "l0ner" Soltys 2015-01-11 18:27:08 +01:00
parent e8dd4b442f
commit a2df46dae5
3 changed files with 39 additions and 5 deletions

@ -22,8 +22,11 @@
#include <string> #include <string>
#include <stdlib.h> // getloadavg() #include <stdlib.h> // getloadavg()
#include <cmath> // floorf() #include <cmath> // floorf()
#include <sys/types.h>
#include "common.h"
#include "load.h" #include "load.h"
#include "../luts.h"
// Load Averages // Load Averages
std::string load_string( bool use_colors = false ) { std::string load_string( bool use_colors = false ) {
@ -32,14 +35,33 @@ std::string load_string( bool use_colors = false ) {
int nelem = 3; int nelem = 3;
double averages[3]; double averages[3];
// based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c
if(getloadavg(averages, nelem) < 0) if(getloadavg(averages, nelem) < 0)
ss << "0.00 0.00 0.00"; // couldn't get averages. ss << "0.00 0.00 0.00"; // couldn't get averages.
else { else {
if( use_colors ) {
// may not work
int32_t cpu_count = 0;
GETSYSCTL("hw.ncpu", cpu_count);
unsigned load_percent = static_cast<unsigned int>(
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) { for(int i = 0; i < nelem; ++i) {
// Round to nearest, make sure this is only a 0.00 value not a 0.0000 // Round to nearest, make sure this is only a 0.00 value not a 0.0000
float avg = floorf(static_cast<float>(averages[i]) * 100 + 0.5) / 100; float avg = floorf(static_cast<float>(averages[i]) * 100 + 0.5) / 100;
ss << avg << " "; ss << avg << " ";
} }
if( use_colors )
ss << "#[fg=default,bg=default]";
} }
return ss.str(); return ss.str();

@ -24,6 +24,7 @@
#include "common.h" #include "common.h"
#include "memory.h" #include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false ) {
// These values are in bytes // These values are in bytes
@ -54,8 +55,14 @@ std::string mem_string( bool use_colors = false ) {
static_cast<int64_t>(active_mem) + static_cast<int64_t>(inactive_mem) + static_cast<int64_t>(active_mem) + static_cast<int64_t>(inactive_mem) +
static_cast<int64_t>(wired_mem)) * static_cast<int64_t>(page_size); static_cast<int64_t>(wired_mem)) * static_cast<int64_t>(page_size);
if( use_colors )
oss << mem_lut[(100 * used_mem) / total_mem];
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
if( use_colors )
oss << "#[fg=default,bg=default]";
return oss.str(); return oss.str();
} }

@ -25,6 +25,7 @@
#include "common.h" #include "common.h"
#include "memory.h" #include "memory.h"
#include "../luts.h"
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false ) {
// These values are in bytes // These values are in bytes
@ -51,13 +52,17 @@ std::string mem_string( bool use_colors = false ) {
// Get all memory which can be allocated // Get all memory which can be allocated
//unused_mem = (inactive_mem + cache_mem + free_mem) * page_size; //unused_mem = (inactive_mem + cache_mem + free_mem) * page_size;
used_mem = ( used_mem = (
static_cast<int64_t>(active_mem) + static_cast<int64_t>(active_mem) + static_cast<int64_t>(wired_mem) +
static_cast<int64_t>(wired_mem) + static_cast<int64_t>(inactive_mem)) * static_cast<int64_t>(page_size);
static_cast<int64_t>(inactive_mem)) *
static_cast<int64_t>(page_size); if( use_colors )
oss << mem_lut[(100 * used_mem) / total_mem];
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
if( use_colors )
oss << "#[fg=default,bg=default]";
return oss.str(); return oss.str();
} }