tmux-mem-cpu-load/common/powerline.cc

69 lines
1.6 KiB
C++
Raw Permalink Normal View History

/* 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.
*/
2016-03-05 05:19:31 -05:00
#include "powerline.h"
2016-07-31 21:57:56 -04:00
#include <string>
2016-03-05 05:19:31 -05:00
#include <cstring>
#include <sstream>
2016-07-31 21:57:56 -04:00
#define PWL_LEFT_FILLED ""
2016-03-05 05:19:31 -05:00
#define PWL_RIGHT_FILLED ""
2016-07-31 21:57:56 -04:00
const char * bg2fg( const char s[] )
2016-03-05 05:19:31 -05:00
{
static char buf[40] = {0};
2018-04-23 21:47:03 -04:00
const char *substr = strchr(s, ',');
2016-03-05 05:19:31 -05:00
buf[0] = '#';
buf[1] = '[';
buf[2] = 'f';
strcpy(buf+3, substr+2);
return buf;
}
2016-07-31 21:57:56 -04:00
void powerline( std::ostringstream & oss, const char color[],
POWERLINE_DIRECTION direction, bool background_only )
2016-03-05 05:19:31 -05:00
{
2016-07-31 21:57:56 -04:00
switch( direction )
{
2016-07-31 21:57:56 -04:00
case NONE:
oss << color;
break;
case POWERLINE_LEFT:
if( background_only )
{
oss << ' ' << bg2fg( color );
}
2016-03-05 05:19:31 -05:00
else
2016-07-31 21:57:56 -04:00
{
std::string colorstr( color );
oss << "#[" << colorstr.substr( colorstr.find( "," ) + 1 )
<< PWL_LEFT_FILLED
<< color;
2016-07-31 21:57:56 -04:00
}
break;
case POWERLINE_RIGHT:
oss << ' '
<< bg2fg( color )
<< PWL_RIGHT_FILLED
<< color;
2016-07-31 21:57:56 -04:00
break;
};
2016-03-05 05:19:31 -05:00
}