lnav/src/view_curses.cc

316 lines
11 KiB
C++
Raw Normal View History

2009-09-14 01:07:32 +00:00
/**
2013-05-03 06:02:03 +00:00
* Copyright (c) 2007-2012, Timothy Stack
*
* All rights reserved.
*
2013-05-03 06:02:03 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
2013-05-03 06:02:03 +00:00
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
2013-05-03 06:02:03 +00:00
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
2009-09-14 01:07:32 +00:00
* @file view_curses.cc
*/
#include "config.h"
#include <string>
#include <algorithm>
2009-09-14 01:07:32 +00:00
#include "view_curses.hh"
using namespace std;
void view_curses::mvwattrline(WINDOW *window,
2013-05-28 04:35:00 +00:00
int y,
int x,
attr_line_t &al,
struct line_range &lr,
view_colors::role_t base_role)
2009-09-14 01:07:32 +00:00
{
2013-05-28 04:35:00 +00:00
int text_attrs, attrs, line_width;
string_attrs_t & sa = al.get_attrs();
string & line = al.get_string();
2009-09-14 01:07:32 +00:00
string_attrs_t::iterator iter;
std::map<size_t, size_t, std::greater<size_t> > tab_list;
2013-05-28 04:35:00 +00:00
int tab_count = 0;
char * buffer, *expanded_line;
size_t exp_index = 0;
string full_line;
2009-09-14 01:07:32 +00:00
assert(lr.lr_end >= 0);
2013-05-28 04:35:00 +00:00
line_width = lr.length();
buffer = (char *)alloca(line_width + 1);
tab_count = count(line.begin(), line.end(), '\t');
expanded_line = (char *)alloca(line.size() + tab_count * 8 + 1);
2009-09-14 01:07:32 +00:00
for (size_t lpc = 0; lpc < line.size(); lpc++) {
switch (line[lpc]) {
case '\t':
2013-05-28 04:35:00 +00:00
do {
expanded_line[exp_index] = ' ';
exp_index += 1;
} while (exp_index % 8);
tab_list[lpc] = exp_index;
break;
case '\r':
2013-05-28 04:35:00 +00:00
/* exp_index = -1; */
break;
case '\n':
expanded_line[exp_index] = ' ';
exp_index += 1;
break;
default:
2013-05-28 04:35:00 +00:00
expanded_line[exp_index] = line[lpc];
exp_index += 1;
break;
}
2009-09-14 01:07:32 +00:00
}
expanded_line[exp_index] = '\0';
full_line = string(expanded_line);
2009-09-14 01:07:32 +00:00
text_attrs = view_colors::singleton().attrs_for_role(base_role);
2013-05-28 04:35:00 +00:00
attrs = text_attrs;
2009-09-14 01:07:32 +00:00
wmove(window, y, x);
wattron(window, attrs);
if (lr.lr_start < (int)full_line.size()) {
2013-05-28 04:35:00 +00:00
waddnstr(window, &full_line.c_str()[lr.lr_start], line_width);
}
if (lr.lr_end > (int)full_line.size()) {
whline(window, ' ', lr.lr_end - full_line.size());
2009-09-14 01:07:32 +00:00
}
wattroff(window, attrs);
std::vector<line_range> graphic_range;
2013-05-28 04:35:00 +00:00
std::vector<int> graphic_in;
2013-04-17 16:27:12 +00:00
for (iter = sa.begin(); iter != sa.end(); ++iter) {
2013-05-28 04:35:00 +00:00
struct line_range attr_range = iter->first;
std::map<size_t, size_t>::iterator tab_iter;
2013-05-28 04:35:00 +00:00
assert(attr_range.lr_start >= 0);
assert(attr_range.lr_end >= -1);
2009-09-14 01:07:32 +00:00
tab_iter = tab_list.lower_bound(attr_range.lr_start);
2013-05-28 04:35:00 +00:00
if (tab_iter != tab_list.end()) {
attr_range.lr_start += (tab_iter->second - tab_iter->first) - 1;
}
if (attr_range.lr_end != -1) {
tab_iter = tab_list.lower_bound(attr_range.lr_end);
if (tab_iter != tab_list.end()) {
attr_range.lr_end += (tab_iter->second - tab_iter->first) - 1;
}
}
attr_range.lr_start = max(0, attr_range.lr_start - lr.lr_start);
if (attr_range.lr_end == -1) {
attr_range.lr_end = line_width;
}
else {
attr_range.lr_end = min((int)line_width,
attr_range.lr_end - lr.lr_start);
}
if (attr_range.lr_end > 0) {
int awidth = attr_range.length();
attrs_map_t & am = iter->second;
attrs_map_t::iterator am_iter;
attrs = 0;
for (am_iter = am.begin(); am_iter != am.end(); ++am_iter) {
if (am_iter->first == "style") {
attrs |= am_iter->second.sa_int;
}
}
if (attrs != 0) {
/* This silliness is brought to you by a buggy old curses lib. */
mvwinnstr(window, y, x + attr_range.lr_start, buffer, awidth);
wattron(window, attrs);
mvwaddnstr(window, y, x + attr_range.lr_start, buffer, awidth);
wattroff(window, attrs);
}
for (am_iter = am.begin(); am_iter != am.end(); ++am_iter) {
if (am_iter->first == "graphic") {
graphic_range.push_back(attr_range);
graphic_in.push_back(am_iter->second.sa_int | attrs);
}
}
}
attrs = text_attrs; /* Reset attrs to regular text. */
}
for (size_t lpc = 0; lpc < graphic_range.size(); lpc++) {
for (int lpc2 = graphic_range[lpc].lr_start;
lpc2 < graphic_range[lpc].lr_end;
lpc2++) {
2013-05-28 04:35:00 +00:00
mvwaddch(window, y, lpc2, graphic_in[lpc]);
}
2009-09-14 01:07:32 +00:00
}
}
view_colors &view_colors::singleton(void)
{
static view_colors s_vc;
return s_vc;
}
view_colors::view_colors()
: vc_next_highlight(0)
{
int lpc;
/* Setup the mappings from roles to actual colors. */
this->vc_role_colors[VCR_TEXT] = COLOR_PAIR(VC_WHITE) | A_DIM;
this->vc_role_colors[VCR_SEARCH] =
2013-05-28 04:35:00 +00:00
this->vc_role_colors[VCR_TEXT] | A_REVERSE;
this->vc_role_colors[VCR_OK] = COLOR_PAIR(VC_GREEN) | A_BOLD;
2009-09-14 01:07:32 +00:00
this->vc_role_colors[VCR_ERROR] = COLOR_PAIR(VC_RED) | A_BOLD;
this->vc_role_colors[VCR_WARNING] = COLOR_PAIR(VC_YELLOW) | A_BOLD;
this->vc_role_colors[VCR_ALT_ROW] = COLOR_PAIR(VC_WHITE) | A_BOLD;
this->vc_role_colors[VCR_STATUS] =
2013-05-28 04:35:00 +00:00
COLOR_PAIR(VC_BLACK_ON_WHITE);
2009-09-14 01:07:32 +00:00
this->vc_role_colors[VCR_WARN_STATUS] =
2013-05-28 04:35:00 +00:00
COLOR_PAIR(VC_YELLOW_ON_WHITE) | A_BOLD;
2009-09-14 01:07:32 +00:00
this->vc_role_colors[VCR_ALERT_STATUS] =
2013-05-28 04:35:00 +00:00
COLOR_PAIR(VC_RED_ON_WHITE);
2009-09-14 01:07:32 +00:00
this->vc_role_colors[VCR_ACTIVE_STATUS] =
2013-05-28 04:35:00 +00:00
COLOR_PAIR(VC_GREEN_ON_WHITE);
2009-09-14 01:07:32 +00:00
this->vc_role_colors[VCR_ACTIVE_STATUS2] =
2013-05-28 04:35:00 +00:00
COLOR_PAIR(VC_GREEN_ON_WHITE) | A_BOLD;
2013-06-06 02:34:48 +00:00
this->vc_role_colors[VCR_BOLD_STATUS] =
COLOR_PAIR(VC_BLACK_ON_WHITE) | A_BOLD;
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
this->vc_role_colors[VCR_DIFF_DELETE] = COLOR_PAIR(VC_RED);
this->vc_role_colors[VCR_DIFF_ADD] = COLOR_PAIR(VC_GREEN);
this->vc_role_colors[VCR_DIFF_SECTION] = COLOR_PAIR(VC_MAGENTA);
this->vc_role_colors[VCR_SHADOW] = COLOR_PAIR(VC_GRAY);
2009-09-14 01:07:32 +00:00
for (lpc = 0; lpc < VCR__MAX; lpc++) {
2013-05-28 04:35:00 +00:00
this->vc_role_reverse_colors[lpc] =
this->vc_role_colors[lpc] | A_REVERSE;
2009-09-14 01:07:32 +00:00
}
/*
* Prime the highlight vector. The first HL_COLOR_COUNT color pairs are
* assumed to be the highlight colors.
*/
for (lpc = 1; lpc <= HL_COLOR_COUNT; lpc++) {
2013-05-28 04:35:00 +00:00
this->vc_role_colors[VCR__MAX + (lpc - 1) * 2] = COLOR_PAIR(lpc);
this->vc_role_colors[VCR__MAX + (lpc - 1) * 2 + 1] =
COLOR_PAIR(lpc) | A_BOLD;
this->vc_role_reverse_colors[VCR__MAX + (lpc - 1) * 2] =
COLOR_PAIR(lpc + HL_COLOR_COUNT) | A_REVERSE;
this->vc_role_reverse_colors[VCR__MAX + (lpc - 1) * 2 + 1] =
COLOR_PAIR(lpc) | A_BOLD | A_REVERSE;
2009-09-14 01:07:32 +00:00
}
}
void view_colors::init(void)
{
if (has_colors()) {
2013-06-08 14:57:40 +00:00
static int ansi_colors_to_curses[] = {
COLOR_BLACK,
COLOR_RED,
COLOR_GREEN,
COLOR_YELLOW,
COLOR_BLUE,
COLOR_MAGENTA,
COLOR_CYAN,
COLOR_WHITE,
};
2013-05-28 04:35:00 +00:00
start_color();
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
/* use_default_colors(); */
init_pair(VC_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(VC_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(VC_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(VC_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
2013-05-28 04:35:00 +00:00
init_pair(VC_BLUE_ON_WHITE, COLOR_BLUE, COLOR_WHITE);
init_pair(VC_CYAN_ON_BLACK, COLOR_CYAN, COLOR_BLACK);
init_pair(VC_CYAN_ON_WHITE, COLOR_CYAN, COLOR_WHITE);
2013-05-28 04:35:00 +00:00
init_pair(VC_GREEN_ON_WHITE, COLOR_GREEN, COLOR_WHITE);
init_pair(VC_MAGENTA_ON_WHITE, COLOR_MAGENTA, COLOR_WHITE);
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
init_pair(VC_RED, COLOR_RED, COLOR_BLACK);
init_pair(VC_YELLOW, COLOR_YELLOW, COLOR_BLACK);
init_pair(VC_WHITE, COLOR_WHITE, COLOR_BLACK);
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
init_pair(VC_BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
init_pair(VC_RED_ON_WHITE, COLOR_RED, COLOR_WHITE);
init_pair(VC_YELLOW_ON_WHITE, COLOR_YELLOW, COLOR_WHITE);
init_pair(VC_CYAN_ON_BLUE, COLOR_CYAN, COLOR_BLUE);
2013-05-28 04:35:00 +00:00
init_pair(VC_WHITE_ON_GREEN, COLOR_WHITE, COLOR_GREEN);
init_pair(VC_WHITE_ON_CYAN, COLOR_WHITE, COLOR_CYAN);
2013-05-28 04:35:00 +00:00
init_pair(VC_GRAY, COLOR_BLACK, COLOR_BLACK);
2013-06-08 14:57:40 +00:00
for (int fg = 0; fg < 8; fg++) {
for (int bg = 0; bg < 8; bg++) {
init_pair(ansi_color_pair_index(fg, bg),
ansi_colors_to_curses[fg],
ansi_colors_to_curses[bg]);
}
}
for (int lpc = 0; lpc < 8; lpc++) {
short gradient_value = (1000 / 8) * lpc;
init_color(lpc + 16, gradient_value, gradient_value, gradient_value);
init_pair(VC_GRADIENT_START + lpc, lpc + 16, COLOR_BLACK);
}
2009-09-14 01:07:32 +00:00
}
}
view_colors::role_t view_colors::next_highlight()
2009-09-14 01:07:32 +00:00
{
role_t retval = (role_t)(VCR__MAX + this->vc_next_highlight);
this->vc_next_highlight = (this->vc_next_highlight + 1) %
2013-05-28 04:35:00 +00:00
(HL_COLOR_COUNT * 2);
2009-09-14 01:07:32 +00:00
return retval;
}
view_colors::role_t view_colors::next_plain_highlight()
{
role_t retval = (role_t)(VCR__MAX + this->vc_next_plain_highlight);
this->vc_next_plain_highlight = (this->vc_next_plain_highlight + 2) %
2013-05-28 04:35:00 +00:00
(HL_COLOR_COUNT * 2);
return retval;
}