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-24 14:55:56 +00:00
|
|
|
*
|
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-24 14:55:56 +00:00
|
|
|
*
|
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-24 14:55:56 +00:00
|
|
|
*
|
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>
|
|
|
|
|
2016-03-14 05:22:27 +00:00
|
|
|
#include "auto_mem.hh"
|
2014-03-04 15:38:33 +00:00
|
|
|
#include "lnav_log.hh"
|
2009-09-14 01:07:32 +00:00
|
|
|
#include "view_curses.hh"
|
2016-03-14 05:22:27 +00:00
|
|
|
#include "ansi_scrubber.hh"
|
2016-05-02 03:35:37 +00:00
|
|
|
#include "lnav_config.hh"
|
2017-12-29 14:50:01 +00:00
|
|
|
#include "yajlpp.hh"
|
2018-05-25 13:32:01 +00:00
|
|
|
#include "yajlpp_def.hh"
|
2017-12-29 14:50:01 +00:00
|
|
|
#include "xterm-palette.hh"
|
2018-04-02 15:02:41 +00:00
|
|
|
#include "attr_line.hh"
|
2009-09-14 01:07:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2017-12-29 14:50:01 +00:00
|
|
|
struct xterm_color {
|
|
|
|
short xc_id;
|
|
|
|
string xc_name;
|
|
|
|
rgb_color xc_color;
|
|
|
|
lab_color xc_lab_color;
|
|
|
|
};
|
|
|
|
|
2018-05-25 13:32:01 +00:00
|
|
|
static struct json_path_handler xterm_color_rgb_handler[] = {
|
|
|
|
json_path_handler("r")
|
|
|
|
.FOR_FIELD(rgb_color, rc_r),
|
|
|
|
json_path_handler("g")
|
|
|
|
.FOR_FIELD(rgb_color, rc_g),
|
|
|
|
json_path_handler("b")
|
|
|
|
.FOR_FIELD(rgb_color, rc_b),
|
|
|
|
|
|
|
|
json_path_handler()
|
|
|
|
};
|
|
|
|
|
2017-12-29 14:50:01 +00:00
|
|
|
static struct json_path_handler xterm_color_handler[] = {
|
|
|
|
json_path_handler("colorId")
|
2018-05-25 13:32:01 +00:00
|
|
|
.FOR_FIELD(xterm_color, xc_id),
|
2017-12-29 14:50:01 +00:00
|
|
|
json_path_handler("name")
|
2018-05-25 13:32:01 +00:00
|
|
|
.FOR_FIELD(xterm_color, xc_name),
|
|
|
|
json_path_handler("rgb/")
|
|
|
|
.with_obj_provider<rgb_color, xterm_color>([](const auto &pc, xterm_color *xc) { return &xc->xc_color; })
|
|
|
|
.with_children(xterm_color_rgb_handler),
|
2017-12-29 14:50:01 +00:00
|
|
|
|
|
|
|
json_path_handler()
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct json_path_handler root_color_handler[] = {
|
|
|
|
json_path_handler("#/")
|
|
|
|
.with_obj_provider<xterm_color, vector<xterm_color>>(
|
|
|
|
[](const yajlpp_provider_context &ypc, vector<xterm_color> *palette) {
|
|
|
|
palette->resize(ypc.ypc_index + 1);
|
|
|
|
return &((*palette)[ypc.ypc_index]);
|
|
|
|
})
|
|
|
|
.with_children(xterm_color_handler),
|
|
|
|
|
|
|
|
json_path_handler()
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct _xterm_colors {
|
|
|
|
_xterm_colors() {
|
|
|
|
yajlpp_parse_context ypc_xterm("xterm-palette.json", root_color_handler);
|
|
|
|
yajl_handle handle;
|
|
|
|
|
2018-05-17 14:06:50 +00:00
|
|
|
handle = yajl_alloc(&ypc_xterm.ypc_callbacks, nullptr, &ypc_xterm);
|
2017-12-29 14:50:01 +00:00
|
|
|
ypc_xterm
|
|
|
|
.with_ignore_unused(true)
|
|
|
|
.with_obj(this->xc_palette)
|
|
|
|
.with_handle(handle);
|
|
|
|
ypc_xterm.parse((const unsigned char *) xterm_palette_json,
|
|
|
|
strlen(xterm_palette_json));
|
|
|
|
ypc_xterm.complete_parse();
|
|
|
|
yajl_free(handle);
|
|
|
|
|
|
|
|
for (auto &xc : this->xc_palette) {
|
|
|
|
xc.xc_lab_color = lab_color(xc.xc_color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
short match_color(const lab_color &to_match) {
|
|
|
|
double lowest = 1000.0;
|
|
|
|
short lowest_id = -1;
|
|
|
|
|
|
|
|
for (auto &xc : this->xc_palette) {
|
|
|
|
double xc_delta = xc.xc_lab_color.deltaE(to_match);
|
|
|
|
|
|
|
|
if (lowest_id == -1) {
|
|
|
|
lowest = xc_delta;
|
|
|
|
lowest_id = xc.xc_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xc_delta < lowest) {
|
|
|
|
lowest = xc_delta;
|
|
|
|
lowest_id = xc.xc_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lowest_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<xterm_color> xc_palette;
|
|
|
|
} xterm_colors;
|
|
|
|
|
|
|
|
bool rgb_color::from_str(const string_fragment &color,
|
|
|
|
rgb_color &rgb_out,
|
|
|
|
std::string &errmsg)
|
|
|
|
{
|
|
|
|
if (color[0] == '#') {
|
|
|
|
switch (color.length()) {
|
|
|
|
case 4:
|
|
|
|
if (sscanf(color.data(), "#%1hx%1hx%1hx",
|
|
|
|
&rgb_out.rc_r, &rgb_out.rc_g, &rgb_out.rc_b) == 3) {
|
|
|
|
rgb_out.rc_r |= rgb_out.rc_r << 4;
|
|
|
|
rgb_out.rc_g |= rgb_out.rc_g << 4;
|
|
|
|
rgb_out.rc_b |= rgb_out.rc_b << 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (sscanf(color.data(), "#%2hx%2hx%2hx",
|
|
|
|
&rgb_out.rc_r, &rgb_out.rc_g, &rgb_out.rc_b) == 3) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
errmsg = "Could not parse color: " + color.to_string();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &xc : xterm_colors.xc_palette) {
|
|
|
|
if (color == xc.xc_name) {
|
|
|
|
rgb_out = xc.xc_color;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:48:16 +00:00
|
|
|
errmsg = "Unknown color: " + color.to_string() +
|
|
|
|
". See https://jonasjacek.github.io/colors/ for a list of supported color names";
|
2017-12-29 14:50:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-31 14:01:11 +00:00
|
|
|
string_attr_type view_curses::VC_STYLE("style");
|
|
|
|
string_attr_type view_curses::VC_GRAPHIC("graphic");
|
2014-01-25 17:29:35 +00:00
|
|
|
|
2014-01-25 20:13:41 +00:00
|
|
|
const struct itimerval ui_periodic_timer::INTERVAL = {
|
|
|
|
{ 0, 350 * 1000 },
|
|
|
|
{ 0, 350 * 1000 }
|
|
|
|
};
|
|
|
|
|
|
|
|
ui_periodic_timer::ui_periodic_timer()
|
2015-09-20 04:13:46 +00:00
|
|
|
: upt_counter(0)
|
2014-01-25 20:13:41 +00:00
|
|
|
{
|
2015-03-23 06:10:52 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sa.sa_handler = ui_periodic_timer::sigalrm;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
2015-03-24 04:25:19 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2018-05-17 14:06:50 +00:00
|
|
|
sigaction(SIGALRM, &sa, nullptr);
|
|
|
|
if (setitimer(ITIMER_REAL, &INTERVAL, nullptr) == -1) {
|
2014-01-25 20:13:41 +00:00
|
|
|
perror("setitimer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_periodic_timer &ui_periodic_timer::singleton()
|
|
|
|
{
|
|
|
|
static ui_periodic_timer retval;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ui_periodic_timer::sigalrm(int sig)
|
|
|
|
{
|
|
|
|
singleton().upt_counter += 1;
|
|
|
|
}
|
|
|
|
|
2013-07-27 19:07:05 +00:00
|
|
|
alerter &alerter::singleton() {
|
|
|
|
static alerter retval;
|
|
|
|
|
|
|
|
return retval;
|
2013-07-31 04:21:28 +00:00
|
|
|
}
|
2013-07-27 19:07:05 +00:00
|
|
|
|
2016-03-14 05:22:27 +00:00
|
|
|
attr_line_t &attr_line_t::with_ansi_string(const char *str, ...)
|
|
|
|
{
|
|
|
|
auto_mem<char> formatted_str;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, str);
|
2018-03-28 02:07:13 +00:00
|
|
|
auto ret = vasprintf(formatted_str.out(), str, args);
|
2016-03-14 05:22:27 +00:00
|
|
|
va_end(args);
|
|
|
|
|
2018-05-17 14:06:50 +00:00
|
|
|
if (ret >= 0 && formatted_str != nullptr) {
|
2016-03-14 05:22:27 +00:00
|
|
|
this->al_string = formatted_str;
|
|
|
|
scrub_ansi_string(this->al_string, this->al_attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-04-15 05:49:36 +00:00
|
|
|
attr_line_t &attr_line_t::with_ansi_string(const std::string &str)
|
|
|
|
{
|
|
|
|
this->al_string = str;
|
|
|
|
scrub_ansi_string(this->al_string, this->al_attrs);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
attr_line_t &attr_line_t::insert(size_t index, const attr_line_t &al, text_wrap_settings *tws)
|
2017-03-26 13:02:53 +00:00
|
|
|
{
|
2018-05-10 13:44:03 +00:00
|
|
|
if (index < this->al_string.length()) {
|
|
|
|
shift_string_attrs(this->al_attrs, index, al.al_string.length());
|
|
|
|
}
|
2017-03-26 13:02:53 +00:00
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
this->al_string.insert(index, al.al_string);
|
2017-03-26 13:02:53 +00:00
|
|
|
|
|
|
|
for (auto &sa : al.al_attrs) {
|
|
|
|
this->al_attrs.emplace_back(sa);
|
|
|
|
|
2017-03-31 14:01:11 +00:00
|
|
|
line_range &lr = this->al_attrs.back().sa_range;
|
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
lr.shift(0, index);
|
2017-03-31 14:01:11 +00:00
|
|
|
if (lr.lr_end == -1) {
|
2018-05-10 13:44:03 +00:00
|
|
|
lr.lr_end = index + al.al_string.length();
|
2017-03-31 14:01:11 +00:00
|
|
|
}
|
2017-03-26 13:02:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:54:50 +00:00
|
|
|
if (tws != nullptr && (int)this->al_string.length() > tws->tws_width) {
|
2018-05-10 13:44:03 +00:00
|
|
|
ssize_t start_pos = index;
|
2017-03-26 13:02:53 +00:00
|
|
|
ssize_t line_start = this->al_string.rfind('\n', start_pos);
|
|
|
|
|
2018-03-28 01:54:50 +00:00
|
|
|
if (line_start == (ssize_t)string::npos) {
|
2017-03-26 13:02:53 +00:00
|
|
|
line_start = 0;
|
|
|
|
} else {
|
|
|
|
line_start += 1;
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
ssize_t line_len = index - line_start;
|
2017-03-26 13:02:53 +00:00
|
|
|
ssize_t usable_width = tws->tws_width - tws->tws_indent;
|
|
|
|
ssize_t avail = max((ssize_t) 0, (ssize_t) tws->tws_width - line_len);
|
|
|
|
|
2017-04-05 14:05:19 +00:00
|
|
|
if (avail == 0) {
|
|
|
|
avail = INT_MAX;
|
|
|
|
}
|
|
|
|
|
2018-03-28 01:54:50 +00:00
|
|
|
while (start_pos < (int)this->al_string.length()) {
|
2017-03-26 13:02:53 +00:00
|
|
|
ssize_t lpc;
|
|
|
|
|
2017-03-31 14:01:11 +00:00
|
|
|
// Find the end of a word or a breakpoint.
|
2017-03-26 13:02:53 +00:00
|
|
|
for (lpc = start_pos;
|
2018-03-28 01:54:50 +00:00
|
|
|
lpc < (int)this->al_string.length() &&
|
2017-03-26 13:02:53 +00:00
|
|
|
(isalnum(this->al_string[lpc]) ||
|
|
|
|
this->al_string[lpc] == ',' ||
|
2017-03-31 14:01:11 +00:00
|
|
|
this->al_string[lpc] == '_' ||
|
|
|
|
this->al_string[lpc] == '.' ||
|
2017-03-26 13:02:53 +00:00
|
|
|
this->al_string[lpc] == ';');
|
|
|
|
lpc++) {
|
2017-03-31 14:01:11 +00:00
|
|
|
if (this->al_string[lpc] == '-' ||
|
|
|
|
this->al_string[lpc] == '.') {
|
2017-03-26 13:02:53 +00:00
|
|
|
lpc += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:05:19 +00:00
|
|
|
if ((avail != usable_width) && (lpc - start_pos > avail)) {
|
2017-03-31 14:01:11 +00:00
|
|
|
// Need to wrap the word. Do the wrap.
|
2017-03-26 13:02:53 +00:00
|
|
|
this->insert(start_pos, 1, '\n')
|
|
|
|
.insert(start_pos + 1, tws->tws_indent, ' ');
|
|
|
|
start_pos += 1 + tws->tws_indent;
|
|
|
|
avail = tws->tws_width - tws->tws_indent;
|
|
|
|
} else {
|
2017-03-31 14:01:11 +00:00
|
|
|
// There's still room to add stuff.
|
2017-03-26 13:02:53 +00:00
|
|
|
avail -= (lpc - start_pos);
|
2018-03-28 01:54:50 +00:00
|
|
|
while (lpc < (int)this->al_string.length() && avail) {
|
2017-03-31 14:01:11 +00:00
|
|
|
if (this->al_string[lpc] == '\n') {
|
|
|
|
this->insert(lpc + 1, tws->tws_indent, ' ');
|
|
|
|
avail = usable_width;
|
|
|
|
lpc += 1 + tws->tws_indent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isalnum(this->al_string[lpc]) ||
|
|
|
|
this->al_string[lpc] == '_') {
|
2017-03-26 13:02:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
avail -= 1;
|
|
|
|
lpc += 1;
|
|
|
|
}
|
|
|
|
start_pos = lpc;
|
|
|
|
if (!avail) {
|
|
|
|
this->insert(start_pos, 1, '\n')
|
|
|
|
.insert(start_pos + 1, tws->tws_indent, ' ');
|
|
|
|
start_pos += 1 + tws->tws_indent;
|
|
|
|
avail = usable_width;
|
2017-03-31 14:01:11 +00:00
|
|
|
|
|
|
|
for (lpc = start_pos;
|
2018-03-28 01:54:50 +00:00
|
|
|
lpc < (int)this->al_string.length() &&
|
2017-03-31 14:01:11 +00:00
|
|
|
this->al_string[lpc] == ' ';
|
|
|
|
lpc++) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpc != start_pos) {
|
|
|
|
this->erase(start_pos, (lpc - start_pos));
|
|
|
|
}
|
2017-03-26 13:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
attr_line_t attr_line_t::subline(size_t start, size_t len) const
|
|
|
|
{
|
2018-03-28 14:25:10 +00:00
|
|
|
if (len == std::string::npos) {
|
2018-05-10 13:44:03 +00:00
|
|
|
len = this->al_string.length() - start;
|
2018-03-28 14:25:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 13:02:53 +00:00
|
|
|
line_range lr{(int) start, (int) (start + len)};
|
|
|
|
attr_line_t retval;
|
|
|
|
|
|
|
|
retval.al_string = this->al_string.substr(start, len);
|
|
|
|
for (auto &sa : this->al_attrs) {
|
|
|
|
if (!lr.intersects(sa.sa_range)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval.al_attrs.emplace_back(lr.intersection(sa.sa_range)
|
|
|
|
.shift(lr.lr_start, -lr.lr_start),
|
2018-05-10 13:44:03 +00:00
|
|
|
sa.sa_type, sa.sa_value);
|
|
|
|
|
|
|
|
line_range &last_lr = retval.al_attrs.back().sa_range;
|
|
|
|
|
|
|
|
ensure(last_lr.lr_end <= retval.al_string.length());
|
2017-03-26 13:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void attr_line_t::split_lines(std::vector<attr_line_t> &lines) const
|
|
|
|
{
|
|
|
|
size_t pos = 0, next_line;
|
|
|
|
|
|
|
|
while ((next_line = this->al_string.find('\n', pos)) != std::string::npos) {
|
|
|
|
lines.emplace_back(this->subline(pos, next_line - pos));
|
|
|
|
pos = next_line + 1;
|
|
|
|
}
|
|
|
|
lines.emplace_back(this->subline(pos));
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:41:09 +00:00
|
|
|
struct utf_to_display_adjustment {
|
|
|
|
int uda_origin;
|
|
|
|
int uda_offset;
|
2017-04-23 14:11:21 +00:00
|
|
|
|
2018-04-04 14:41:09 +00:00
|
|
|
utf_to_display_adjustment(int utf_origin, int offset)
|
|
|
|
: uda_origin(utf_origin), uda_offset(offset) {
|
2017-04-23 14:11:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
void view_curses::mvwattrline(WINDOW *window,
|
2013-05-28 04:35:00 +00:00
|
|
|
int y,
|
|
|
|
int x,
|
|
|
|
attr_line_t &al,
|
2014-03-04 15:38:33 +00:00
|
|
|
const struct line_range &lr,
|
2013-05-28 04:35:00 +00:00
|
|
|
view_colors::role_t base_role)
|
2009-09-14 01:07:32 +00:00
|
|
|
{
|
2018-05-17 14:06:50 +00:00
|
|
|
attr_t text_attrs, attrs;
|
|
|
|
int line_width;
|
2013-05-28 04:35:00 +00:00
|
|
|
string_attrs_t & sa = al.get_attrs();
|
|
|
|
string & line = al.get_string();
|
2017-04-23 14:11:21 +00:00
|
|
|
string_attrs_t::const_iterator iter;
|
2018-04-04 14:41:09 +00:00
|
|
|
vector<utf_to_display_adjustment> utf_adjustments;
|
|
|
|
int tab_count = 0;
|
|
|
|
char *expanded_line;
|
|
|
|
int exp_index = 0;
|
|
|
|
int exp_offset = 0;
|
2013-04-20 20:21:10 +00:00
|
|
|
string full_line;
|
2009-09-14 01:07:32 +00:00
|
|
|
|
2014-03-06 14:58:49 +00:00
|
|
|
require(lr.lr_end >= 0);
|
2012-06-05 20:18:59 +00:00
|
|
|
|
2013-05-28 04:35:00 +00:00
|
|
|
line_width = lr.length();
|
|
|
|
tab_count = count(line.begin(), line.end(), '\t');
|
2013-04-20 20:21:10 +00:00
|
|
|
expanded_line = (char *)alloca(line.size() + tab_count * 8 + 1);
|
2009-09-14 01:07:32 +00:00
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
for (size_t lpc = 0; lpc < line.size(); lpc++) {
|
2017-04-23 14:11:21 +00:00
|
|
|
int exp_start_index = exp_index;
|
2018-04-04 14:41:09 +00:00
|
|
|
unsigned char ch = static_cast<unsigned char>(line[lpc]);
|
2017-04-23 14:11:21 +00:00
|
|
|
|
2018-04-04 14:41:09 +00:00
|
|
|
switch (ch) {
|
2013-04-20 20:21:10 +00:00
|
|
|
case '\t':
|
2013-05-28 04:35:00 +00:00
|
|
|
do {
|
|
|
|
expanded_line[exp_index] = ' ';
|
|
|
|
exp_index += 1;
|
|
|
|
} while (exp_index % 8);
|
2018-04-04 14:41:09 +00:00
|
|
|
utf_adjustments.emplace_back(lpc, exp_index - exp_start_index - 1);
|
2013-05-28 04:35:00 +00:00
|
|
|
break;
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
case '\r':
|
2013-05-28 04:35:00 +00:00
|
|
|
/* exp_index = -1; */
|
|
|
|
break;
|
|
|
|
|
2013-06-01 03:45:40 +00:00
|
|
|
case '\n':
|
|
|
|
expanded_line[exp_index] = ' ';
|
|
|
|
exp_index += 1;
|
|
|
|
break;
|
|
|
|
|
2018-04-04 14:41:09 +00:00
|
|
|
default: {
|
|
|
|
int offset = 0;
|
|
|
|
|
2013-05-28 04:35:00 +00:00
|
|
|
expanded_line[exp_index] = line[lpc];
|
|
|
|
exp_index += 1;
|
2018-04-04 14:41:09 +00:00
|
|
|
if ((ch & 0xf8) == 0xf0) {
|
|
|
|
offset = -3;
|
|
|
|
} else if ((ch & 0xf0) == 0xe0) {
|
|
|
|
offset = -2;
|
|
|
|
} else if ((ch & 0xe0) == 0xc0) {
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset) {
|
|
|
|
exp_offset += offset;
|
|
|
|
utf_adjustments.emplace_back(lpc, offset);
|
|
|
|
for (; offset && (lpc + 1) < line.size(); lpc++, offset++) {
|
|
|
|
expanded_line[exp_index] = line[lpc + 1];
|
|
|
|
exp_index += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 04:35:00 +00:00
|
|
|
break;
|
2013-04-20 20:21:10 +00:00
|
|
|
}
|
2018-04-04 14:41:09 +00:00
|
|
|
}
|
2009-09-14 01:07:32 +00:00
|
|
|
}
|
2013-04-20 20:21:10 +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);
|
2013-04-20 20:21:10 +00:00
|
|
|
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()) {
|
2018-04-04 14:41:09 +00:00
|
|
|
whline(window, ' ', lr.lr_end - (full_line.size() + exp_offset));
|
2009-09-14 01:07:32 +00:00
|
|
|
}
|
|
|
|
wattroff(window, attrs);
|
|
|
|
|
2014-01-25 17:29:35 +00:00
|
|
|
stable_sort(sa.begin(), sa.end());
|
2013-04-17 16:27:12 +00:00
|
|
|
for (iter = sa.begin(); iter != sa.end(); ++iter) {
|
2014-01-25 17:29:35 +00:00
|
|
|
struct line_range attr_range = iter->sa_range;
|
2013-05-24 14:55:56 +00:00
|
|
|
|
2014-03-06 14:58:49 +00:00
|
|
|
require(attr_range.lr_start >= 0);
|
|
|
|
require(attr_range.lr_end >= -1);
|
2009-09-14 01:07:32 +00:00
|
|
|
|
2018-04-02 15:02:41 +00:00
|
|
|
if (!(iter->sa_type == &VC_STYLE || iter->sa_type == &VC_GRAPHIC)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:41:09 +00:00
|
|
|
for (const auto &adj : utf_adjustments) {
|
2018-04-04 14:57:13 +00:00
|
|
|
if (adj.uda_origin < iter->sa_range.lr_start) {
|
2018-04-04 14:41:09 +00:00
|
|
|
attr_range.lr_start += adj.uda_offset;
|
2014-03-08 07:42:43 +00:00
|
|
|
}
|
2013-05-28 04:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attr_range.lr_end != -1) {
|
2018-04-04 14:41:09 +00:00
|
|
|
for (const auto &adj : utf_adjustments) {
|
2018-04-04 14:57:13 +00:00
|
|
|
if (adj.uda_origin < iter->sa_range.lr_end) {
|
2018-04-04 14:41:09 +00:00
|
|
|
attr_range.lr_end += adj.uda_offset;
|
2014-03-08 07:42:43 +00:00
|
|
|
}
|
2013-05-28 04:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attr_range.lr_start = max(0, attr_range.lr_start - lr.lr_start);
|
|
|
|
if (attr_range.lr_end == -1) {
|
2014-03-08 07:31:12 +00:00
|
|
|
attr_range.lr_end = lr.lr_start + line_width;
|
2013-05-28 04:35:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 14:36:09 +00:00
|
|
|
attr_range.lr_end = min(line_width, attr_range.lr_end - lr.lr_start);
|
|
|
|
|
|
|
|
if (iter->sa_type == &VC_GRAPHIC) {
|
2018-04-03 14:57:04 +00:00
|
|
|
for (int index = attr_range.lr_start;
|
|
|
|
index < attr_range.lr_end;
|
|
|
|
index++) {
|
|
|
|
mvwaddch(window, y, x + index, iter->sa_value.sav_int);
|
|
|
|
}
|
2018-04-03 14:36:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 07:31:12 +00:00
|
|
|
|
|
|
|
if (attr_range.lr_end > attr_range.lr_start) {
|
2013-05-28 04:35:00 +00:00
|
|
|
int awidth = attr_range.length();
|
2018-04-02 15:02:41 +00:00
|
|
|
int color_pair;
|
2013-05-28 04:35:00 +00:00
|
|
|
|
2018-04-02 15:02:41 +00:00
|
|
|
attrs = iter->sa_value.sav_int & ~A_COLOR;
|
|
|
|
color_pair = PAIR_NUMBER(iter->sa_value.sav_int);
|
2013-05-24 14:55:56 +00:00
|
|
|
|
2018-04-02 15:02:41 +00:00
|
|
|
if (attrs || color_pair > 0) {
|
2014-03-08 07:31:12 +00:00
|
|
|
int x_pos = x + attr_range.lr_start;
|
|
|
|
int ch_width = min(awidth, (line_width - attr_range.lr_start));
|
2017-08-01 17:17:30 +00:00
|
|
|
cchar_t row_ch[ch_width + 1];
|
2014-03-08 07:31:12 +00:00
|
|
|
|
2017-08-01 17:17:30 +00:00
|
|
|
mvwin_wchnstr(window, y, x_pos, row_ch, ch_width);
|
2014-03-08 07:31:12 +00:00
|
|
|
for (int lpc = 0; lpc < ch_width; lpc++) {
|
|
|
|
if (color_pair > 0) {
|
2018-04-02 15:02:41 +00:00
|
|
|
row_ch[lpc].attr =
|
|
|
|
attrs | (row_ch[lpc].attr & ~A_COLOR);
|
2017-08-01 17:28:27 +00:00
|
|
|
#ifdef NCURSES_EXT_COLORS
|
2017-08-01 17:17:30 +00:00
|
|
|
row_ch[lpc].ext_color = color_pair;
|
2017-08-01 17:28:27 +00:00
|
|
|
#else
|
|
|
|
row_ch[lpc].attr |= COLOR_PAIR(color_pair);
|
|
|
|
#endif
|
2014-03-08 07:31:12 +00:00
|
|
|
} else {
|
2018-04-02 15:02:41 +00:00
|
|
|
row_ch[lpc].attr |= attrs;
|
2014-03-08 07:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 17:17:30 +00:00
|
|
|
mvwadd_wchnstr(window, y, x_pos, row_ch, ch_width);
|
2013-05-24 14:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-14 01:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 03:35:37 +00:00
|
|
|
class color_listener : public lnav_config_listener {
|
|
|
|
void reload_config() {
|
2017-12-03 06:11:34 +00:00
|
|
|
view_colors::singleton().init_roles(0);
|
2016-05-02 03:35:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static color_listener _COLOR_LISTENER;
|
|
|
|
|
2018-05-17 14:06:50 +00:00
|
|
|
attr_t view_colors::BASIC_HL_PAIRS[view_colors::BASIC_COLOR_COUNT] = {
|
2016-10-25 13:39:36 +00:00
|
|
|
ansi_color_pair(COLOR_BLUE, COLOR_BLACK),
|
|
|
|
ansi_color_pair(COLOR_CYAN, COLOR_BLACK),
|
|
|
|
ansi_color_pair(COLOR_GREEN, COLOR_BLACK),
|
|
|
|
ansi_color_pair(COLOR_MAGENTA, COLOR_BLACK),
|
|
|
|
ansi_color_pair(COLOR_BLUE, COLOR_WHITE),
|
|
|
|
ansi_color_pair(COLOR_CYAN, COLOR_BLACK),
|
|
|
|
ansi_color_pair(COLOR_GREEN, COLOR_WHITE),
|
|
|
|
ansi_color_pair(COLOR_MAGENTA, COLOR_WHITE),
|
|
|
|
};
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
view_colors &view_colors::singleton(void)
|
|
|
|
{
|
|
|
|
static view_colors s_vc;
|
|
|
|
|
|
|
|
return s_vc;
|
|
|
|
}
|
|
|
|
|
2017-12-29 14:50:01 +00:00
|
|
|
view_colors::view_colors() : vc_color_pair_end(0)
|
2009-09-14 01:07:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-25 13:39:36 +00:00
|
|
|
bool view_colors::initialized = false;
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
void view_colors::init(void)
|
|
|
|
{
|
2017-12-03 06:11:34 +00:00
|
|
|
int color_pair_base = VC_ANSI_END;
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
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
|
|
|
|
2017-12-03 06:11:34 +00:00
|
|
|
if (lnav_config.lc_ui_default_colors) {
|
|
|
|
use_default_colors();
|
|
|
|
}
|
2013-06-08 14:57:40 +00:00
|
|
|
for (int fg = 0; fg < 8; fg++) {
|
|
|
|
for (int bg = 0; bg < 8; bg++) {
|
2013-06-29 13:22:24 +00:00
|
|
|
if (fg == 0 && bg == 0)
|
|
|
|
continue;
|
2017-12-03 06:11:34 +00:00
|
|
|
init_pair(ansi_color_pair_index(fg, bg),
|
|
|
|
ansi_colors_to_curses[fg],
|
|
|
|
ansi_colors_to_curses[bg]);
|
2013-06-08 14:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 13:22:24 +00:00
|
|
|
if (COLORS == 256) {
|
2017-09-15 01:52:29 +00:00
|
|
|
int bg = (lnav_config.lc_ui_default_colors ? -1 : COLOR_BLACK);
|
2013-06-29 13:22:24 +00:00
|
|
|
|
|
|
|
for (int z = 0; z < 6; z++) {
|
|
|
|
for (int x = 1; x < 6; x += 2) {
|
2013-06-29 18:00:34 +00:00
|
|
|
for (int y = 1; y < 6; y += 2) {
|
2013-06-29 13:22:24 +00:00
|
|
|
int fg = 16 + x + (y * 6) + (z * 6 * 6);
|
|
|
|
|
2017-09-15 01:52:29 +00:00
|
|
|
init_pair(color_pair_base, fg, bg);
|
2013-06-29 13:22:24 +00:00
|
|
|
color_pair_base += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:11:34 +00:00
|
|
|
singleton().init_roles(color_pair_base);
|
2016-10-25 13:39:36 +00:00
|
|
|
|
|
|
|
initialized = true;
|
2013-06-29 13:22:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 14:06:50 +00:00
|
|
|
inline attr_t attr_for_colors(int &pair_base, short fg, short bg)
|
2017-12-03 06:11:34 +00:00
|
|
|
{
|
|
|
|
int pair = ++pair_base;
|
|
|
|
|
|
|
|
if (lnav_config.lc_ui_default_colors) {
|
|
|
|
if (fg == COLOR_WHITE) {
|
|
|
|
fg = -1;
|
|
|
|
}
|
|
|
|
if (bg == COLOR_BLACK) {
|
|
|
|
bg = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_pair(pair, fg, bg);
|
|
|
|
|
|
|
|
return COLOR_PAIR(pair);
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_colors::init_roles(int color_pair_base)
|
2013-06-29 13:22:24 +00:00
|
|
|
{
|
|
|
|
/* Setup the mappings from roles to actual colors. */
|
2017-12-03 06:11:34 +00:00
|
|
|
this->vc_role_colors[VCR_TEXT] =
|
|
|
|
attr_for_colors(color_pair_base, COLOR_WHITE, COLOR_BLACK);
|
2016-05-02 03:35:37 +00:00
|
|
|
if (lnav_config.lc_ui_dim_text) {
|
2016-05-02 03:37:45 +00:00
|
|
|
this->vc_role_colors[VCR_TEXT] |= A_DIM;
|
2016-05-02 03:35:37 +00:00
|
|
|
}
|
2018-04-02 15:02:41 +00:00
|
|
|
this->vc_role_colors[VCR_SEARCH] = A_REVERSE;
|
2017-12-03 06:11:34 +00:00
|
|
|
this->vc_role_colors[VCR_OK] = attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_BLACK) | A_BOLD;
|
|
|
|
this->vc_role_colors[VCR_ERROR] = attr_for_colors(color_pair_base, COLOR_RED, COLOR_BLACK) | A_BOLD;
|
|
|
|
this->vc_role_colors[VCR_WARNING] = attr_for_colors(color_pair_base, COLOR_YELLOW, COLOR_BLACK) | A_BOLD;
|
|
|
|
this->vc_role_colors[VCR_ALT_ROW] = this->vc_role_colors[VCR_TEXT] | A_BOLD;
|
|
|
|
this->vc_role_colors[VCR_HIDDEN] = attr_for_colors(color_pair_base, COLOR_YELLOW, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_ADJUSTED_TIME] = attr_for_colors(color_pair_base, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_SKEWED_TIME] = attr_for_colors(color_pair_base, COLOR_YELLOW, COLOR_BLACK) | A_UNDERLINE;
|
|
|
|
this->vc_role_colors[VCR_OFFSET_TIME] = attr_for_colors(color_pair_base, COLOR_CYAN, COLOR_BLACK);
|
2013-06-29 13:22:24 +00:00
|
|
|
|
|
|
|
this->vc_role_colors[VCR_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_BLACK, COLOR_WHITE);
|
2013-06-29 13:22:24 +00:00
|
|
|
this->vc_role_colors[VCR_WARN_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_YELLOW, COLOR_WHITE) | A_BOLD;
|
2013-06-29 13:22:24 +00:00
|
|
|
this->vc_role_colors[VCR_ALERT_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_RED, COLOR_WHITE) | A_BOLD;
|
2013-06-29 13:22:24 +00:00
|
|
|
this->vc_role_colors[VCR_ACTIVE_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_WHITE);
|
2013-06-29 13:22:24 +00:00
|
|
|
this->vc_role_colors[VCR_ACTIVE_STATUS2] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_WHITE) | A_BOLD;
|
2013-06-29 13:22:24 +00:00
|
|
|
this->vc_role_colors[VCR_BOLD_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_BLACK, COLOR_WHITE) | A_BOLD;
|
2016-03-20 22:15:50 +00:00
|
|
|
this->vc_role_colors[VCR_VIEW_STATUS] =
|
2017-12-03 06:11:34 +00:00
|
|
|
attr_for_colors(color_pair_base, COLOR_WHITE, COLOR_BLUE) | A_BOLD;
|
|
|
|
|
|
|
|
this->vc_role_colors[VCR_KEYWORD] = attr_for_colors(color_pair_base, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_STRING] = attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_BLACK) | A_BOLD;
|
|
|
|
this->vc_role_colors[VCR_COMMENT] = attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_VARIABLE] = attr_for_colors(color_pair_base, COLOR_CYAN, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_SYMBOL] = attr_for_colors(color_pair_base, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_RE_SPECIAL] = attr_for_colors(color_pair_base, COLOR_CYAN, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_RE_REPEAT] = attr_for_colors(color_pair_base, COLOR_YELLOW, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_FILE] = attr_for_colors(color_pair_base, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
|
|
|
|
this->vc_role_colors[VCR_DIFF_DELETE] = attr_for_colors(color_pair_base, COLOR_RED, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_DIFF_ADD] = attr_for_colors(color_pair_base, COLOR_GREEN, COLOR_BLACK);
|
|
|
|
this->vc_role_colors[VCR_DIFF_SECTION] = attr_for_colors(color_pair_base, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
|
|
|
|
this->vc_role_colors[VCR_LOW_THRESHOLD] = attr_for_colors(color_pair_base, COLOR_BLACK, COLOR_GREEN);
|
|
|
|
this->vc_role_colors[VCR_MED_THRESHOLD] = attr_for_colors(color_pair_base, COLOR_BLACK, COLOR_YELLOW);
|
|
|
|
this->vc_role_colors[VCR_HIGH_THRESHOLD] = attr_for_colors(color_pair_base, COLOR_BLACK, COLOR_RED);
|
2017-12-29 14:50:01 +00:00
|
|
|
|
|
|
|
this->vc_color_pair_end = color_pair_base + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int view_colors::ensure_color_pair(const rgb_color &rgb_fg, const rgb_color &rgb_bg)
|
|
|
|
{
|
|
|
|
return attr_for_colors(
|
|
|
|
this->vc_color_pair_end,
|
|
|
|
xterm_colors.match_color(rgb_fg),
|
|
|
|
rgb_bg.empty() ? (short) COLOR_BLACK : xterm_colors.match_color(rgb_bg));
|
2013-05-24 14:55:56 +00:00
|
|
|
}
|