Skip to content

Commit

Permalink
[theme] use default styles for cursor-line/selected-text if they are …
Browse files Browse the repository at this point in the history
…missing from the current theme

Some markdown-related changes as well
  • Loading branch information
tstack committed May 18, 2024
1 parent 4fd6b62 commit 50e895c
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 208 deletions.
8 changes: 6 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ Features:
* The `log_opid` column on log vtables can now by `UPDATE`d
so that you can manually set an opid on log messages that
don't have one. Setting an opid allows messages to show
up in the gantt chart view.
up in the Gantt chart view.
* Add support for GitHub Markdown Alerts.

Interface Changes:
* In the Gantt chart view, pressing `ENTER` will focus on
the preview pane so you can scroll through messages
the preview pane, so you can scroll through messages
with the selected Op ID.
* With mouse mode enabled, `CTRL` can be used as an alternate
to `SHIFT` when clicking/dragging in the main view to
highlight lines. A few terminals capture shift+clicks as a
way to select text and do not pass them to the application.
* Clicking on an internal link in a Markdown document will move
to that section.

Bug Fixes:
* Log messages in formats with custom timestamp formats were
not being converted to the local timezone.
* The timezone offset is now shown in the parser details
overlay for log messages.
* If a theme does not define `cursor-line` or `selected-text`
styles, the styles from the default theme will be used.

Maintenance:
* You can now do an `UPDATE` on the `lnav_top_view` SQL view.
Expand Down
30 changes: 30 additions & 0 deletions src/base/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ scrub_ws(const char* in, ssize_t len)
return retval;
}

static constexpr const char* const SUPERSCRIPT_NUMS[] = {
"",
"¹",
"²",
"³",
"",
"",
"",
"",
"",
"",
};

std::string
to_superscript(const std::string& in)
{
std::string retval;
for (const auto ch : in) {
if (isdigit(ch)) {
auto index = ch - '0';

retval.append(SUPERSCRIPT_NUMS[index]);
} else {
retval.push_back(ch);
}
}

return retval;
}

namespace fmt {
auto
formatter<lnav::tainted_string>::format(const lnav::tainted_string& ts,
Expand Down
13 changes: 11 additions & 2 deletions src/base/string_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ on_blank(const std::string& str, const std::string& def)
return str;
}

std::string to_superscript(const std::string& in);

template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
inline std::string
to_superscript(T in)
{
return to_superscript(fmt::to_string(in));
}

namespace lnav {
class tainted_string {
public:
Expand Down Expand Up @@ -282,8 +291,8 @@ private:
namespace fmt {
template<>
struct formatter<lnav::tainted_string> : formatter<string_view> {
auto format(const lnav::tainted_string& ts,
format_context& ctx) -> decltype(ctx.out()) const;
auto format(const lnav::tainted_string& ts, format_context& ctx)
-> decltype(ctx.out()) const;
};
} // namespace fmt

Expand Down
26 changes: 16 additions & 10 deletions src/md2attr_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ md2attr_line::leave_span(const md4cpp::event_handler::span& sp)
VC_STYLE.value(text_attrs{A_UNDERLINE}),
});
} else if (sp.is<MD_SPAN_A_DETAIL*>()) {
auto* a_detail = sp.get<MD_SPAN_A_DETAIL*>();
const auto* a_detail = sp.get<MD_SPAN_A_DETAIL*>();
auto href_str = std::string(a_detail->href.text, a_detail->href.size);
line_range lr{
static_cast<int>(this->ml_span_starts.back()),
Expand All @@ -585,8 +585,9 @@ md2attr_line::leave_span(const md4cpp::event_handler::span& sp)
});
this->append_url_footnote(href_str);
} else if (sp.is<MD_SPAN_IMG_DETAIL*>()) {
auto* img_detail = sp.get<MD_SPAN_IMG_DETAIL*>();
auto src_str = std::string(img_detail->src.text, img_detail->src.size);
const auto* img_detail = sp.get<MD_SPAN_IMG_DETAIL*>();
const auto src_str
= std::string(img_detail->src.text, img_detail->src.size);

this->append_url_footnote(src_str);
}
Expand Down Expand Up @@ -747,7 +748,7 @@ md2attr_line::to_attr_line(const pugi::xml_node& doc)
.append(" ")
.append(
lnav::string::attrs::href(link_label, src_href.value()))
.appendf(FMT_STRING("[{}]"), this->ml_footnotes.size() + 1);
.append(to_superscript(this->ml_footnotes.size() + 1));

auto href
= attr_line_t()
Expand Down Expand Up @@ -1014,7 +1015,7 @@ md2attr_line::text(MD_TEXTTYPE tt, const string_fragment& sf)
std::string span_text;

