tmux-mem-cpu-load/tmux-mem-cpu-load.cpp

143 lines
3.7 KiB
C++
Raw Normal View History

/*
* Copyright 2012 Matthew McCormick
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* */
2013-02-21 00:08:14 -05:00
#include <cstring>
2015-01-07 08:15:08 -05:00
#include <iostream>
#include <fstream>
2009-09-09 02:28:04 -04:00
#include <sstream>
#include <string>
2013-06-09 21:01:32 -04:00
#include <cstdlib> // EXIT_SUCCESS
#include "argParse/argParse.h"
2009-09-09 02:28:04 -04:00
2014-04-14 14:51:46 -04:00
// Tmux color lookup tables for the different metrics.
#include "luts.h"
2015-01-07 08:31:49 -05:00
#include "version.h"
#if defined(__APPLE__) && defined(__MACH__)
2014-04-14 14:51:46 -04:00
// Apple osx system
#include "osx/cpu.h"
#include "osx/memory.h"
#include "osx/load.h"
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
// BSD system
#define BSD_BASED 1
#include "bsd/cpu.h"
#include "bsd/load.h"
#include "bsd/memory.h"
2014-04-14 14:51:46 -04:00
#else
// assume linux system
#include "linux/cpu.h"
#include "linux/memory.h"
#include "linux/load.h"
#endif
2009-09-09 02:28:04 -04:00
#include "graph.h"
std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false) {
2015-01-04 11:23:39 -05:00
float percentage;
//output stuff
std::ostringstream oss;
oss.precision( 1 );
oss.setf( std::ios::fixed | std::ios::right );
// get %
percentage = cpu_percentage( cpu_usage_delay );
if( use_colors )
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
if( graph_lines > 0) {
oss << "[";
oss << getGraphByPercentage( unsigned(percentage), graph_lines );
oss << "]";
}
oss.width( 5 );
oss << percentage;
oss << "%";
if( use_colors )
oss << "#[fg=default,bg=default]";
2010-02-25 00:37:21 -05:00
2010-02-24 20:02:55 -05:00
return oss.str();
}
2014-04-14 14:51:46 -04:00
int main(int argc, char** argv) {
using namespace ArgvParse;
2015-01-08 14:04:27 -05:00
unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough
2013-02-21 00:08:14 -05:00
bool use_colors = false;
// Argv parser
ArgvParser arg;
// ugly, I know
std::string intro = "tmux-mem-cpu-load v";
intro += std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR);
intro += "." + std::to_string(VERSION_PATCH) + "\n";
intro += "Usage: tmux-mem-cpu-load [OPTIONS]";
arg.setIntroduction(intro);
arg.setHelpOption("h", "help", "Prints this help message");
// define actual options
arg.defineOption("colors", "Use tmux colors in output",
ArgvParser::NoAttribute);
arg.defineOption("i", "interval", "set tmux status refresh interval in "
"seconds. Default: 1 second", ArgvParser::RequiresValue);
arg.defineOption("g", "graph-lines", "Set how many lines should be drawn in "
"a graph. Default: 10", ArgvParser::RequiresValue);
int result = arg.parse(argc, argv);
if (result != ArgvParser::Success) {
std::cerr << arg.parseErrorDescription(result);
return EXIT_FAILURE;
}
// mangle arguments
if (arg.foundOption("colors"))
use_colors = true;
if (arg.foundOption("interval")) {
int delay = std::stoi(arg.optionValue("interval"));
if (delay < 1) {
std::cerr << "Status interval argument must be one or greater.\n";
return EXIT_FAILURE;
}
2015-01-08 14:04:27 -05:00
cpu_usage_delay = delay * 1000000 - 10000;
}
if (arg.foundOption("graph-lines")) {
graph_lines = std::stoi(arg.optionValue("graph-lines"));
if( graph_lines < 0 ) {
std::cerr << "Graph lines argument must be zero or greater.\n";
return EXIT_FAILURE;
}
}
2015-01-04 11:33:03 -05:00
std::cout << mem_string( use_colors ) << ' '
<< cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' '
<< load_string( use_colors );
2013-06-09 21:01:32 -04:00
return EXIT_SUCCESS;
2009-09-09 02:28:04 -04:00
}