[pretty-print] pay attention to hidden fields

Fixes #653
This commit is contained in:
Timothy Stack 2020-12-06 23:52:09 -08:00
parent a0986bf271
commit 836fc83203
9 changed files with 279 additions and 223 deletions

View File

@ -151,10 +151,6 @@ FILES = [
]
while True:
for fname, gen in FILES:
for i in range(random.randrange(0, 4)):
with open(fname, "a+") as fp:
fp.write(gen.next())
time.sleep(random.uniform(0.00, 0.01))
if random.uniform(0.0, 1.0) < 0.001:
os.remove(fname)
sys.stdout.write(FILES[1][1].next())
sys.stdout.flush()
time.sleep(random.uniform(1.0, 2.0))

View File

@ -232,6 +232,7 @@ add_library(diag STATIC
all_logs_vtab.cc
ansi_scrubber.cc
archive_manager.cc
attr_line.cc
bin2c.h
bookmarks.cc
bottom_status_source.cc

View File

@ -397,6 +397,7 @@ libdiag_a_SOURCES = \
all_logs_vtab.cc \
ansi_scrubber.cc \
archive_manager.cc \
attr_line.cc \
bookmarks.cc \
bottom_status_source.cc \
collation-functions.cc \

263
src/attr_line.cc Normal file
View File

@ -0,0 +1,263 @@
/**
* Copyright (c) 2020, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * 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.
*
* 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.
*/
#include "config.h"
#include "auto_mem.hh"
#include "ansi_scrubber.hh"
#include "view_curses.hh"
#include "attr_line.hh"
attr_line_t &attr_line_t::with_ansi_string(const char *str, ...)
{
auto_mem<char> formatted_str;
va_list args;
va_start(args, str);
auto ret = vasprintf(formatted_str.out(), str, args);
va_end(args);
if (ret >= 0 && formatted_str != nullptr) {
this->al_string = formatted_str;
scrub_ansi_string(this->al_string, this->al_attrs);
}
return *this;
}
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;
}
attr_line_t &attr_line_t::insert(size_t index, const attr_line_t &al, text_wrap_settings *tws)
{
if (index < this->al_string.length()) {
shift_string_attrs(this->al_attrs, index, al.al_string.length());
}
this->al_string.insert(index, al.al_string);
for (auto &sa : al.al_attrs) {
this->al_attrs.emplace_back(sa);
line_range &lr = this->al_attrs.back().sa_range;
lr.shift(0, index);
if (lr.lr_end == -1) {
lr.lr_end = index + al.al_string.length();
}
}
if (tws != nullptr && (int)this->al_string.length() > tws->tws_width) {
ssize_t start_pos = index;
ssize_t line_start = this->al_string.rfind('\n', start_pos);
if (line_start == (ssize_t) std::string::npos) {
line_start = 0;
} else {
line_start += 1;
}
ssize_t line_len = index - line_start;
ssize_t usable_width = tws->tws_width - tws->tws_indent;
ssize_t avail = std::max((ssize_t) 0, (ssize_t) tws->tws_width - line_len);
if (avail == 0) {
avail = INT_MAX;
}
while (start_pos < (int)this->al_string.length()) {
ssize_t lpc;
// Find the end of a word or a breakpoint.
for (lpc = start_pos;
lpc < (int)this->al_string.length() &&
(isalnum(this->al_string[lpc]) ||
this->al_string[lpc] == ',' ||
this->al_string[lpc] == '_' ||
this->al_string[lpc] == '.' ||
this->al_string[lpc] == ';');
lpc++) {
if (this->al_string[lpc] == '-' ||
this->al_string[lpc] == '.') {
lpc += 1;
break;
}
}
if ((avail != usable_width) && (lpc - start_pos > avail)) {
// Need to wrap the word. Do the wrap.
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 {
// There's still room to add stuff.
avail -= (lpc - start_pos);
while (lpc < (int)this->al_string.length() && avail) {
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] == '_') {
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;
for (lpc = start_pos;
lpc < (int)this->al_string.length() &&
this->al_string[lpc] == ' ';
lpc++) {
}
if (lpc != start_pos) {
this->erase(start_pos, (lpc - start_pos));
}
}
}
}
}
return *this;
}
attr_line_t attr_line_t::subline(size_t start, size_t len) const
{
if (len == std::string::npos) {
len = this->al_string.length() - start;
}
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),
sa.sa_type, sa.sa_value);
line_range &last_lr = retval.al_attrs.back().sa_range;
ensure(last_lr.lr_end <= (int) retval.al_string.length());
}
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));
}
attr_line_t &attr_line_t::right_justify(unsigned long width)
{
long padding = width - this->length();
if (padding > 0) {
this->al_string.insert(0, padding, ' ');
for (auto &al_attr : this->al_attrs) {
if (al_attr.sa_range.lr_start > 0) {
al_attr.sa_range.lr_start += padding;
}
if (al_attr.sa_range.lr_end != -1) {
al_attr.sa_range.lr_end += padding;
}
}
}
return *this;
}
size_t attr_line_t::nearest_text(size_t x) const
{
if (x > 0 && (int)x >= this->length()) {
if (this->empty()) {
x = 0;
} else {
x = this->length() - 1;
}
}
while (x > 0 && isspace(this->al_string[x])) {
x -= 1;
}
return x;
}
void attr_line_t::apply_hide()
{
auto& vc = view_colors::singleton();
auto& sa = this->al_attrs;
for (auto &sattr : sa) {
if (sattr.sa_type == &SA_HIDDEN &&
sattr.sa_range.length() > 3) {
struct line_range &lr = sattr.sa_range;
for_each(sa.begin(), sa.end(), [&] (string_attr &attr) {
if (attr.sa_type == &view_curses::VC_STYLE &&
lr.contains(attr.sa_range)) {
attr.sa_type = &SA_REMOVED;
}
});
this->al_string.replace(lr.lr_start, lr.length(), "\xE2\x8B\xAE");
shift_string_attrs(sa, lr.lr_start + 1, -(lr.length() - 3));
sattr.sa_type = &view_curses::VC_STYLE;
sattr.sa_value.sav_int = vc.attrs_for_role(view_colors::VCR_HIDDEN);
lr.lr_end = lr.lr_start + 3;
}
}
}

