Skip to content

Commit a7109a2

Browse files
committed
[spectro] use more colors in the spectro view
Related to #1593
1 parent d7ca34f commit a7109a2

15 files changed

+159
-49
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Interface changes:
185185
row in-between gaps in time. The spacer row shows
186186
bullet points on a log scale to represent the amount
187187
of time in the gap.
188+
* The SPECTRO view now shows the year in timestamps
189+
and uses additional colors to show the value range.
188190
* If there are background tasks, like the processing done
189191
by `:add-source-path`, a panel with progress bars for
190192
each operation will be shown just above the bottom

src/base/color_spaces.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ lab_color::sufficient_contrast(const lab_color& other) const
285285
|| std::signbit(this->lc_b) != std::signbit(other.lc_b));
286286
}
287287

288+
lab_color
289+
lab_color::avg(const lab_color& other) const
290+
{
291+
return lab_color{
292+
(this->lc_l + other.lc_l) / 2.0,
293+
(this->lc_a + other.lc_a) / 2.0,
294+
(this->lc_b + other.lc_b) / 2.0,
295+
};
296+
}
297+
288298
namespace styling {
289299

290300
constexpr color_unit color_unit::EMPTY = color_unit{transparent{}};

src/base/color_spaces.hh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifndef color_spaces_hh
3131
#define color_spaces_hh
3232

33+
#include <optional>
3334
#include <string>
3435
#include <variant>
3536

@@ -90,7 +91,10 @@ struct rgb_color {
9091
};
9192

9293
struct lab_color {
93-
constexpr lab_color() : lc_l(0), lc_a(0), lc_b(0) {}
94+
explicit constexpr lab_color(double l = 0.0, double a = 0.0, double b = 0.0)
95+
: lc_l(l), lc_a(a), lc_b(b)
96+
{
97+
}
9498

9599
explicit lab_color(const rgb_color& rgb);
96100

@@ -100,6 +104,8 @@ struct lab_color {
100104

101105
bool sufficient_contrast(const lab_color& other) const;
102106

107+
lab_color avg(const lab_color& other) const;
108+
103109
lab_color& operator=(const lab_color& other)
104110
{
105111
this->lc_l = other.lc_l;

src/base/lnav.console.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,19 @@ role_to_style(const role_t role,
455455
default_bg_style = fmt::bg(fmt::terminal_color::black);
456456
break;
457457
case role_t::VCR_LOW_THRESHOLD:
458+
case role_t::VCR_SPECTRO_THRESHOLD0:
459+
case role_t::VCR_SPECTRO_THRESHOLD1:
458460
line_style |= fmt::bg(fmt::terminal_color::green);
459461
break;
460462
case role_t::VCR_MED_THRESHOLD:
463+
case role_t::VCR_SPECTRO_THRESHOLD2:
464+
case role_t::VCR_SPECTRO_THRESHOLD3:
465+
case role_t::VCR_SPECTRO_THRESHOLD4:
461466
line_style |= fmt::bg(fmt::terminal_color::yellow);
462467
break;
463468
case role_t::VCR_HIGH_THRESHOLD:
469+
case role_t::VCR_SPECTRO_THRESHOLD5:
470+
case role_t::VCR_SPECTRO_THRESHOLD6:
464471
line_style |= fmt::bg(fmt::terminal_color::red);
465472
break;
466473
default:

src/base/string_attr_type.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ enum class role_t : int32_t {
174174

175175
VCR_OBJECT_KEY,
176176

177+
VCR_SPECTRO_THRESHOLD0,
178+
VCR_SPECTRO_THRESHOLD1,
179+
VCR_SPECTRO_THRESHOLD2,
180+
VCR_SPECTRO_THRESHOLD3,
181+
VCR_SPECTRO_THRESHOLD4,
182+
VCR_SPECTRO_THRESHOLD5,
183+
VCR_SPECTRO_THRESHOLD6,
184+
177185
VCR__MAX
178186
};
179187

src/lnav.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,9 @@ VALUES ('org.lnav.mouse-support', -1, DATETIME('now', '+1 minute'),
16241624
ld_view.tc_on_click = click_handler;
16251625
ld_view.tc_interactive = true;
16261626
}
1627+
lnav_data.ld_views[LNV_SPECTRO].tc_cursor_role = std::nullopt;
1628+
lnav_data.ld_views[LNV_SPECTRO].tc_disabled_cursor_role = std::nullopt;
1629+
16271630
lnav_data.ld_views[LNV_DB].set_supports_marks(true);
16281631
lnav_data.ld_views[LNV_HELP].set_supports_marks(true);
16291632
lnav_data.ld_views[LNV_HISTOGRAM].set_supports_marks(true);

src/lnav.prompt.cc

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,33 +1145,27 @@ prompt::get_cmd_parameter_completion(textview_curses& tc,
11451145
std::unordered_set<std::string> field_names;
11461146
auto* tss = tc.get_sub_source();
11471147
auto* dls = dynamic_cast<db_label_source*>(tss);
1148+
auto* lss = dynamic_cast<logfile_sub_source*>(tss);
1149+
auto sel_opt = tc.get_selection();
11481150
if (dls != nullptr) {
11491151
for (const auto& hdr : dls->dls_headers) {
11501152
if (!hdr.is_graphable()) {
11511153
continue;
11521154
}
11531155
field_names.emplace(hdr.hm_name);
11541156
}
1155-
} else {
1156-
tc.map_top_row([&field_names](const auto& al) {
1157-
auto attr_opt = get_string_attr(al.al_attrs, SA_FORMAT);
1158-
if (attr_opt) {
1159-
auto format_name = attr_opt->get();
1160-
auto format = log_format::find_root_format(
1161-
format_name.c_str());
1162-
if (format) {
1163-
for (const auto& lvm :
1164-
format->get_value_metadata())
1165-
{
1166-
if (lvm.is_numeric()) {
1167-
field_names.emplace(
1168-
lvm.lvm_name.to_string());
1169-
}
1170-
}
1157+
} else if (lss != nullptr && sel_opt) {
1158+
auto win = lss->window_at(sel_opt.value());
1159+
for (const auto& log_msg : *win) {
1160+
for (const auto& lvm : log_msg.get_file_ptr()
1161+
->get_format()
1162+
->get_value_metadata())
1163+
{
1164+
if (lvm.is_numeric()) {
1165+
field_names.emplace(lvm.lvm_name.to_string());
11711166
}
11721167
}
1173-
return std::nullopt;
1174-
});
1168+
}
11751169
}
11761170

11771171
if (field_names.empty()) {

src/log_format_impls.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ class bro_log_format : public log_format {
608608
this->blf_field_defs.clear();
609609
}
610610

611+
std::vector<logline_value_meta> get_value_metadata() const override
612+
{
613+
std::vector<logline_value_meta> retval;
614+
615+
for (const auto& fd : this->blf_field_defs) {
616+
retval.emplace_back(fd.fd_meta);
617+
}
618+
return retval;
619+
}
620+
611621
scan_result_t scan_int(std::vector<logline>& dst,
612622
const line_info& li,
613623
shared_buffer_ref& sbr,
@@ -1404,6 +1414,16 @@ class w3c_log_format : public log_format {
14041414
this->wlf_field_defs.clear();
14051415
}
14061416

1417+
std::vector<logline_value_meta> get_value_metadata() const override
1418+
{
1419+
std::vector<logline_value_meta> retval;
1420+
1421+
for (const auto& fd : this->wlf_field_defs) {
1422+
retval.emplace_back(fd.fd_meta);
1423+
}
1424+
return retval;
1425+
}
1426+
14071427
scan_result_t scan_int(std::vector<logline>& dst,
14081428
const line_info& li,
14091429
shared_buffer_ref& sbr,

src/spectro_source.cc

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ spectrogram_source::list_value_for_overlay(const listview_curses& lv,
241241
auto mark_offset = TIME_COLUMN_WIDTH + this->ss_cursor_column.value();
242242
auto mark_is_before = true;
243243

244-
retval.al_attrs.emplace_back(line_range{0, -1},
245-
VC_ROLE.value(role_t::VCR_STATUS_INFO));
246244
if (desc.length() + 8 > (ssize_t) width) {
247245
desc.clear();
248246
}
@@ -259,6 +257,7 @@ spectrogram_source::list_value_for_overlay(const listview_curses& lv,
259257
if (!mark_is_before) {
260258
retval.append("\u25b2 ");
261259
}
260+
retval.with_attr_for_all(VC_ROLE.value(role_t::VCR_CURSOR_LINE));
262261

263262
if (this->ss_details_view != nullptr) {
264263
if (s_row.sr_details_source_provider) {
@@ -411,18 +410,16 @@ spectrogram_source::chart_attrs_for_line(textview_curses& tc,
411410
continue;
412411
}
413412

414-
role_t role;
415-
416-
if (col_value < st.st_green_threshold) {
417-
role = role_t::VCR_LOW_THRESHOLD;
418-
} else if (col_value < st.st_yellow_threshold) {
419-
role = role_t::VCR_MED_THRESHOLD;
420-
} else {
421-
role = role_t::VCR_HIGH_THRESHOLD;
422-
}
413+
auto role = lnav::enums::to_underlying(role_t::VCR_SPECTRO_THRESHOLD0);
414+
auto t_iter = std::lower_bound(std::begin(st.st_thresholds),
415+
std::end(st.st_thresholds),
416+
col_value);
417+
auto dist = std::distance(std::begin(st.st_thresholds), t_iter);
418+
role += dist;
419+
ensure(role < (int) role_t::VCR__MAX);
423420
auto lr
424421
= line_range{TIME_COLUMN_WIDTH + lpc, TIME_COLUMN_WIDTH + lpc + 1};
425-
value_out.emplace_back(lr, VC_ROLE.value(role));
422+
value_out.emplace_back(lr, VC_ROLE.value(role_t{role}));
426423
}
427424
}
428425

@@ -545,6 +542,8 @@ spectrogram_source::cache_bounds()
545542
return;
546543
}
547544

545+
auto [height, width] = this->tss_view->get_dimensions();
546+
width -= UNUSABLE_WIDTH;
548547
auto grain_begin_time = rounddown(sb.sb_begin_time, this->ss_granularity);
549548
auto grain_end_time = roundup_size(sb.sb_end_time, this->ss_granularity);
550549

@@ -555,19 +554,21 @@ spectrogram_source::cache_bounds()
555554
int64_t samples_per_row = sb.sb_count / this->ss_cached_line_count;
556555
auto& st = this->ss_cached_thresholds;
557556

558-
st.st_yellow_threshold = samples_per_row / 4;
559-
st.st_green_threshold = st.st_yellow_threshold / 2;
560-
561-
if (st.st_green_threshold <= 1) {
562-
st.st_green_threshold = 2;
563-
}
564-
if (st.st_yellow_threshold <= st.st_green_threshold) {
565-
st.st_yellow_threshold = st.st_green_threshold + 1;
557+
st.st_thresholds[0] = samples_per_row / width;
558+
st.st_thresholds[1] = samples_per_row / (width / 2);
559+
st.st_thresholds[2] = samples_per_row / (width / 4);
560+
st.st_thresholds[3] = samples_per_row / (width / 5);
561+
st.st_thresholds[4] = samples_per_row / (width / 6);
562+
st.st_thresholds[5] = samples_per_row / (width / 8);
563+
st.st_thresholds[6] = std::numeric_limits<int>::max();
564+
for (size_t lpc = 0; lpc < 6; lpc++) {
565+
if (st.st_thresholds[lpc] < lpc + 1) {
566+
st.st_thresholds[lpc] = lpc + 1;
567+
}
566568
}
567569

568570
auto& bm = this->tss_view->get_bookmarks()[&textview_curses::BM_USER];
569571
bm.clear();
570-
auto [height, width] = this->tss_view->get_dimensions();
571572
for (auto row = 0_vl; row < this->ss_cached_line_count; row += 1_vl) {
572573
spectrogram_request sr(sb);
573574

@@ -699,12 +700,12 @@ spectrogram_source::list_static_overlay(const listview_curses& lv,
699700
ANSI_ROLE(" ") " 1-%'d " ANSI_ROLE(" ") " %'d-%'d " ANSI_ROLE(
700701
" ") " %'d+",
701702
lnav::enums::to_underlying(role_t::VCR_LOW_THRESHOLD),
702-
st.st_green_threshold - 1,
703+
st.st_thresholds[0],
703704
lnav::enums::to_underlying(role_t::VCR_MED_THRESHOLD),
704-
st.st_green_threshold,
705-
st.st_yellow_threshold - 1,
705+
st.st_thresholds[0],
706+
st.st_thresholds[3],
706707
lnav::enums::to_underlying(role_t::VCR_HIGH_THRESHOLD),
707-
st.st_yellow_threshold);
708+
st.st_thresholds[5]);
708709
auto buflen = strlen(buf);
709710
if (line.length() + buflen + 20 < width) {
710711
line.append(width / 2 - buflen / 3 - line.length(), ' ');

src/spectro_source.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ struct spectrogram_bounds {
5656
};
5757

5858
struct spectrogram_thresholds {
59-
int st_green_threshold{0};
60-
int st_yellow_threshold{0};
59+
int st_thresholds[7]{};
6160
};
6261

6362
struct spectrogram_request {

0 commit comments

Comments
 (0)