Merge pull request #34 from golinski/master

Adding powerline support
This commit is contained in:
Matt McCormick 2016-04-14 07:01:33 -04:00
commit 34b9d39b57
9 changed files with 136 additions and 16 deletions

@ -80,7 +80,7 @@ else()
endif()
# set common source files
set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc")
set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc" "common/powerline.cc")
# add binary tree so we find version.h
include_directories("${PROJECT_BINARY_DIR}")
@ -111,6 +111,10 @@ if(BUILD_TESTING)
COMMAND tmux-mem-cpu-load --colors
)
add_test(NAME powerline
COMMAND tmux-mem-cpu-load --powerline
)
add_test(NAME invalid_status_interval
COMMAND tmux-mem-cpu-load -i -1
)

@ -118,6 +118,8 @@ The full usage::
Prints this help message
--colors
Use tmux colors in output
--powerline-right
Use powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors
-i <value>, --interval <value>
Set tmux status refresh interval in seconds. Default: 1 second
-g <value>, --graph-lines <value>

@ -30,10 +30,12 @@
#include "load.h"
#include "luts.h"
#include "powerline.h"
// Load Averages
std::string load_string( bool use_colors = false )
std::string load_string( bool use_colors, bool use_powerline )
{
std::stringstream ss;
std::ostringstream ss;
// Get only 3 load averages
const int nelem = 3;
double averages[nelem];
@ -54,7 +56,7 @@ std::string load_string( bool use_colors = false )
{
load_percent = 100;
}
ss << load_lut[load_percent];
powerline(ss, load_lut[load_percent], use_powerline);
}
ss << ' ';
@ -74,10 +76,17 @@ std::string load_string( bool use_colors = false )
}
if( use_colors )
{
if( use_powerline )
{
ss << ' ';
}
else
{
ss << "#[fg=default,bg=default]";
}
}
}
return ss.str();
}

@ -21,6 +21,6 @@
#include <string>
std::string load_string( bool );
std::string load_string( bool use_colors = false, bool use_powerline = false );
#endif

@ -33,8 +33,10 @@
#include "memory.h"
#include "load.h"
#include "powerline.h"
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false )
bool use_colors = false, bool use_powerline = false )
{
float percentage;
@ -49,7 +51,8 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
if( use_colors )
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
unsigned int percent = static_cast<unsigned int>( percentage );
powerline(oss, cpu_percentage_lut[percent], use_powerline);
}
if( graph_lines > 0)
@ -62,9 +65,16 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
oss << percentage;
oss << "%";
if( use_colors )
{
if( use_powerline )
{
oss << ' ';
}
else
{
oss << "#[fg=default,bg=default]";
}
}
return oss.str();
}
@ -79,8 +89,11 @@ void print_help()
<< "Available options:\n"
<< "-h, --help\n"
<< "\t Prints this help message\n"
<< "-c, --colors\n"
<< "--colors\n"
<< "\tUse tmux colors in output\n"
<< "-p, --powerline-right\n"
<< "\tUse powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors\n"
<< "-i <value>, --interval <value>\n"
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
<< "-g <value>, --graph-lines <value>\n"
@ -95,6 +108,7 @@ int main( int argc, char** argv )
unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough
bool use_colors = false;
bool use_powerline = false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
static struct option long_options[] =
@ -105,6 +119,7 @@ int main( int argc, char** argv )
// otherwise it's a value to set the variable *flag points to
{ "help", no_argument, NULL, 'h' },
{ "colors", no_argument, NULL, 'c' },
{ "powerline-right", no_argument, NULL, 'p' },
{ "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
@ -124,6 +139,10 @@ int main( int argc, char** argv )
case 'c': // --colors
use_colors = true;
break;
case 'p': // --powerline-right
use_colors = true;
use_powerline = true;
break;
case 'i': // --interval, -i
if( atoi( optarg ) < 1 )
{
@ -168,9 +187,9 @@ int main( int argc, char** argv )
MemoryStatus memory_status;
mem_status( memory_status );
std::cout << mem_string( memory_status, mem_mode, use_colors )
<< cpu_string( cpu_usage_delay, graph_lines, use_colors )
<< load_string( use_colors );
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline )
<< cpu_string( cpu_usage_delay, graph_lines, use_colors, use_powerline )
<< load_string( use_colors, use_powerline );
return EXIT_SUCCESS;
}

@ -21,10 +21,12 @@
#include "memory.h"
#include "luts.h"
#include "conversions.h"
#include "powerline.h"
std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode,
bool use_colors )
bool use_colors,
bool use_powerline )
{
std::ostringstream oss;
// Change the percision for floats, for a pretty output
@ -33,7 +35,8 @@ std::string mem_string( const MemoryStatus & mem_status,
if( use_colors )
{
oss << mem_lut[static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem)];
unsigned int color = static_cast< unsigned int >((100 * mem_status.used_mem) / mem_status.total_mem);
powerline(oss, mem_lut[color], use_powerline);
}
switch( mode )
@ -69,9 +72,16 @@ std::string mem_string( const MemoryStatus & mem_status,
}
if( use_colors )
{
if( use_powerline )
{
oss << " ";
}
else
{
oss << "#[fg=default,bg=default]";
}
}
return oss.str();
}

@ -49,6 +49,7 @@ enum MEMORY_MODE
std::string mem_string( const MemoryStatus & mem_status,
MEMORY_MODE mode = MEMORY_MODE_DEFAULT,
bool use_colors = false );
bool use_colors = false,
bool use_powerline = false );
#endif

49
common/powerline.cc Normal file

@ -0,0 +1,49 @@
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick
* Copyright 2016 Michał Goliński
*
* 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.
*/
#include "powerline.h"
#include <cstring>
#include <sstream>
#define PWL_RIGHT_FILLED ""
const char * bg2fg(const char s[])
{
static char buf[40] = {0};
const char *substr = index(s, ',');
buf[0] = '#';
buf[1] = '[';
buf[2] = 'f';
strcpy(buf+3, substr+2);
return buf;
}
void powerline(std::ostringstream &oss, const char s[], bool use_powerline)
{
if( use_powerline )
{
oss << bg2fg(s)
<< PWL_RIGHT_FILLED
<< s << ' ';
}
else
{
oss << s;
}
}

26
common/powerline.h Normal file

@ -0,0 +1,26 @@
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick
* Copyright 2016 Michał Goliński
*
* 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.
*/
#ifndef POWERLINE_H
#define POWERLINE_H
#include <sstream>
void powerline(std::ostringstream &oss, const char s[], bool);
#endif // POWERLINE_H