auto loop_res = REPL_RE.capture_from(sf).for_each(
[&span_text](lnav::pcre2pp::match_data& md) {
[&span_text](const lnav::pcre2pp::match_data& md) {
span_text += md.leading();

auto matched = *md[0];
Expand Down Expand Up @@ -1054,19 +1055,24 @@ md2attr_line::text(MD_TEXTTYPE tt, const string_fragment& sf)
void
md2attr_line::append_url_footnote(std::string href_str)
{
if (startswith(href_str, "#")) {
return;
}

auto is_internal = startswith(href_str, "#");
auto& last_block = this->ml_blocks.back();
last_block.appendf(FMT_STRING("[{}]"), this->ml_footnotes.size() + 1);
last_block.with_attr(string_attr{
line_range{
(int) this->ml_span_starts.back(),
(int) last_block.length(),
},
VC_STYLE.value(text_attrs{A_UNDERLINE}),
});
if (is_internal) {
return;
}

if (this->ml_last_superscript_index == last_block.length()) {
last_block.append("\u02d2");
}
last_block.append(to_superscript(this->ml_footnotes.size() + 1));
this->ml_last_superscript_index = last_block.length();
if (this->ml_source_path && href_str.find(':') == std::string::npos) {
auto link_path = ghc::filesystem::absolute(
this->ml_source_path.value().parent_path() / href_str);
Expand Down
1 change: 1 addition & 0 deletions src/md2attr_line.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private:
std::vector<std::pair<std::string, size_t>> ml_html_starts;
std::vector<attr_line_t> ml_footnotes;
int32_t ml_code_depth{0};
ssize_t ml_last_superscript_index{-1};
};

#endif
28 changes: 14 additions & 14 deletions src/plain_text_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ to_text_line(const std::vector<attr_line_t>& lines)
});
}

plain_text_source::plain_text_source(const std::string& text)
plain_text_source::
plain_text_source(const std::string& text)
{
size_t start = 0, end;

Expand All @@ -64,12 +65,14 @@ plain_text_source::plain_text_source(const std::string& text)
this->tds_longest_line = this->compute_longest_line();
}

plain_text_source::plain_text_source(const std::vector<std::string>& text_lines)
plain_text_source::
plain_text_source(const std::vector<std::string>& text_lines)
{
this->replace_with(text_lines);
}

plain_text_source::plain_text_source(const std::vector<attr_line_t>& text_lines)
plain_text_source::
plain_text_source(const std::vector<attr_line_t>& text_lines)
: tds_lines(to_text_line(text_lines))
{
this->tds_longest_line = this->compute_longest_line();
Expand Down Expand Up @@ -432,8 +435,7 @@ plain_text_source::row_for_anchor(const std::string& id)
meta.m_sections_root.get(),
[this, &id, &retval](const lnav::document::hier_node* node) {
for (const auto& child_pair : node->hn_named_children) {
const auto& child_anchor
= text_anchors::to_anchor_string(child_pair.first);
const auto& child_anchor = to_anchor_string(child_pair.first);

if (child_anchor != id) {
continue;
Expand All @@ -456,8 +458,7 @@ plain_text_source::get_anchors()
this->tds_doc_sections.m_sections_root.get(),
[&retval](const lnav::document::hier_node* node) {
for (const auto& child_pair : node->hn_named_children) {
retval.emplace(
text_anchors::to_anchor_string(child_pair.first));
retval.emplace(to_anchor_string(child_pair.first));
}
});

Expand Down Expand Up @@ -488,8 +489,7 @@ plain_text_source::anchor_for_row(vis_line_t vl)
|| this->tds_text_format == text_format_t::TF_MARKDOWN)
&& path_for_line.back().is<std::string>())
{
return text_anchors::to_anchor_string(
path_for_line.back().get<std::string>());
return to_anchor_string(path_for_line.back().get<std::string>());
}

auto comps = path_for_line | lnav::itertools::map([](const auto& elem) {
Expand All @@ -505,7 +505,7 @@ plain_text_source::anchor_for_row(vis_line_t vl)
}

std::optional<vis_line_t>
plain_text_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
plain_text_source::adjacent_anchor(vis_line_t vl, direction dir)
{
if (vl > this->tds_lines.size()
|| this->tds_doc_sections.m_sections_root == nullptr)
Expand All @@ -525,14 +525,14 @@ plain_text_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
}

switch (dir) {
case text_anchors::direction::prev: {
case direction::prev: {
if (neighbors_res->cnr_previous) {
return this->line_for_offset(
neighbors_res->cnr_previous.value()->hn_start);
}
break;
}
case text_anchors::direction::next: {
case direction::next: {
if (neighbors_res->cnr_next) {
return this->line_for_offset(
neighbors_res->cnr_next.value()->hn_start);
Expand Down Expand Up @@ -585,14 +585,14 @@ plain_text_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
}

switch (dir) {
case text_anchors::direction::prev: {
case direction::prev: {
if (neighbors_res->cnr_previous) {
return this->line_for_offset(
neighbors_res->cnr_previous.value()->hn_start);
}
break;
}
case text_anchors::direction::next: {
case direction::next: {
if (neighbors_res->cnr_next) {
return this->line_for_offset(
neighbors_res->cnr_next.value()->hn_start);
Expand Down
24 changes: 24 additions & 0 deletions src/textview_curses.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "base/time_util.hh"
#include "config.h"
#include "data_scanner.hh"
#include "date/solar_hijri.h"
#include "fmt/format.h"
#include "lnav_config.hh"
#include "log_format_fwd.hh"
Expand Down Expand Up @@ -693,6 +694,29 @@ textview_curses::handle_mouse(mouse_event& me)
}
this->tc_selection_start = std::nullopt;
}
if (me.me_button == mouse_button_t::BUTTON_LEFT
&& mouse_line.is<main_content>())
{
const auto& [mc_line] = mouse_line.get<main_content>();
attr_line_t al;

this->textview_value_for_row(mc_line, al);
auto get_res = get_string_attr(al.get_attrs(),
&VC_HYPERLINK,
this->lv_left + me.me_press_x);
if (get_res) {
auto href = get_res.value()->sa_value.get<std::string>();

if (startswith(href, "#")) {
auto* ta
= dynamic_cast<text_anchors*>(this->tc_sub_source);
if (ta != nullptr) {
ta->row_for_anchor(href) |
[this](auto row) { this->set_selection(row); };
}
}
}
}
if (this->tc_delegate != nullptr) {
this->tc_delegate->text_handle_mouse(*this, mouse_line, me);
}
Expand Down

0 comments on commit 50e895c

Please sign in to comment.