2015-01-18 08:31:04 -05:00
|
|
|
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
|
|
|
|
*
|
2012-11-25 12:19:50 -05:00
|
|
|
* 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.
|
2015-01-18 08:31:04 -05:00
|
|
|
*/
|
2010-02-24 19:49:10 -05:00
|
|
|
|
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
|
2015-01-18 08:31:04 -05:00
|
|
|
|
2015-01-07 14:57:25 -05:00
|
|
|
#include "argParse/argParse.h"
|
2015-01-18 14:04:28 -05:00
|
|
|
#include "version.h"
|
2015-01-18 08:31:04 -05:00
|
|
|
#include "graph.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"
|
|
|
|
|
2013-04-15 15:15:31 -04:00
|
|
|
#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
|
2015-01-10 12:34:37 -05:00
|
|
|
#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"
|
2013-04-15 15:15:31 -04:00
|
|
|
#endif
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
|
|
|
|
bool use_colors = false )
|
|
|
|
{
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-04 11:23:39 -05:00
|
|
|
float percentage;
|
2014-04-19 18:36:59 -04:00
|
|
|
|
2015-01-07 14:57:25 -05:00
|
|
|
//output stuff
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ostringstream oss;
|
2010-02-24 19:49:10 -05:00
|
|
|
oss.precision( 1 );
|
2013-04-24 22:13:50 -04:00
|
|
|
oss.setf( std::ios::fixed | std::ios::right );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2014-04-19 18:36:59 -04:00
|
|
|
// get %
|
2010-02-25 09:54:18 -05:00
|
|
|
percentage = cpu_percentage( cpu_usage_delay );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
|
|
|
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
|
|
|
|
}
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
if( graph_lines > 0)
|
|
|
|
{
|
|
|
|
oss << "[";
|
|
|
|
oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
|
|
|
|
oss << "]";
|
2015-01-10 11:32:08 -05:00
|
|
|
}
|
2010-02-24 19:49:10 -05:00
|
|
|
oss.width( 5 );
|
|
|
|
oss << percentage;
|
|
|
|
oss << "%";
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
|
|
|
oss << "#[fg=default,bg=default]";
|
|
|
|
}
|
2010-02-25 00:37:21 -05:00
|
|
|
|
2010-02-24 20:02:55 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2015-01-07 14:57:25 -05:00
|
|
|
using namespace ArgvParse;
|
|
|
|
|
2015-01-08 14:04:27 -05:00
|
|
|
unsigned cpu_usage_delay = 990000;
|
2015-01-08 13:53:22 -05:00
|
|
|
short graph_lines = 10; // max 32767 should be enough
|
2013-02-21 00:08:14 -05:00
|
|
|
bool use_colors = false;
|
2015-01-07 14:57:25 -05:00
|
|
|
|
|
|
|
// Argv parser
|
|
|
|
ArgvParser arg;
|
|
|
|
|
2015-01-08 13:31:25 -05:00
|
|
|
// ugly, I know
|
|
|
|
std::string intro = "tmux-mem-cpu-load v";
|
2015-01-18 08:51:27 -05:00
|
|
|
intro += tmux_mem_cpu_load_VERSION;
|
|
|
|
intro += "\nUsage: tmux-mem-cpu-load [OPTIONS]";
|
2015-01-08 13:31:25 -05:00
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
arg.setIntroduction( intro );
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
arg.setHelpOption( "h", "help", "Prints this help message" );
|
2015-01-07 14:57:25 -05:00
|
|
|
|
|
|
|
// define actual options
|
2015-01-18 08:31:04 -05:00
|
|
|
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;
|
2015-01-07 14:57:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// mangle arguments
|
2015-01-18 08:31:04 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
cpu_usage_delay = delay * 1000000 - 10000;
|
2015-01-07 14:57:25 -05:00
|
|
|
}
|
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
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-07 14:57:25 -05:00
|
|
|
}
|
2010-08-01 11:17:42 -04:00
|
|
|
|
2015-01-04 11:33:03 -05:00
|
|
|
std::cout << mem_string( use_colors ) << ' '
|
2015-01-18 08:31:04 -05:00
|
|
|
<< cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' '
|
|
|
|
<< load_string( use_colors );
|
2010-02-25 09:54:18 -05:00
|
|
|
|
2013-06-09 21:01:32 -04:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-09 02:28:04 -04:00
|
|
|
}
|
|
|
|
|