[lo-fi] try to maintain the position of lines on the display

Fixes #815
This commit is contained in:
Tim Stack 2022-08-05 16:09:54 -07:00
parent b829b6ddde
commit 1832303bde
5 changed files with 63 additions and 42 deletions

View File

@ -205,10 +205,10 @@ listview_curses::do_update()
width = std::min((unsigned long) this->vc_width, width);
}
wrap_width = width
- (this->lv_word_wrap ? 1
: this->lv_show_scrollbar ? 1
: 0);
wrap_width = width;
if (this->lv_show_scrollbar) {
wrap_width -= 1;
}
size_t row_count = this->get_inner_height();
row = this->lv_top;
@ -231,15 +231,16 @@ listview_curses::do_update()
overlay_line.clear();
++y;
} else if (row < (int) row_count) {
attr_line_t& al = rows[row - this->lv_top];
auto& al = rows[row - this->lv_top];
size_t remaining = 0;
do {
mvwattrline(this->lv_window,
y,
this->lv_x,
al,
lr,
this->vc_default_role);
remaining = mvwattrline(this->lv_window,
y,
this->lv_x,
al,
lr,
this->vc_default_role);
if (this->lv_word_wrap) {
mvwhline(this->lv_window,
y,
@ -250,8 +251,7 @@ listview_curses::do_update()
lr.lr_start += wrap_width;
lr.lr_end += wrap_width;
++y;
} while (this->lv_word_wrap && y < bottom
&& lr.lr_start < (int) al.length());
} while (this->lv_word_wrap && y < bottom && remaining > 0);
++row;
} else {
wattr_set(this->lv_window,

View File

@ -1212,7 +1212,7 @@ looper()
lnav_data.ld_views[lpc].set_window(lnav_data.ld_window);
lnav_data.ld_views[lpc].set_y(2);
lnav_data.ld_views[lpc].set_height(
vis_line_t(-(rlc->get_height() + 1)));
vis_line_t(-(rlc->get_height() + 3)));
lnav_data.ld_views[lpc].set_scroll_action(sb);
lnav_data.ld_views[lpc].set_search_action(update_hits);
lnav_data.ld_views[lpc].tc_state_event_handler = event_handler;

View File

@ -1168,7 +1168,9 @@ com_save_to(exec_context& ec,
bottom = tc->get_inner_height() - 1_vl;
}
auto y = 0_vl;
auto wrapped_count = 0_vl;
std::vector<attr_line_t> rows(bottom - top + 1);
auto dim = tc->get_dimensions();
attr_line_t ov_al;
auto* los = tc->get_overlay_source();
@ -1181,6 +1183,7 @@ com_save_to(exec_context& ec,
write_line_to(outfile, ov_al);
++y;
}
wrapped_count += vis_line_t((al.length() - 1) / (dim.second - 2));
write_line_to(outfile, al);
line_count += 1;
@ -1198,12 +1201,16 @@ com_save_to(exec_context& ec,
tc->set_word_wrap(wrapped);
tc->set_top(orig_top);
if (!(lnav_data.ld_flags & LNF_HEADLESS)) {
while (y + wrapped_count < dim.first + 2_vl) {
fmt::print(outfile, FMT_STRING("\n"));
++y;
}
}
} else if (args[0] == "write-raw-to") {
if (tc == &lnav_data.ld_views[LNV_DB]) {
std::vector<std::vector<const char*>>::iterator row_iter;
std::vector<const char*>::iterator iter;
for (row_iter = dls.dls_rows.begin();
for (auto row_iter = dls.dls_rows.begin();
row_iter != dls.dls_rows.end();
++row_iter)
{
@ -1213,9 +1220,8 @@ com_save_to(exec_context& ec,
break;
}
for (iter = row_iter->begin(); iter != row_iter->end(); ++iter)
{
fputs(*iter, outfile);
for (auto& iter : *row_iter) {
fputs(iter, outfile);
}
fprintf(outfile, "\n");
@ -1734,7 +1740,8 @@ com_filter(exec_context& ec,
"filters with a pipe symbol (e.g. foo|bar)");
}
auto compile_res = pcrepp::shared_from_str(args[1], PCRE_CASELESS | PCRE_UTF8);
auto compile_res
= pcrepp::shared_from_str(args[1], PCRE_CASELESS | PCRE_UTF8);
if (compile_res.isErr()) {
auto ce = compile_res.unwrapErr();
@ -2156,8 +2163,8 @@ com_create_search_table(exec_context& ec,
regex = lnav_data.ld_views[LNV_LOG].get_current_search();
}
auto re_res
= pcrepp::shared_from_str(regex, log_search_table::pattern_options());
auto re_res = pcrepp::shared_from_str(
regex, log_search_table::pattern_options());
if (re_res.isErr()) {
auto re_err = re_res.unwrapErr();
@ -5471,10 +5478,10 @@ readline_context::command_t STD_COMMANDS[] = {
.with_parameter(help_text("path", "The path to the file to write"))
.with_tags({"io", "scripting"})},
{"rebuild",
com_rebuild,
help_text(":rebuild")
.with_summary("Forcefully rebuild file indexes")
.with_tags({"scripting"})},
com_rebuild,
help_text(":rebuild")
.with_summary("Forcefully rebuild file indexes")
.with_tags({"scripting"})},
{"set-min-log-level",
com_set_min_log_level,
@ -5599,9 +5606,8 @@ init_lnav_commands(readline_context::command_map_t& cmd_map)
cmd_map["add-test"] = &add_test;
}
if (getenv("lnav_test") != nullptr) {
static readline_context::command_t
shexec(com_shexec), poll_now(com_poll_now),
test_comment(com_test_comment);
static readline_context::command_t shexec(com_shexec),
poll_now(com_poll_now), test_comment(com_test_comment);
cmd_map["shexec"] = &shexec;
cmd_map["poll-now"] = &poll_now;

View File

@ -125,7 +125,7 @@ view_curses::awaiting_user_input()
}
}
void
size_t
view_curses::mvwattrline(WINDOW* window,
int y,
int x,
@ -148,15 +148,21 @@ view_curses::mvwattrline(WINDOW* window,
bool has_fg = false;
short* bg_color = (short*) alloca(line_width_chars * sizeof(short));
bool has_bg = false;
line_range lr_bytes{lr_chars.lr_start, lr_chars.lr_end};
line_range lr_bytes;
int char_index = 0;
for (size_t lpc = 0; lpc < line.size(); lpc++) {
int exp_start_index = expanded_line.size();
auto ch = static_cast<unsigned char>(line[lpc]);
if (char_index == lr_chars.lr_start) {
lr_bytes.lr_start = exp_start_index;
} else if (char_index == lr_chars.lr_end) {
lr_bytes.lr_end = exp_start_index;
}
switch (ch) {
case '\t':
case '\t': {
do {
expanded_line.push_back(' ');
char_index += 1;
@ -164,6 +170,7 @@ view_curses::mvwattrline(WINDOW* window,
utf_adjustments.emplace_back(
lpc, expanded_line.size() - exp_start_index - 1);
break;
}
case '\r':
case '\n':
@ -183,12 +190,14 @@ view_curses::mvwattrline(WINDOW* window,
expanded_line.push_back(ch);
if (offset) {
#if 0
if (char_index < lr_chars.lr_start) {
lr_bytes.lr_start += abs(offset);
}
if (char_index < lr_chars.lr_end) {
lr_bytes.lr_end += abs(offset);
}
#endif
exp_offset += offset;
utf_adjustments.emplace_back(lpc, offset);
for (; offset && (lpc + 1) < line.size();
@ -203,6 +212,10 @@ view_curses::mvwattrline(WINDOW* window,
}
}
}
if (lr_bytes.lr_end == -1) {
lr_bytes.lr_end = expanded_line.size();
}
size_t retval = expanded_line.size() - lr_bytes.lr_end;
full_line = expanded_line;
@ -218,8 +231,8 @@ view_curses::mvwattrline(WINDOW* window,
waddnstr(
window, &full_line.c_str()[lr_bytes.lr_start], lr_bytes.length());
}
if (lr_bytes.lr_end > (int) full_line.size()) {
whline(window, ' ', lr_bytes.lr_end - (full_line.size() + exp_offset));
if (lr_chars.lr_end > char_index) {
whline(window, ' ', lr_chars.lr_end - char_index);
}
std::stable_sort(sa.begin(), sa.end());
@ -421,6 +434,8 @@ view_curses::mvwattrline(WINDOW* window,
}
mvwadd_wchnstr(window, y, x, row_ch, ch_width);
}
return retval;
}
constexpr short view_colors::MATCH_COLOR_DEFAULT;

View File

@ -375,12 +375,12 @@ public:
static void awaiting_user_input();
static void mvwattrline(WINDOW* window,
int y,
int x,
attr_line_t& al,
const struct line_range& lr,
role_t base_role = role_t::VCR_TEXT);
static size_t mvwattrline(WINDOW* window,
int y,
int x,
attr_line_t& al,
const struct line_range& lr,
role_t base_role = role_t::VCR_TEXT);
protected:
bool vc_visible{true};