[internal] add enumerate() helper

pull/1265/head
Tim Stack 3 weeks ago
parent 77b9829224
commit 984e133292

@ -52,6 +52,7 @@ add_library(
is_utf8.hh
isc.hh
itertools.hh
itertools.enumerate.hh
keycodes.hh
line_range.hh
lnav.console.hh

@ -48,6 +48,7 @@ noinst_HEADERS = \
is_utf8.hh \
isc.hh \
itertools.hh \
itertools.enumerate.hh \
keycodes.hh \
line_range.hh \
lnav_log.hh \

@ -0,0 +1,88 @@
/**
* Copyright (c) 2024, 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.
*/
#ifndef lnav_itertools_enumerate_hh
#define lnav_itertools_enumerate_hh
#include <tuple>
namespace lnav::itertools {
namespace details {
template<typename Iterator, typename CounterType = size_t>
class EnumerateIterator {
Iterator iter;
CounterType index;
public:
EnumerateIterator(Iterator iter, CounterType index)
: iter(std::move(iter)), index(index)
{
}
EnumerateIterator& operator++()
{
++iter;
++index;
return *this;
}
auto operator*() { return std::forward_as_tuple(index, *iter); }
bool operator!=(EnumerateIterator const& rhs) const
{
return iter != rhs.iter;
}
};
} // namespace details
template<typename Iterable, typename CounterType = size_t>
class enumerate {
public:
enumerate(Iterable& iterable, CounterType start = 0)
: iterable(iterable), index(start)
{
}
auto begin()
{
return details::EnumerateIterator{std::begin(iterable), index};
}
auto end() { return details::EnumerateIterator{std::end(iterable), index}; }
private:
Iterable& iterable;
CounterType index;
};
} // namespace lnav::itertools
#endif

@ -29,6 +29,7 @@
#include "breadcrumb_curses.hh"
#include "base/itertools.enumerate.hh"
#include "base/itertools.hh"
#include "itertools.similar.hh"
@ -39,7 +40,8 @@ breadcrumb_curses::no_op_action(breadcrumb_curses&)
{
}
breadcrumb_curses::breadcrumb_curses()
breadcrumb_curses::
breadcrumb_curses()
{
this->bc_match_search_overlay.sos_parent = this;
this->bc_match_source.set_reverse_selection(true);
@ -60,7 +62,6 @@ breadcrumb_curses::do_update()
return false;
}
size_t crumb_index = 0;
size_t sel_crumb_offset = 0;
auto width = static_cast<size_t>(getmaxx(this->bc_window));
auto crumbs = this->bc_focused_crumbs.empty() ? this->bc_line_source()
@ -72,7 +73,8 @@ breadcrumb_curses::do_update()
}
this->bc_displayed_crumbs.clear();
attr_line_t crumbs_line;
for (const auto& crumb : crumbs) {
for (const auto& [crumb_index, crumb] : lnav::itertools::enumerate(crumbs))
{
auto accum_width = crumbs_line.column_width();
auto elem_width = crumb.c_display_value.column_width();
auto is_selected = this->bc_selected_crumb
@ -99,7 +101,6 @@ breadcrumb_curses::do_update()
(int) (accum_width + elem_width),
line_range::unit::codepoint},
crumb_index);
crumb_index += 1;
crumbs_line.append(" \uff1a"_breadcrumb);
}

@ -30,6 +30,7 @@
#include "md2attr_line.hh"
#include "base/attr_line.builder.hh"
#include "base/itertools.enumerate.hh"
#include "base/itertools.hh"
#include "base/lnav_log.hh"
#include "base/map_util.hh"
@ -76,10 +77,11 @@ md2attr_line::flush_footnotes()
auto longest_foot = this->ml_footnotes
| lnav::itertools::map(&attr_line_t::utf8_length_or_length)
| lnav::itertools::max(0);
size_t index = 1;
block_text.append("\n");
for (auto& foot : this->ml_footnotes) {
for (const auto& [index, foot] :
lnav::itertools::enumerate(this->ml_footnotes, 1))
{
auto footline
= attr_line_t(" ")
.append("\u258c"_footnote_border)
@ -91,7 +93,6 @@ md2attr_line::flush_footnotes()
.with_attr_for_all(SA_PREFORMATTED.value());
block_text.append(footline).append("\n");
index += 1;
}
this->ml_footnotes.clear();
}

Loading…
Cancel
Save