View File

@ -463,22 +463,7 @@ public:
return *this;
};
attr_line_t &right_justify(unsigned long width) {
long padding = width - this->length();
if (padding > 0) {
this->al_string.insert(0, padding, ' ');
for (auto &al_attr : this->al_attrs) {
if (al_attr.sa_range.lr_start > 0) {
al_attr.sa_range.lr_start += padding;
}
if (al_attr.sa_range.lr_end != -1) {
al_attr.sa_range.lr_end += padding;
}
}
}
return *this;
}
attr_line_t &right_justify(unsigned long width);
ssize_t length() const {
size_t retval = this->al_string.length();
@ -527,21 +512,9 @@ public:
void split_lines(std::vector<attr_line_t> &lines) const;
size_t nearest_text(size_t x) const {
if (x > 0 && (int)x >= this->length()) {
if (this->empty()) {
x = 0;
} else {
x = this->length() - 1;
}
}
size_t nearest_text(size_t x) const;
while (x > 0 && isspace(this->al_string[x])) {
x -= 1;
}
return x;
}
void apply_hide();
private:
const static size_t RESERVE_SIZE = 128;

View File

@ -434,26 +434,7 @@ void textview_curses::textview_value_for_row(vis_line_t row,
}
if (this->tc_hide_fields) {
for (auto &sattr : sa) {
if (sattr.sa_type == &SA_HIDDEN &&
sattr.sa_range.length() > 3) {
struct line_range &lr = sattr.sa_range;
str.replace(lr.lr_start, lr.length(), "\xE2\x8B\xAE");
shift_string_attrs(sa, lr.lr_start + 1, -(lr.length() - 3));
sattr.sa_type = &VC_STYLE;
sattr.sa_value.sav_int =
vc.attrs_for_role(view_colors::VCR_HIDDEN);
lr.lr_end = lr.lr_start + 3;
for_each(sa.begin(), sa.end(), [&] (string_attr &attr) {
if (attr.sa_type == &VC_STYLE &&
attr.sa_range.lr_start == lr.lr_start) {
attr.sa_type = &SA_REMOVED;
}
});
}
}
value_out.apply_hide();
}
#if 0

View File

@ -820,6 +820,10 @@ public:
return retval;
};
bool get_hide_fields() const {
return this->tc_hide_fields;
}
void execute_search(const std::string &regex_orig);
void redo_search() {

View File

@ -88,172 +88,6 @@ alerter &alerter::singleton() {
return retval;
}
attr_line_t &attr_line_t::with_ansi_string(const char *str, ...)
{
auto_mem<char> formatted_str;
va_list args;
va_start(args, str);
auto ret = vasprintf(formatted_str.out(), str, args);
va_end(args);
if (ret >= 0 && formatted_str != nullptr) {
this->al_string = formatted_str;
scrub_ansi_string(this->al_string, this->al_attrs);
}
return *this;
}
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;
}
attr_line_t &attr_line_t::insert(size_t index, const attr_line_t &al, text_wrap_settings *tws)
{
if (index < this->al_string.length()) {
shift_string_attrs(this->al_attrs, index, al.al_string.length());
}
this->al_string.insert(index, al.al_string);
for (auto &sa : al.al_attrs) {
this->al_attrs.emplace_back(sa);
line_range &lr = this->al_attrs.back().sa_range;
lr.shift(0, index);
if (lr.lr_end == -1) {
lr.lr_end = index + al.al_string.length();
}
}
if (tws != nullptr && (int)this->al_string.length() > tws->tws_width) {
ssize_t start_pos = index;
ssize_t line_start = this->al_string.rfind('\n', start_pos);
if (line_start == (ssize_t)string::npos) {
line_start = 0;
} else {
line_start += 1;
}
ssize_t line_len = index - line_start;
ssize_t usable_width = tws->tws_width - tws->tws_indent;
ssize_t avail = max((ssize_t) 0, (ssize_t) tws->tws_width - line_len);
if (avail == 0) {
avail = INT_MAX;
}
while (start_pos < (int)this->al_string.length()) {
ssize_t lpc;
// Find the end of a word or a breakpoint.
for (lpc = start_pos;
lpc < (int)this->al_string.length() &&
(isalnum(this->al_string[lpc]) ||
this->al_string[lpc] == ',' ||
this->al_string[lpc] == '_' ||
this->al_string[lpc] == '.' ||
this->al_string[lpc] == ';');
lpc++) {
if (this->al_string[lpc] == '-' ||
this->al_string[lpc] == '.') {
lpc += 1;
break;
}
}
if ((avail != usable_width) && (lpc - start_pos > avail)) {
// Need to wrap the word. Do the wrap.
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 {
// There's still room to add stuff.
avail -= (lpc - start_pos);
while (lpc < (int)this->al_string.length() && avail) {
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] == '_') {
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;
for (lpc = start_pos;
lpc < (int)this->al_string.length() &&
this->al_string[lpc] == ' ';
lpc++) {
}
if (lpc != start_pos) {
this->erase(start_pos, (lpc - start_pos));
}
}
}
}
}
return *this;
}
attr_line_t attr_line_t::subline(size_t start, size_t len) const
{
if (len == std::string::npos) {
len = this->al_string.length() - start;
}
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),
sa.sa_type, sa.sa_value);
line_range &last_lr = retval.al_attrs.back().sa_range;
ensure(last_lr.lr_end <= (int) retval.al_string.length());
}
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));
}
struct utf_to_display_adjustment {
int uda_origin;
int uda_offset;

View File

@ -101,6 +101,9 @@ static void open_pretty_view()
text_sub_source::RF_FULL|
text_sub_source::RF_REWRITE);
lss.text_attrs_for_line(*log_tc, vl, al.get_attrs());
if (log_tc->get_hide_fields()) {
al.apply_hide();
}
line_range orig_lr = find_string_attr_range(
al.get_attrs(), &SA_ORIGINAL_LINE);