From 62e4c68907d8d33709c2c1f92a161dff00b3d5f2 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 22:01:36 +0200 Subject: Adding upstream version 0.11.2. Signed-off-by: Daniel Baumann --- src/plain_text_source.hh | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/plain_text_source.hh (limited to 'src/plain_text_source.hh') diff --git a/src/plain_text_source.hh b/src/plain_text_source.hh new file mode 100644 index 0000000..3cddb1c --- /dev/null +++ b/src/plain_text_source.hh @@ -0,0 +1,137 @@ +/** + * Copyright (c) 2015, 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_PLAIN_TEXT_SOURCE_HH +#define LNAV_PLAIN_TEXT_SOURCE_HH + +#include +#include + +#include "base/attr_line.hh" +#include "base/file_range.hh" +#include "document.sections.hh" +#include "textview_curses.hh" + +class plain_text_source + : public text_sub_source + , public vis_location_history + , public text_anchors { +public: + struct text_line { + text_line(file_off_t off, attr_line_t value) + : tl_offset(off), tl_value(std::move(value)) + { + } + + bool contains_offset(file_off_t off) const + { + return (this->tl_offset <= off + && off < this->tl_offset + this->tl_value.length()); + } + + file_off_t tl_offset; + attr_line_t tl_value; + }; + + plain_text_source() = default; + + plain_text_source(const std::string& text); + + plain_text_source(const std::vector& text_lines); + + plain_text_source(const std::vector& text_lines); + + plain_text_source& set_reverse_selection(bool val) + { + this->tds_reverse_selection = val; + return *this; + } + + plain_text_source& replace_with(const attr_line_t& text_lines); + + plain_text_source& replace_with(const std::vector& text_lines); + + void clear(); + + plain_text_source& truncate_to(size_t max_lines); + + size_t text_line_count() override { return this->tds_lines.size(); } + + bool empty() const { return this->tds_lines.empty(); } + + size_t text_line_width(textview_curses& curses) override; + + void text_value_for_line(textview_curses& tc, + int row, + std::string& value_out, + line_flags_t flags) override; + + void text_attrs_for_line(textview_curses& tc, + int line, + string_attrs_t& value_out) override; + + size_t text_size_for_line(textview_curses& tc, + int row, + line_flags_t flags) override; + + text_format_t get_text_format() const override; + + const std::vector& get_lines() const { return this->tds_lines; } + + plain_text_source& set_text_format(text_format_t format) + { + this->tds_text_format = format; + return *this; + } + + nonstd::optional get_location_history() override + { + return this; + } + + void text_crumbs_for_line(int line, + std::vector& crumbs) override; + + nonstd::optional row_for_anchor(const std::string& id) override; + nonstd::optional anchor_for_row(vis_line_t vl) override; + std::unordered_set get_anchors() override; + +protected: + size_t compute_longest_line(); + + nonstd::optional line_for_offset(file_off_t off) const; + + std::vector tds_lines; + text_format_t tds_text_format{text_format_t::TF_UNKNOWN}; + size_t tds_longest_line{0}; + bool tds_reverse_selection{false}; + lnav::document::metadata tds_doc_sections; +}; + +#endif // LNAV_PLAIN_TEXT_SOURCE_HH -- cgit v1.2.3