From d5b06fca5f66560df8aca86f8b70dd1ba0291d8f Mon Sep 17 00:00:00 2001 From: Timothy Stack Date: Wed, 29 Jun 2022 11:04:37 -0700 Subject: [PATCH] [mark-expr] expression marks should count when showing elapsed time --- src/bookmarks.hh | 16 +++++++-------- src/hotkeys.cc | 8 ++++---- src/lnav_commands.cc | 13 ++++++++----- src/log_gutter_source.hh | 13 ++++++------- src/logfile_sub_source.cc | 35 ++++++++++++++++++--------------- src/readline_callbacks.cc | 19 +++++++++--------- src/view_helpers.cc | 40 +++++++++++++++++++++----------------- src/view_helpers.hh | 7 ++++--- test/test_bookmarks.cc | 41 +++++++++++++++++++++------------------ 9 files changed, 103 insertions(+), 89 deletions(-) diff --git a/src/bookmarks.hh b/src/bookmarks.hh index f11356d1..189e8308 100644 --- a/src/bookmarks.hh +++ b/src/bookmarks.hh @@ -121,7 +121,7 @@ public: * the next bookmark is returned. If the 'start' value is not a * bookmark, the next highest value in the vector is returned. */ - LineType next(LineType start) const; + nonstd::optional next(LineType start) const; /** * @param start The value to start the search for the previous @@ -130,7 +130,7 @@ public: * are no more prior bookmarks. * @see next */ - LineType prev(LineType start) const; + nonstd::optional prev(LineType start) const; }; /** @@ -162,10 +162,10 @@ private: }; template -LineType +nonstd::optional bookmark_vector::next(LineType start) const { - LineType retval(-1); + nonstd::optional retval; require(start >= -1); @@ -174,16 +174,16 @@ bookmark_vector::next(LineType start) const retval = *ub; } - ensure(retval == -1 || start < retval); + ensure(!retval || start < retval.value()); return retval; } template -LineType +nonstd::optional bookmark_vector::prev(LineType start) const { - LineType retval(-1); + nonstd::optional retval; require(start >= 0); @@ -193,7 +193,7 @@ bookmark_vector::prev(LineType start) const retval = *lb; } - ensure(retval < start); + ensure(!retval || retval.value() < start); return retval; } diff --git a/src/hotkeys.cc b/src/hotkeys.cc index 7fe79e2b..06c4a388 100644 --- a/src/hotkeys.cc +++ b/src/hotkeys.cc @@ -313,8 +313,8 @@ handle_paging_key(int ch) case 'f': if (tc == &lnav_data.ld_views[LNV_LOG]) { - tc->set_top( - bm[&logfile_sub_source::BM_FILES].next(tc->get_top())); + bm[&logfile_sub_source::BM_FILES].next(tc->get_top()) | + [&tc](auto vl) { tc->set_top(vl); }; } else if (tc == &lnav_data.ld_views[LNV_TEXT]) { textfile_sub_source& tss = lnav_data.ld_text_source; @@ -327,8 +327,8 @@ handle_paging_key(int ch) case 'F': if (tc == &lnav_data.ld_views[LNV_LOG]) { - tc->set_top( - bm[&logfile_sub_source::BM_FILES].prev(tc->get_top())); + bm[&logfile_sub_source::BM_FILES].prev(tc->get_top()) | + [&tc](auto vl) { tc->set_top(vl); }; } else if (tc == &lnav_data.ld_views[LNV_TEXT]) { textfile_sub_source& tss = lnav_data.ld_text_source; diff --git a/src/lnav_commands.cc b/src/lnav_commands.cc index 10309362..8c706245 100644 --- a/src/lnav_commands.cc +++ b/src/lnav_commands.cc @@ -3211,22 +3211,25 @@ com_clear_partition(exec_context& ec, = tc.get_bookmarks()[&textview_curses::BM_META]; std::map& bm = lss.get_user_bookmark_metadata(); - vis_line_t part_start; + nonstd::optional part_start; if (binary_search(bv.begin(), bv.end(), tc.get_top())) { part_start = tc.get_top(); } else { part_start = bv.prev(tc.get_top()); } - if (part_start == -1) { + if (!part_start) { return ec.make_error("top line is not in a partition"); - } else if (!ec.ec_dry_run) { - content_line_t cl = lss.at(part_start); + } + + if (!ec.ec_dry_run) { + content_line_t cl = lss.at(part_start.value()); bookmark_metadata& line_meta = bm[cl]; line_meta.bm_name.clear(); if (line_meta.empty()) { - tc.set_user_mark(&textview_curses::BM_META, part_start, false); + tc.set_user_mark( + &textview_curses::BM_META, part_start.value(), false); } retval = "info: cleared partition name"; diff --git a/src/log_gutter_source.hh b/src/log_gutter_source.hh index 738d6961..b3aa6ff7 100644 --- a/src/log_gutter_source.hh +++ b/src/log_gutter_source.hh @@ -44,30 +44,29 @@ public: { textview_curses* tc = (textview_curses*) &lv; vis_bookmarks& bm = tc->get_bookmarks(); - vis_line_t next; bool search_hit = false; start -= 1; - next = bm[&textview_curses::BM_SEARCH].next(vis_line_t(start)); - search_hit = (next != -1 && next <= end); + auto next = bm[&textview_curses::BM_SEARCH].next(vis_line_t(start)); + search_hit = (next && next.value() <= end); next = bm[&textview_curses::BM_USER].next(vis_line_t(start)); - if (next == -1) { + if (!next) { next = bm[&textview_curses::BM_META].next(vis_line_t(start)); } - if (next != -1 && next <= end) { + if (next && next.value() <= end) { ch = search_hit ? ACS_PLUS : ACS_LTEE; } else { ch = search_hit ? ACS_RTEE : ACS_VLINE; } next = bm[&logfile_sub_source::BM_ERRORS].next(vis_line_t(start)); - if (next != -1 && next <= end) { + if (next && next.value() <= end) { role_out = role_t::VCR_ERROR; bar_role_out = role_t::VCR_SCROLLBAR_ERROR; } else { next = bm[&logfile_sub_source::BM_WARNINGS].next(vis_line_t(start)); - if (next != -1 && next <= end) { + if (next && next.value() <= end) { role_out = role_t::VCR_WARNING; bar_role_out = role_t::VCR_SCROLLBAR_WARNING; } diff --git a/src/logfile_sub_source.cc b/src/logfile_sub_source.cc index 645caecc..5ad92e20 100644 --- a/src/logfile_sub_source.cc +++ b/src/logfile_sub_source.cc @@ -315,23 +315,25 @@ logfile_sub_source::text_value_for_line(textview_curses& tc, if (this->lss_flags & F_TIME_OFFSET) { auto curr_tv = this->lss_token_line->get_timeval(); struct timeval diff_tv; - - vis_line_t prev_mark - = tc.get_bookmarks()[&textview_curses::BM_USER].prev( - vis_line_t(row)); - vis_line_t next_mark - = tc.get_bookmarks()[&textview_curses::BM_USER].next( - vis_line_t(row)); - if (prev_mark == -1 && next_mark != -1) { - auto next_line = this->find_line(this->at(next_mark)); + auto row_vl = vis_line_t(row); + + auto prev_umark + = tc.get_bookmarks()[&textview_curses::BM_USER].prev(row_vl); + auto next_umark + = tc.get_bookmarks()[&textview_curses::BM_USER].next(row_vl); + auto prev_emark + = tc.get_bookmarks()[&textview_curses::BM_USER_EXPR].prev(row_vl); + auto next_emark + = tc.get_bookmarks()[&textview_curses::BM_USER_EXPR].next(row_vl); + if (!prev_umark && !prev_emark && (next_umark || next_emark)) { + auto next_line = this->find_line(this->at( + std::max(next_umark.value_or(0), next_emark.value_or(0)))); diff_tv = curr_tv - next_line->get_timeval(); } else { - if (prev_mark == -1_vl) { - prev_mark = 0_vl; - } - - auto first_line = this->find_line(this->at(prev_mark)); + auto prev_row + = std::max(prev_umark.value_or(0), prev_emark.value_or(0)); + auto first_line = this->find_line(this->at(prev_row)); auto start_tv = first_line->get_timeval(); diff_tv = curr_tv - start_tv; } @@ -1918,10 +1920,11 @@ logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line) vis_bookmarks& bm = this->lmg_source.tss_view->get_bookmarks(); bookmark_vector& bv = bm[&textview_curses::BM_META]; - line = bv.next(vis_line_t(line)); - if (line == -1) { + auto line_opt = bv.next(vis_line_t(line)); + if (!line_opt) { this->lmg_done = true; } + line = line_opt.value_or(-1_vl); } void diff --git a/src/readline_callbacks.cc b/src/readline_callbacks.cc index 6519dd83..6cc3f21a 100644 --- a/src/readline_callbacks.cc +++ b/src/readline_callbacks.cc @@ -622,16 +622,16 @@ rl_callback_int(readline_curses* rc, bool is_alt) auto_mem pfile(pclose); vis_bookmarks& bm = tc->get_bookmarks(); const auto& bv = bm[&textview_curses::BM_SEARCH]; - vis_line_t vl = is_alt ? bv.prev(tc->get_top()) - : bv.next(tc->get_top()); + auto vl = is_alt ? bv.prev(tc->get_top()) + : bv.next(tc->get_top()); pfile = sysclip::open(sysclip::type_t::FIND); if (pfile.in() != nullptr) { fmt::print( pfile, FMT_STRING("{}"), rc->get_value().get_string()); } - if (vl != -1_vl) { - tc->set_top(vl); + if (vl) { + tc->set_top(vl.value()); } else { tc->set_follow_search_for(2000, [tc, is_alt, &bm]() { if (bm[&textview_curses::BM_SEARCH].empty()) { @@ -642,7 +642,7 @@ rl_callback_int(readline_curses* rc, bool is_alt) return false; } - vis_line_t first_hit; + nonstd::optional first_hit; if (is_alt) { first_hit = bm[&textview_curses::BM_SEARCH].prev( @@ -651,11 +651,12 @@ rl_callback_int(readline_curses* rc, bool is_alt) first_hit = bm[&textview_curses::BM_SEARCH].next( vis_line_t(tc->get_top() - 1)); } - if (first_hit != -1) { - if (first_hit > 0) { - --first_hit; + if (first_hit) { + auto first_hit_vl = first_hit.value(); + if (first_hit_vl > 0_vl) { + --first_hit_vl; } - tc->set_top(first_hit); + tc->set_top(first_hit_vl); } return true; diff --git a/src/view_helpers.cc b/src/view_helpers.cc index 27560bf0..ba17e0c6 100644 --- a/src/view_helpers.cc +++ b/src/view_helpers.cc @@ -700,10 +700,10 @@ update_hits(textview_curses* tc) auto prev_vl = bv.prev(tc->get_top()); - if (prev_vl != -1_vl) { + if (prev_vl) { attr_line_t al; - tc->textview_value_for_row(prev_vl, al); + tc->textview_value_for_row(prev_vl.value(), al); if (preview_count > 0) { all_matches.append("\n"); } @@ -711,15 +711,16 @@ update_hits(textview_curses* tc) sizeof(linebuf), "L%*d: ", max_line_width, - (int) prev_vl); + (int) prev_vl.value()); all_matches.append(linebuf).append(al); preview_count += 1; } - while ((vl = bv.next(vl)) != -1_vl - && preview_count < MAX_MATCH_COUNT) { + nonstd::optional next_vl; + while ((next_vl = bv.next(vl)) && preview_count < MAX_MATCH_COUNT) { attr_line_t al; + vl = next_vl.value(); tc->textview_value_for_row(vl, al); if (preview_count > 0) { all_matches.append("\n"); @@ -887,7 +888,8 @@ ensure_view(lnav_view_t expected) } nonstd::optional -next_cluster(vis_line_t (bookmark_vector::*f)(vis_line_t) const, +next_cluster(nonstd::optional (bookmark_vector::*f)( + vis_line_t) const, const bookmark_type_t* bt, const vis_line_t top) { @@ -895,36 +897,37 @@ next_cluster(vis_line_t (bookmark_vector::*f)(vis_line_t) const, vis_bookmarks& bm = tc->get_bookmarks(); bookmark_vector& bv = bm[bt]; bool top_is_marked = binary_search(bv.begin(), bv.end(), top); - vis_line_t last_top(top), new_top(top), tc_height; + vis_line_t last_top(top), tc_height; + nonstd::optional new_top = top; unsigned long tc_width; int hit_count = 0; tc->get_dimensions(tc_height, tc_width); - while ((new_top = (bv.*f)(new_top)) != -1) { - int diff = new_top - last_top; + while ((new_top = (bv.*f)(new_top.value()))) { + int diff = new_top.value() - last_top; hit_count += 1; if (!top_is_marked || diff > 1) { return new_top; } - if (hit_count > 1 && std::abs(new_top - top) >= tc_height) { - return vis_line_t(new_top - diff); + if (hit_count > 1 && std::abs(new_top.value() - top) >= tc_height) { + return vis_line_t(new_top.value() - diff); } if (diff < -1) { - last_top = new_top; - while ((new_top = (bv.*f)(new_top)) != -1) { - if ((std::abs(last_top - new_top) > 1) + last_top = new_top.value(); + while ((new_top = (bv.*f)(new_top.value()))) { + if ((std::abs(last_top - new_top.value()) > 1) || (hit_count > 1 - && (std::abs(top - new_top) >= tc_height))) + && (std::abs(top - new_top.value()) >= tc_height))) { break; } - last_top = new_top; + last_top = new_top.value(); } return last_top; } - last_top = new_top; + last_top = new_top.value(); } if (last_top != top) { @@ -935,7 +938,8 @@ next_cluster(vis_line_t (bookmark_vector::*f)(vis_line_t) const, } bool -moveto_cluster(vis_line_t (bookmark_vector::*f)(vis_line_t) const, +moveto_cluster(nonstd::optional (bookmark_vector::*f)( + vis_line_t) const, const bookmark_type_t* bt, vis_line_t top) { diff --git a/src/view_helpers.hh b/src/view_helpers.hh index a8e8a0bd..754e4912 100644 --- a/src/view_helpers.hh +++ b/src/view_helpers.hh @@ -83,11 +83,12 @@ void layout_views(); void update_hits(textview_curses* tc); nonstd::optional next_cluster( - vis_line_t (bookmark_vector::*f)(vis_line_t) const, + nonstd::optional (bookmark_vector::*f)(vis_line_t) + const, const bookmark_type_t* bt, vis_line_t top); -bool moveto_cluster(vis_line_t (bookmark_vector::*f)(vis_line_t) - const, +bool moveto_cluster(nonstd::optional ( + bookmark_vector::*f)(vis_line_t) const, const bookmark_type_t* bt, vis_line_t top); void previous_cluster(const bookmark_type_t* bt, textview_curses* tc); diff --git a/test/test_bookmarks.cc b/test/test_bookmarks.cc index 8ce397ae..37ae86e8 100644 --- a/test/test_bookmarks.cc +++ b/test/test_bookmarks.cc @@ -79,31 +79,31 @@ main(int argc, char* argv[]) } bv.clear(); - assert(bv.next(vis_line_t(0)) == -1); - assert(bv.prev(vis_line_t(0)) == -1); - assert(bv.next(vis_line_t(100)) == -1); - assert(bv.prev(vis_line_t(100)) == -1); + assert(!bv.next(vis_line_t(0))); + assert(!bv.prev(vis_line_t(0))); + assert(!bv.next(vis_line_t(100))); + assert(!bv.prev(vis_line_t(100))); bv.insert_once(vis_line_t(2)); - assert(bv.next(vis_line_t(0)) == 2); - assert(bv.next(vis_line_t(2)) == -1); - assert(bv.next(vis_line_t(3)) == -1); + assert(bv.next(vis_line_t(0)).value() == 2); + assert(!bv.next(vis_line_t(2))); + assert(!bv.next(vis_line_t(3))); - assert(bv.prev(vis_line_t(3)) == 2); - assert(bv.prev(vis_line_t(2)) == -1); + assert(bv.prev(vis_line_t(3)).value() == 2); + assert(!bv.prev(vis_line_t(2))); bv.insert_once(vis_line_t(4)); - assert(bv.next(vis_line_t(0)) == 2); - assert(bv.next(vis_line_t(2)) == 4); - assert(bv.next(vis_line_t(3)) == 4); - assert(bv.next(vis_line_t(4)) == -1); + assert(bv.next(vis_line_t(0)).value() == 2); + assert(bv.next(vis_line_t(2)).value() == 4); + assert(bv.next(vis_line_t(3)).value() == 4); + assert(!bv.next(vis_line_t(4))); - assert(bv.prev(vis_line_t(10)) == 4); - assert(bv.prev(vis_line_t(5)) == 4); - assert(bv.prev(vis_line_t(4)) == 2); - assert(bv.prev(vis_line_t(2)) == -1); + assert(bv.prev(vis_line_t(10)).value() == 4); + assert(bv.prev(vis_line_t(5)).value() == 4); + assert(bv.prev(vis_line_t(4)).value() == 2); + assert(!bv.prev(vis_line_t(2))); bv.clear(); @@ -121,7 +121,8 @@ main(int argc, char* argv[]) { vis_line_t last_line(-1); - for (lpc = 0; lpc != -1; lpc = bv.next(vis_line_t(lpc))) { + for (lpc = 0; lpc != -1; lpc = bv.next(vis_line_t(lpc)).value_or(-1_vl)) + { assert(lpc >= 0); assert(lpc < LINE_COUNT); assert(last_line < lpc); @@ -130,7 +131,9 @@ main(int argc, char* argv[]) } last_line = vis_line_t(10000); - for (lpc = LINE_COUNT - 1; lpc != -1; lpc = bv.prev(vis_line_t(lpc))) { + for (lpc = LINE_COUNT - 1; lpc != -1; + lpc = bv.prev(vis_line_t(lpc)).value_or(-1_vl)) + { assert(lpc >= 0); assert(lpc < LINE_COUNT); assert(last_line > lpc);