2016-04-14 06:18:10 -04:00
|
|
|
/* 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-04-14 06:18:10 -04:00
|
|
|
{
|
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
|
2016-08-01 12:46:31 -04:00
|
|
|
<< color;
|
2016-07-31 21:57:56 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case POWERLINE_RIGHT:
|
|
|
|
oss << ' '
|
|
|
|
<< bg2fg( color )
|
|
|
|
<< PWL_RIGHT_FILLED
|
2016-08-02 18:41:13 -04:00
|
|
|
<< color;
|
2016-07-31 21:57:56 -04:00
|
|
|
break;
|
|
|
|
};
|
2016-03-05 05:19:31 -05:00
|
|
|
}
|
2023-03-27 11:11:55 -04:00
|
|
|
|
|
|
|
void powerline_char( std::ostringstream & oss, const char dynamic_color[],
|
|
|
|
short static_color, POWERLINE_DIRECTION direction, bool eol )
|
|
|
|
{
|
|
|
|
char write_color[7];
|
|
|
|
sprintf(write_color, "%d", static_color);
|
|
|
|
switch( direction )
|
|
|
|
{
|
|
|
|
case POWERLINE_LEFT:
|
|
|
|
if ( eol )
|
|
|
|
{
|
|
|
|
oss << bg2fg( dynamic_color )
|
|
|
|
<< "#[bg=colour"
|
|
|
|
<< write_color
|
|
|
|
<< "]";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oss << dynamic_color
|
|
|
|
<< "#[fg=colour"
|
|
|
|
<< write_color
|
|
|
|
<< "]";
|
|
|
|
}
|
|
|
|
oss << PWL_LEFT_FILLED
|
|
|
|
<< dynamic_color;
|
|
|
|
break;
|
|
|
|
case POWERLINE_RIGHT:
|
|
|
|
if ( eol )
|
|
|
|
{
|
|
|
|
oss << dynamic_color
|
|
|
|
<< "#[fg=colour"
|
|
|
|
<< write_color
|
|
|
|
<< "] ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oss << bg2fg(dynamic_color)
|
|
|
|
<< " #[bg=colour"
|
|
|
|
<< write_color
|
|
|
|
<< "]";
|
|
|
|
}
|
|
|
|
oss << PWL_RIGHT_FILLED
|
|
|
|
<< dynamic_color;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|