• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.0
/src/textfile_sub_source.cc
1
/**
2
 * Copyright (c) 2020, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#include <chrono>
31

32
#include "textfile_sub_source.hh"
33

34
#include <date/date.h>
35

36
#include "base/ansi_scrubber.hh"
37
#include "base/attr_line.builder.hh"
38
#include "base/fs_util.hh"
39
#include "base/injector.hh"
40
#include "base/itertools.hh"
41
#include "base/map_util.hh"
42
#include "base/math_util.hh"
43
#include "bound_tags.hh"
44
#include "config.h"
45
#include "data_scanner.hh"
46
#include "lnav.events.hh"
47
#include "md2attr_line.hh"
48
#include "msg.text.hh"
49
#include "pretty_printer.hh"
50
#include "readline_highlighters.hh"
51
#include "scn/scan.h"
52
#include "sql_util.hh"
53
#include "sqlitepp.hh"
54
#include "textfile_sub_source.cfg.hh"
55
#include "yajlpp/yajlpp_def.hh"
56

57
using namespace lnav::roles::literals;
58

59
static bool
60
file_needs_reformatting(const std::shared_ptr<logfile> lf)
465✔
61
{
62
    static const auto& cfg = injector::get<const lnav::textfile::config&>();
465✔
63

64
    switch (lf->get_text_format()) {
465✔
65
        case text_format_t::TF_BINARY:
33✔
66
        case text_format_t::TF_DIFF:
67
            return false;
33✔
68
        default:
432✔
69
            if (lf->get_longest_line_length()
432✔
70
                > cfg.c_max_unformatted_line_length)
432✔
71
            {
72
                return true;
23✔
73
            }
74
            return false;
409✔
75
    }
76
}
77

78
size_t
79
textfile_sub_source::text_line_count()
14,765✔
80
{
81
    size_t retval = 0;
14,765✔
82

83
    if (!this->tss_files.empty()) {
14,765✔
84
        const auto curr_iter = this->current_file_state();
5,417✔
85
        if (this->tss_view_mode == view_mode::raw
10,834✔
86
            || !curr_iter->fvs_text_source)
5,417✔
87
        {
88
            const auto& lf = curr_iter->fvs_file;
4,071✔
89
            if (lf->get_text_format() == text_format_t::TF_BINARY) {
4,071✔
90
                const auto fsize = lf->get_content_size();
1,545✔
91
                retval = fsize / 16;
1,545✔
92
                if (fsize % 16) {
1,545✔
93
                    retval += 1;
1,545✔
94
                }
95
            } else {
96
                auto* lfo = (line_filter_observer*) lf->get_logline_observer();
2,526✔
97
                if (lfo != nullptr) {
2,526✔
98
                    retval = lfo->lfo_filter_state.tfs_index.size();
2,526✔
99
                }
100
            }
101
        } else {
102
            retval = curr_iter->fvs_text_source->text_line_count();
1,346✔
103
        }
104
    }
105

106
    return retval;
14,765✔
107
}
108

109
line_info
110
textfile_sub_source::text_value_for_line(textview_curses& tc,
3,141✔
111
                                         int line,
112
                                         std::string& value_out,
113
                                         text_sub_source::line_flags_t flags)
114
{
115
    if (this->tss_files.empty() || line < 0) {
3,141✔
UNCOV
116
        value_out.clear();
×
UNCOV
117
        return {};
×
118
    }
119

120
    const auto curr_iter = this->current_file_state();
3,141✔
121
    const auto& lf = curr_iter->fvs_file;
3,141✔
122
    if (this->tss_view_mode == view_mode::rendered
6,282✔
123
        && curr_iter->fvs_text_source)
3,141✔
124
    {
125
        curr_iter->fvs_text_source->text_value_for_line(
1,038✔
126
            tc, line, value_out, flags);
127
        return {};
1,038✔
128
    }
129

130
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
2,103✔
131
        this->tss_hex_line.clear();
1,430✔
132
        auto fsize = lf->get_content_size();
1,430✔
133
        auto fr = file_range{line * 16};
1,430✔
134
        fr.fr_size = std::min((file_ssize_t) 16, fsize - fr.fr_offset);
1,430✔
135

136
        auto read_res = lf->read_range(fr);
1,430✔
137
        if (read_res.isErr()) {
1,430✔
UNCOV
138
            log_error("%s: failed to read range %lld:%lld -- %s",
×
139
                      lf->get_path_for_key().c_str(),
140
                      fr.fr_offset,
141
                      fr.fr_size,
142
                      read_res.unwrapErr().c_str());
UNCOV
143
            return {};
×
144
        }
145

146
        auto sbr = read_res.unwrap();
1,430✔
147
        auto sf = sbr.to_string_fragment();
1,430✔
148
        attr_line_builder alb(this->tss_hex_line);
1,430✔
149
        {
150
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
1,430✔
151
            alb.appendf(FMT_STRING("{: >16x} "), fr.fr_offset);
4,290✔
152
        }
1,430✔
153
        alb.append_as_hexdump(sf);
1,430✔
154
        auto alt_row_index = line % 4;
1,430✔
155
        if (alt_row_index == 2 || alt_row_index == 3) {
1,430✔
156
            this->tss_hex_line.with_attr_for_all(
712✔
157
                VC_ROLE.value(role_t::VCR_ALT_ROW));
1,424✔
158
        }
159

160
        value_out = this->tss_hex_line.get_string();
1,430✔
161
        return {};
1,430✔
162
    }
1,430✔
163

164
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
673✔
165
    if (lfo == nullptr
673✔
166
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
673✔
167
    {
UNCOV
168
        value_out.clear();
×
UNCOV
169
        return {};
×
170
    }
171

172
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
673✔
173
    auto read_result = lf->read_line(ll);
673✔
174
    this->tss_line_indent_size = 0;
673✔
175
    this->tss_plain_line_attrs.clear();
673✔
176
    if (read_result.isOk()) {
673✔
177
        auto sbr = read_result.unwrap();
673✔
178
        value_out = to_string(sbr);
673✔
179
        if (sbr.get_metadata().m_has_ansi) {
673✔
180
            scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
35✔
181
        }
182
        for (const auto& ch : value_out) {
1,857✔
183
            if (ch == ' ') {
1,725✔
184
                this->tss_line_indent_size += 1;
1,182✔
185
            } else if (ch == '\t') {
543✔
186
                do {
187
                    this->tss_line_indent_size += 1;
15✔
188
                } while (this->tss_line_indent_size % 8);
15✔
189
            } else {
190
                break;
541✔
191
            }
192
        }
193
        if (lf->has_line_metadata() && this->tas_display_time_offset) {
673✔
194
            auto relstr = this->get_time_offset_for_line(tc, vis_line_t(line));
1✔
195
            value_out
196
                = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
4✔
197
        }
1✔
198
    }
673✔
199

200
    return {};
673✔
201
}
673✔
202

203
void
204
textfile_sub_source::text_attrs_for_line(textview_curses& tc,
3,141✔
205
                                         int row,
206
                                         string_attrs_t& value_out)
207
{
208
    const auto curr_iter = this->current_file_state();
3,141✔
209
    if (curr_iter == this->tss_files.end()) {
3,141✔
UNCOV
210
        return;
×
211
    }
212
    const auto& lf = curr_iter->fvs_file;
3,141✔
213

214
    auto lr = line_range{0, -1};
3,141✔
215
    if (this->tss_view_mode == view_mode::rendered
6,282✔
216
        && curr_iter->fvs_text_source)
3,141✔
217
    {
218
        curr_iter->fvs_text_source->text_attrs_for_line(tc, row, value_out);
1,038✔
219
    } else if (lf->get_text_format() == text_format_t::TF_BINARY) {
2,103✔
220
        value_out = this->tss_hex_line.get_attrs();
1,430✔
221
    } else {
222
        value_out = this->tss_plain_line_attrs;
673✔
223
        auto* lfo
224
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
673✔
225
        if (lfo != nullptr && row >= 0
673✔
226
            && row < (ssize_t) lfo->lfo_filter_state.tfs_index.size())
1,346✔
227
        {
228
            auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[row];
673✔
229

230
            value_out.emplace_back(lr, SA_LEVEL.value(ll->get_msg_level()));
673✔
231
            if (lf->has_line_metadata() && this->tas_display_time_offset) {
673✔
232
                auto time_offset_end = 13;
1✔
233
                lr.lr_start = 0;
1✔
234
                lr.lr_end = time_offset_end;
1✔
235

236
                shift_string_attrs(value_out, 0, time_offset_end);
1✔
237

238
                value_out.emplace_back(lr,
1✔
239
                                       VC_ROLE.value(role_t::VCR_OFFSET_TIME));
2✔
240
                value_out.emplace_back(line_range(12, 13),
1✔
241
                                       VC_GRAPHIC.value(NCACS_VLINE));
2✔
242

243
                auto bar_role = role_t::VCR_NONE;
1✔
244

245
                switch (this->get_line_accel_direction(vis_line_t(row))) {
1✔
246
                    case log_accel::direction_t::A_STEADY:
1✔
247
                        break;
1✔
UNCOV
248
                    case log_accel::direction_t::A_DECEL:
×
UNCOV
249
                        bar_role = role_t::VCR_DIFF_DELETE;
×
250
                        break;
×
251
                    case log_accel::direction_t::A_ACCEL:
×
UNCOV
252
                        bar_role = role_t::VCR_DIFF_ADD;
×
UNCOV
253
                        break;
×
254
                }
255
                if (bar_role != role_t::VCR_NONE) {
1✔
UNCOV
256
                    value_out.emplace_back(line_range(12, 13),
×
UNCOV
257
                                           VC_ROLE.value(bar_role));
×
258
                }
259
            }
260

261
            if (curr_iter->fvs_metadata.m_sections_root) {
673✔
262
                auto ll_next_iter = ll + 1;
673✔
263
                auto end_offset = (ll_next_iter == lf->end())
673✔
264
                    ? lf->get_index_size() - 1
1,301✔
265
                    : ll_next_iter->get_offset() - 1;
628✔
266
                const auto& meta = curr_iter->fvs_metadata;
673✔
267
                meta.m_section_types_tree.visit_overlapping(
1,346✔
268
                    lf->get_line_content_offset(ll),
673✔
269
                    end_offset,
270
                    [&value_out, &ll, &lf, end_offset](const auto& iv) {
100✔
271
                        auto ll_offset = lf->get_line_content_offset(ll);
100✔
272
                        auto lr = line_range{0, -1};
100✔
273
                        if (iv.start > ll_offset) {
100✔
274
                            lr.lr_start = iv.start - ll_offset;
1✔
275
                        }
276
                        if (iv.stop < end_offset) {
100✔
UNCOV
277
                            lr.lr_end = iv.stop - ll_offset;
×
278
                        } else {
279
                            lr.lr_end = end_offset - ll_offset;
100✔
280
                        }
281
                        auto role = role_t::VCR_NONE;
100✔
282
                        switch (iv.value) {
100✔
283
                            case lnav::document::section_types_t::comment:
29✔
284
                                role = role_t::VCR_COMMENT;
29✔
285
                                break;
29✔
286
                            case lnav::document::section_types_t::
71✔
287
                                multiline_string:
288
                                role = role_t::VCR_STRING;
71✔
289
                                break;
71✔
290
                        }
291
                        value_out.emplace_back(lr, VC_ROLE.value(role));
100✔
292
                    });
100✔
293
                for (const auto& indent : meta.m_indents) {
2,056✔
294
                    if (indent < this->tss_line_indent_size) {
1,383✔
295
                        auto guide_lr = line_range{
296
                            (int) indent,
148✔
297
                            (int) (indent + 1),
148✔
298
                            line_range::unit::codepoint,
299
                        };
148✔
300
                        if (this->tas_display_time_offset) {
148✔
UNCOV
301
                            guide_lr.shift(0, 13);
×
302
                        }
303
                        value_out.emplace_back(
148✔
304
                            guide_lr,
305
                            VC_BLOCK_ELEM.value(block_elem_t{
296✔
306
                                L'\u258f', role_t::VCR_INDENT_GUIDE}));
307
                    }
308
                }
309
            }
310
        }
311
    }
312

313
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
3,141✔
314
}
315

316
size_t
317
textfile_sub_source::text_size_for_line(textview_curses& tc,
×
318
                                        int line,
319
                                        text_sub_source::line_flags_t flags)
320
{
321
    size_t retval = 0;
×
322

323
    if (!this->tss_files.empty()) {
×
UNCOV
324
        const auto curr_iter = this->current_file_state();
×
325
        const auto& lf = curr_iter->fvs_file;
×
326
        if (this->tss_view_mode == view_mode::raw
×
327
            || !curr_iter->fvs_text_source)
×
328
        {
UNCOV
329
            auto* lfo = dynamic_cast<line_filter_observer*>(
×
UNCOV
330
                lf->get_logline_observer());
×
UNCOV
331
            if (lfo == nullptr || line < 0
×
332
                || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
×
333
            {
334
            } else {
335
                auto read_res = lf->read_line(
336
                    lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
×
337
                if (read_res.isOk()) {
×
UNCOV
338
                    auto sbr = read_res.unwrap();
×
UNCOV
339
                    auto str = to_string(sbr);
×
UNCOV
340
                    scrub_ansi_string(str, nullptr);
×
341
                    retval = string_fragment::from_str(str).column_width();
×
342
                }
343
            }
344
        } else {
UNCOV
345
            retval = curr_iter->fvs_text_source->text_size_for_line(
×
346
                tc, line, flags);
347
        }
348
    }
349

350
    return retval;
×
351
}
352

353
void
354
textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
×
355
{
356
    const auto iter
357
        = std::find(this->tss_files.begin(), this->tss_files.end(), lf);
×
358
    if (iter == this->tss_files.end()) {
×
359
        return;
×
360
    }
361
    this->tss_files.front().save_from(*this->tss_view);
×
362
    auto fvs = std::move(*iter);
×
363
    this->tss_files.erase(iter);
×
UNCOV
364
    this->tss_files.emplace_front(std::move(fvs));
×
UNCOV
365
    this->set_time_offset(false);
×
UNCOV
366
    fvs.load_into(*this->tss_view);
×
367
    this->tss_view->reload_data();
×
368
}
369

370
void
371
textfile_sub_source::rotate_left()
×
372
{
373
    if (this->tss_files.size() > 1) {
×
374
        this->tss_files.emplace_back(std::move(this->tss_files.front()));
×
375
        this->tss_files.pop_front();
×
376
        this->tss_files.back().save_from(*this->tss_view);
×
UNCOV
377
        this->tss_files.front().load_into(*this->tss_view);
×
UNCOV
378
        this->set_time_offset(false);
×
UNCOV
379
        this->tss_view->reload_data();
×
UNCOV
380
        this->tss_view->redo_search();
×
381
        this->tss_view->set_needs_update();
×
382
    }
383
}
384

385
void
386
textfile_sub_source::rotate_right()
×
387
{
388
    if (this->tss_files.size() > 1) {
×
389
        this->tss_files.front().save_from(*this->tss_view);
×
390
        auto fvs = std::move(this->tss_files.back());
×
UNCOV
391
        this->tss_files.emplace_front(std::move(fvs));
×
UNCOV
392
        this->tss_files.pop_back();
×
UNCOV
393
        this->tss_files.front().load_into(*this->tss_view);
×
UNCOV
394
        this->set_time_offset(false);
×
UNCOV
395
        this->tss_view->reload_data();
×
UNCOV
396
        this->tss_view->redo_search();
×
UNCOV
397
        this->tss_view->set_needs_update();
×
398
    }
399
}
400

401
void
402
textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
482✔
403
{
404
    auto iter = std::find(this->tss_files.begin(), this->tss_files.end(), lf);
482✔
405
    if (iter != this->tss_files.end()) {
482✔
UNCOV
406
        this->tss_files.erase(iter);
×
UNCOV
407
        this->detach_observer(lf);
×
408
    }
409
    this->set_time_offset(false);
482✔
410
    if (!this->tss_files.empty()) {
482✔
UNCOV
411
        this->tss_files.front().load_into(*this->tss_view);
×
412
    }
413
    this->tss_view->reload_data();
482✔
414
}
482✔
415

416
void
417
textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
575✔
418
{
419
    auto* lfo = new line_filter_observer(this->get_filters(), lf);
575✔
420
    lf->set_logline_observer(lfo);
575✔
421
    this->tss_files.emplace_back(lf);
575✔
422
}
575✔
423

424
void
425
textfile_sub_source::text_filters_changed()
9✔
426
{
427
    auto lf = this->current_file();
9✔
428
    if (lf == nullptr || lf->get_text_format() == text_format_t::TF_BINARY) {
9✔
429
        return;
6✔
430
    }
431

432
    auto* lfo = (line_filter_observer*) lf->get_logline_observer();
3✔
433
    uint32_t filter_in_mask, filter_out_mask;
434

435
    lfo->clear_deleted_filter_state();
3✔
436
    lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
3✔
437

438
    this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
3✔
439
    lfo->lfo_filter_state.tfs_index.clear();
3✔
440
    for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
18✔
441
        if (this->tss_apply_filters) {
15✔
442
            if (lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
15✔
443
                continue;
5✔
444
            }
445
            if (lf->has_line_metadata()) {
10✔
UNCOV
446
                auto ll = lf->begin() + lpc;
×
UNCOV
447
                if (ll->get_timeval() < this->ttt_min_row_time) {
×
UNCOV
448
                    continue;
×
449
                }
UNCOV
450
                if (this->ttt_max_row_time < ll->get_timeval()) {
×
UNCOV
451
                    continue;
×
452
                }
453
            }
454
        }
455
        lfo->lfo_filter_state.tfs_index.push_back(lpc);
10✔
456
    }
457

458
    this->tss_view->redo_search();
3✔
459

460
    auto iter = std::lower_bound(lfo->lfo_filter_state.tfs_index.begin(),
3✔
461
                                 lfo->lfo_filter_state.tfs_index.end(),
462
                                 this->tss_content_line);
3✔
463
    auto vl = vis_line_t(
464
        std::distance(lfo->lfo_filter_state.tfs_index.begin(), iter));
6✔
465
    this->tss_view->set_selection(vl);
3✔
466
}
9✔
467

468
void
469
textfile_sub_source::scroll_invoked(textview_curses* tc)
1,756✔
470
{
471
    const auto curr_iter = this->current_file_state();
1,756✔
472
    if (curr_iter == this->tss_files.end()
1,756✔
473
        || curr_iter->fvs_file->get_text_format() == text_format_t::TF_BINARY)
1,756✔
474
    {
475
        return;
1,626✔
476
    }
477

478
    const auto& lf = curr_iter->fvs_file;
176✔
479
    if (this->tss_view_mode == view_mode::rendered
352✔
480
        && curr_iter->fvs_text_source)
176✔
481
    {
482
        return;
31✔
483
    }
484

485
    auto line = tc->get_selection();
145✔
486
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
145✔
487
    if (!line || lfo == nullptr || line < 0_vl
288✔
488
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
288✔
489
    {
490
        return;
15✔
491
    }
492

493
    this->tss_content_line = lfo->lfo_filter_state.tfs_index[line.value()];
130✔
494
}
495

496
int
497
textfile_sub_source::get_filtered_count() const
169✔
498
{
499
    const auto curr_iter = this->current_file_state();
169✔
500
    int retval = 0;
169✔
501

502
    if (curr_iter != this->tss_files.end()) {
169✔
503
        if (this->tss_view_mode == view_mode::raw
222✔
504
            || !curr_iter->fvs_text_source)
111✔
505
        {
506
            const auto& lf = curr_iter->fvs_file;
92✔
507
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
92✔
508
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
92✔
509
        }
510
    }
511
    return retval;
169✔
512
}
513

514
int
UNCOV
515
textfile_sub_source::get_filtered_count_for(size_t filter_index) const
×
516
{
UNCOV
517
    std::shared_ptr<logfile> lf = this->current_file();
×
518

UNCOV
519
    if (lf == nullptr) {
×
UNCOV
520
        return 0;
×
521
    }
522

UNCOV
523
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
UNCOV
524
    return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
×
525
}
526

527
text_format_t
528
textfile_sub_source::get_text_format() const
2,722✔
529
{
530
    if (this->tss_files.empty()) {
2,722✔
UNCOV
531
        return text_format_t::TF_UNKNOWN;
×
532
    }
533

534
    return this->tss_files.front().fvs_file->get_text_format();
2,722✔
535
}
536

537
static attr_line_t
538
to_display(const std::shared_ptr<logfile>& lf)
20✔
539
{
540
    attr_line_t retval;
20✔
541

542
    if (lf->get_open_options().loo_piper) {
20✔
543
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
UNCOV
544
            retval.append("\u21bb "_list_glyph);
×
545
        }
546
    }
547
    retval.append(lf->get_unique_path());
20✔
548

549
    return retval;
20✔
UNCOV
550
}
×
551

552
void
553
textfile_sub_source::text_crumbs_for_line(
10✔
554
    int line, std::vector<breadcrumb::crumb>& crumbs)
555
{
556
    text_sub_source::text_crumbs_for_line(line, crumbs);
10✔
557

558
    if (this->empty()) {
10✔
UNCOV
559
        return;
×
560
    }
561

562
    const auto curr_iter = this->current_file_state();
10✔
563
    const auto& lf = curr_iter->fvs_file;
10✔
564
    crumbs.emplace_back(
10✔
565
        lf->get_unique_path(),
10✔
566
        to_display(lf),
10✔
UNCOV
567
        [this]() {
×
568
            return this->tss_files | lnav::itertools::map([](const auto& lf) {
20✔
569
                       return breadcrumb::possibility{
570
                           lf.fvs_file->get_path_for_key(),
10✔
571
                           to_display(lf.fvs_file),
10✔
572
                       };
10✔
573
                   });
20✔
574
        },
575
        [this](const auto& key) {
10✔
UNCOV
576
            auto lf_opt = this->tss_files
×
UNCOV
577
                | lnav::itertools::map([](const auto& x) { return x.fvs_file; })
×
UNCOV
578
                | lnav::itertools::find_if([&key](const auto& elem) {
×
579
                              return key.template get<std::string>()
UNCOV
580
                                  == elem->get_path_for_key();
×
581
                          })
UNCOV
582
                | lnav::itertools::deref();
×
583

UNCOV
584
            if (!lf_opt) {
×
UNCOV
585
                return;
×
586
            }
587

UNCOV
588
            this->to_front(lf_opt.value());
×
UNCOV
589
            this->tss_view->reload_data();
×
UNCOV
590
        });
×
591
    if (lf->size() == 0) {
10✔
UNCOV
592
        return;
×
593
    }
594

595
    if (lf->has_line_metadata()) {
10✔
596
        auto* lfo
597
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
598
        if (line < 0
2✔
599
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
2✔
600
        {
UNCOV
601
            return;
×
602
        }
603
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
2✔
604
        char ts[64];
605

606
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
607

608
        crumbs.emplace_back(
2✔
609
            std::string(ts),
4✔
610
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
611
            [](const auto& key) {});
2✔
612
    }
613

614
    if (this->tss_view_mode == view_mode::rendered
20✔
615
        && curr_iter->fvs_text_source)
10✔
616
    {
617
        curr_iter->fvs_text_source->text_crumbs_for_line(line, crumbs);
2✔
618
    } else if (curr_iter->fvs_metadata.m_sections_tree.empty()) {
8✔
619
    } else {
620
        auto* lfo
621
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
5✔
622
        if (line < 0
5✔
623
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
5✔
624
        {
UNCOV
625
            return;
×
626
        }
627
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
5✔
628
        auto ll_next_iter = ll_iter + 1;
5✔
629
        auto end_offset = (ll_next_iter == lf->end())
5✔
630
            ? lf->get_index_size() - 1
10✔
631
            : ll_next_iter->get_offset() - 1;
5✔
632
        const auto initial_size = crumbs.size();
5✔
633

634
        curr_iter->fvs_metadata.m_sections_tree.visit_overlapping(
5✔
635
            lf->get_line_content_offset(ll_iter),
5✔
636
            end_offset,
637
            [&crumbs, initial_size, meta = &curr_iter->fvs_metadata, this, lf](
10✔
638
                const auto& iv) {
639
                auto path = crumbs | lnav::itertools::skip(initial_size)
24✔
640
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
16✔
641
                    | lnav::itertools::append(iv.value);
8✔
642
                auto curr_node = lnav::document::hier_node::lookup_path(
8✔
643
                    meta->m_sections_root.get(), path);
8✔
644
                crumbs.emplace_back(
16✔
645
                    iv.value,
8✔
646
                    [meta, path]() { return meta->possibility_provider(path); },
16✔
647
                    [this, curr_node, path, lf](const auto& key) {
16✔
648
                        if (!curr_node) {
×
UNCOV
649
                            return;
×
650
                        }
651
                        auto* parent_node = curr_node.value()->hn_parent;
×
652
                        if (parent_node == nullptr) {
×
UNCOV
653
                            return;
×
654
                        }
655
                        key.match(
×
656
                            [this, parent_node](const std::string& str) {
×
657
                                auto sib_iter
UNCOV
658
                                    = parent_node->hn_named_children.find(str);
×
UNCOV
659
                                if (sib_iter
×
UNCOV
660
                                    == parent_node->hn_named_children.end()) {
×
UNCOV
661
                                    return;
×
662
                                }
UNCOV
663
                                this->set_top_from_off(
×
UNCOV
664
                                    sib_iter->second->hn_start);
×
665
                            },
UNCOV
666
                            [this, parent_node](size_t index) {
×
UNCOV
667
                                if (index >= parent_node->hn_children.size()) {
×
UNCOV
668
                                    return;
×
669
                                }
670
                                auto sib
UNCOV
671
                                    = parent_node->hn_children[index].get();
×
UNCOV
672
                                this->set_top_from_off(sib->hn_start);
×
673
                            });
674
                    });
675
                if (curr_node
8✔
676
                    && curr_node.value()->hn_parent->hn_children.size()
16✔
677
                        != curr_node.value()
8✔
678
                               ->hn_parent->hn_named_children.size())
8✔
679
                {
680
                    auto node = lnav::document::hier_node::lookup_path(
1✔
681
                        meta->m_sections_root.get(), path);
1✔
682

683
                    crumbs.back().c_expected_input
1✔
684
                        = curr_node.value()
2✔
685
                              ->hn_parent->hn_named_children.empty()
1✔
686
                        ? breadcrumb::crumb::expected_input_t::index
1✔
687
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
688
                    crumbs.back().with_possible_range(
2✔
689
                        node | lnav::itertools::map([](const auto hn) {
1✔
690
                            return hn->hn_parent->hn_children.size();
1✔
691
                        })
692
                        | lnav::itertools::unwrap_or(size_t{0}));
2✔
693
                }
694
            });
8✔
695

696
        auto path = crumbs | lnav::itertools::skip(initial_size)
10✔
697
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
10✔
698
        auto node = lnav::document::hier_node::lookup_path(
5✔
699
            curr_iter->fvs_metadata.m_sections_root.get(), path);
5✔
700

701
        if (node && !node.value()->hn_children.empty()) {
5✔
702
            auto poss_provider = [curr_node = node.value()]() {
1✔
703
                std::vector<breadcrumb::possibility> retval;
1✔
704
                for (const auto& child : curr_node->hn_named_children) {
7✔
705
                    retval.emplace_back(child.first);
6✔
706
                }
707
                return retval;
1✔
708
            };
709
            auto path_performer = [this, curr_node = node.value()](
2✔
710
                                      const breadcrumb::crumb::key_t& value) {
UNCOV
711
                value.match(
×
UNCOV
712
                    [this, curr_node](const std::string& str) {
×
713
                        auto child_iter
UNCOV
714
                            = curr_node->hn_named_children.find(str);
×
UNCOV
715
                        if (child_iter != curr_node->hn_named_children.end()) {
×
UNCOV
716
                            this->set_top_from_off(
×
UNCOV
717
                                child_iter->second->hn_start);
×
718
                        }
UNCOV
719
                    },
×
UNCOV
720
                    [this, curr_node](size_t index) {
×
UNCOV
721
                        if (index >= curr_node->hn_children.size()) {
×
UNCOV
722
                            return;
×
723
                        }
UNCOV
724
                        auto* child = curr_node->hn_children[index].get();
×
UNCOV
725
                        this->set_top_from_off(child->hn_start);
×
726
                    });
727
            };
1✔
728
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
729
            crumbs.back().c_expected_input
1✔
730
                = node.value()->hn_named_children.empty()
2✔
731
                ? breadcrumb::crumb::expected_input_t::index
1✔
732
                : breadcrumb::crumb::expected_input_t::index_or_exact;
733
        }
734
    }
5✔
735
}
736

737
textfile_sub_source::rescan_result_t
738
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
4,029✔
739
                                  std::optional<ui_clock::time_point> deadline)
740
{
741
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,029✔
742

743
    file_iterator iter;
4,029✔
744
    rescan_result_t retval;
4,029✔
745
    size_t files_scanned = 0;
4,029✔
746

747
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,029✔
748
        return retval;
114✔
749
    }
750

751
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
3,915✔
752

753
    std::vector<std::shared_ptr<logfile>> closed_files;
3,915✔
754
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
5,023✔
755
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,108✔
756
        {
757
            log_info("rescan_files() deadline reached, breaking...");
×
UNCOV
758
            retval.rr_scan_completed = false;
×
UNCOV
759
            this->tss_last_scan_aborted = true;
×
UNCOV
760
            break;
×
761
        }
762

763
        std::shared_ptr<logfile> lf = iter->fvs_file;
1,108✔
764

765
        if (lf->is_closed()) {
1,108✔
766
            iter = this->tss_files.erase(iter);
93✔
767
            this->detach_observer(lf);
93✔
768
            closed_files.emplace_back(lf);
93✔
769
            retval.rr_rescan_needed = true;
93✔
770
            continue;
93✔
771
        }
772

773
        if (last_aborted && lf->size() > 0) {
1,015✔
UNCOV
774
            retval.rr_scan_completed = false;
×
UNCOV
775
            ++iter;
×
UNCOV
776
            continue;
×
777
        }
778
        files_scanned += 1;
1,015✔
779

780
        try {
781
            const auto& st = lf->get_stat();
1,015✔
782
            uint32_t old_size = lf->size();
1,015✔
783
            auto new_text_data = lf->rebuild_index(deadline);
1,015✔
784

785
            if (lf->get_format() != nullptr) {
1,015✔
786
                iter = this->tss_files.erase(iter);
482✔
787
                this->detach_observer(lf);
482✔
788
                callback.promote_file(lf);
482✔
789
                continue;
554✔
790
            }
791

792
            bool new_data = false;
533✔
793
            switch (new_text_data) {
533✔
794
                case logfile::rebuild_result_t::NEW_LINES:
79✔
795
                case logfile::rebuild_result_t::NEW_ORDER:
796
                    new_data = true;
79✔
797
                    retval.rr_new_data += 1;
79✔
798
                    break;
79✔
799
                default:
454✔
800
                    break;
454✔
801
            }
802
            callback.scanned_file(lf);
533✔
803

804
            if (lf->is_indexing()
533✔
805
                && lf->get_text_format() != text_format_t::TF_BINARY)
533✔
806
            {
807
                if (!new_data) {
508✔
808
                    // Only invalidate the meta if the file is small, or we
809
                    // found some meta previously.
810
                    if ((st.st_mtime != iter->fvs_mtime
434✔
811
                         || st.st_size != iter->fvs_file_size
421✔
812
                         || lf->get_index_size() != iter->fvs_file_indexed_size)
421✔
813
                        && (st.st_size < 10 * 1024 || iter->fvs_file_size == 0
855✔
UNCOV
814
                            || !iter->fvs_metadata.m_sections_tree.empty()))
×
815
                    {
816
                        log_debug(
13✔
817
                            "text file has changed, invalidating metadata.  "
818
                            "old: {mtime: %d size: %zu isize: %zu}, new: "
819
                            "{mtime: %d size: %zu isize: %zu}",
820
                            iter->fvs_mtime,
821
                            iter->fvs_file_size,
822
                            iter->fvs_file_indexed_size,
823
                            st.st_mtime,
824
                            st.st_size,
825
                            lf->get_index_size());
826
                        iter->fvs_metadata = {};
13✔
827
                        iter->fvs_error.clear();
13✔
828
                    }
829
                }
830

831
                if (!iter->fvs_metadata.m_sections_root
508✔
832
                    && iter->fvs_error.empty())
508✔
833
                {
834
                    auto read_res
835
                        = lf->read_file(logfile::read_format_t::with_framing);
87✔
836

837
                    if (read_res.isOk()) {
87✔
838
                        auto read_file_res = read_res.unwrap();
87✔
839

840
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf) {
87✔
UNCOV
841
                            log_error(
×
842
                                "%s: file has invalid UTF, skipping meta "
843
                                "discovery",
844
                                lf->get_path_for_key().c_str());
UNCOV
845
                            iter->fvs_mtime = st.st_mtime;
×
UNCOV
846
                            iter->fvs_file_size = lf->get_index_size();
×
847
                        } else {
848
                            auto content
849
                                = attr_line_t(read_file_res.rfr_content);
87✔
850

851
                            log_info("generating metadata for: %s (size=%zu)",
87✔
852
                                     lf->get_path_for_key().c_str(),
853
                                     content.length());
854
                            scrub_ansi_string(content.get_string(),
87✔
855
                                              &content.get_attrs());
87✔
856

857
                            auto text_meta = extract_text_meta(
858
                                content.get_string(), lf->get_text_format());
87✔
859
                            if (text_meta) {
87✔
860
                                lf->set_filename(text_meta->tfm_filename);
3✔
861
                                lf->set_include_in_session(true);
3✔
862
                                callback.renamed_file(lf);
3✔
863
                            }
864

865
                            iter->fvs_mtime = st.st_mtime;
87✔
866
                            iter->fvs_file_size = st.st_size;
87✔
867
                            iter->fvs_file_indexed_size = lf->get_index_size();
87✔
868
                            iter->fvs_metadata
87✔
869
                                = lnav::document::discover(content)
87✔
870
                                      .with_text_format(lf->get_text_format())
87✔
871
                                      .perform();
174✔
872
                        }
87✔
873
                    } else {
87✔
UNCOV
874
                        auto errmsg = read_res.unwrapErr();
×
UNCOV
875
                        log_error(
×
876
                            "%s: unable to read file for meta discover -- %s",
877
                            lf->get_path_for_key().c_str(),
878
                            errmsg.c_str());
UNCOV
879
                        iter->fvs_mtime = st.st_mtime;
×
UNCOV
880
                        iter->fvs_file_size = st.st_size;
×
UNCOV
881
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
UNCOV
882
                        iter->fvs_error = errmsg;
×
883
                    }
884
                }
87✔
885
            }
886

887
            uint32_t filter_in_mask, filter_out_mask;
888

889
            this->get_filters().get_enabled_mask(filter_in_mask,
533✔
890
                                                 filter_out_mask);
891
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
533✔
892
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,043✔
893
                if (this->tss_apply_filters
7,020✔
894
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
3,510✔
895
                {
UNCOV
896
                    continue;
×
897
                }
898
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
3,510✔
899
            }
900

901
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
533✔
902
                if (iter->fvs_text_source) {
68✔
903
                    if (iter->fvs_file_size == st.st_size
57✔
904
                        && iter->fvs_file_indexed_size == lf->get_index_size()
57✔
905
                        && iter->fvs_mtime == st.st_mtime)
114✔
906
                    {
907
                        ++iter;
57✔
908
                        continue;
57✔
909
                    }
UNCOV
910
                    log_info("markdown file has been updated, re-rendering: %s",
×
911
                             lf->get_path_for_key().c_str());
UNCOV
912
                    iter->fvs_text_source = nullptr;
×
913
                }
914

915
                auto read_res = lf->read_file(logfile::read_format_t::plain);
11✔
916
                if (read_res.isOk()) {
11✔
917
                    static const auto FRONT_MATTER_RE
918
                        = lnav::pcre2pp::code::from_const(
919
                            R"((?:^---\n(.*)\n---\n|^\+\+\+\n(.*)\n\+\+\+\n))",
920
                            PCRE2_MULTILINE | PCRE2_DOTALL);
11✔
921
                    thread_local auto md = FRONT_MATTER_RE.create_match_data();
11✔
922

923
                    auto read_file_res = read_res.unwrap();
11✔
924
                    auto content_sf
925
                        = string_fragment::from_str(read_file_res.rfr_content);
11✔
926
                    std::string frontmatter;
11✔
927
                    auto frontmatter_format = text_format_t::TF_UNKNOWN;
11✔
928

929
                    auto cap_res = FRONT_MATTER_RE.capture_from(content_sf)
11✔
930
                                       .into(md)
11✔
931
                                       .matches()
22✔
932
                                       .ignore_error();
11✔
933
                    if (cap_res) {
11✔
UNCOV
934
                        if (md[1]) {
×
UNCOV
935
                            frontmatter_format = text_format_t::TF_YAML;
×
UNCOV
936
                            frontmatter = md[1]->to_string();
×
UNCOV
937
                        } else if (md[2]) {
×
UNCOV
938
                            frontmatter_format = text_format_t::TF_TOML;
×
UNCOV
939
                            frontmatter = md[2]->to_string();
×
940
                        }
UNCOV
941
                        content_sf = cap_res->f_remaining;
×
942
                    } else if (content_sf.startswith("{")) {
11✔
943
                        yajlpp_parse_context ypc(
944
                            intern_string::lookup(lf->get_filename()));
4✔
945
                        auto handle
946
                            = yajlpp::alloc_handle(&ypc.ypc_callbacks, &ypc);
4✔
947

948
                        yajl_config(
4✔
949
                            handle.in(), yajl_allow_trailing_garbage, 1);
950
                        ypc.with_ignore_unused(true)
4✔
951
                            .with_handle(handle.in())
4✔
952
                            .with_error_reporter(
4✔
953
                                [&lf](const auto& ypc, const auto& um) {
4✔
UNCOV
954
                                    log_error(
×
955
                                        "%s: failed to parse JSON front matter "
956
                                        "-- %s",
957
                                        lf->get_filename().c_str(),
958
                                        um.um_reason.al_string.c_str());
UNCOV
959
                                });
×
960
                        if (ypc.parse_doc(content_sf)) {
4✔
961
                            ssize_t consumed = ypc.ypc_total_consumed;
4✔
962
                            if (consumed < content_sf.length()
4✔
963
                                && content_sf[consumed] == '\n')
4✔
964
                            {
965
                                frontmatter_format = text_format_t::TF_JSON;
4✔
966
                                frontmatter = string_fragment::from_str_range(
4✔
967
                                                  read_file_res.rfr_content,
968
                                                  0,
969
                                                  consumed)
970
                                                  .to_string();
4✔
971
                                content_sf = content_sf.substr(consumed);
4✔
972
                            }
973
                        }
974
                    }
4✔
975

976
                    log_info("%s: rendering markdown content of size %d",
11✔
977
                             lf->get_basename().c_str(),
978
                             read_file_res.rfr_content.size());
979
                    md2attr_line mdal;
11✔
980

981
                    mdal.with_source_path(lf->get_actual_path());
11✔
982
                    if (this->tss_view->tc_interactive) {
11✔
UNCOV
983
                        mdal.add_lnav_script_icons();
×
984
                    }
985
                    auto parse_res = md4cpp::parse(content_sf, mdal);
11✔
986

987
                    iter->fvs_mtime = st.st_mtime;
11✔
988
                    iter->fvs_file_indexed_size = lf->get_index_size();
11✔
989
                    iter->fvs_file_size = st.st_size;
11✔
990
                    iter->fvs_text_source
11✔
991
                        = std::make_unique<plain_text_source>();
22✔
992
                    iter->fvs_text_source->set_text_format(
11✔
993
                        lf->get_text_format());
994
                    iter->fvs_text_source->register_view(this->tss_view);
11✔
995
                    if (parse_res.isOk()) {
11✔
996
                        auto& lf_meta = lf->get_embedded_metadata();
11✔
997

998
                        iter->fvs_text_source->replace_with(parse_res.unwrap());
11✔
999

1000
                        if (!frontmatter.empty()) {
11✔
1001
                            lf_meta["net.daringfireball.markdown.frontmatter"]
8✔
1002
                                = {frontmatter_format, frontmatter};
8✔
1003
                        }
1004

1005
                        lnav::events::publish(
11✔
1006
                            lnav_db,
1007
                            lnav::events::file::format_detected{
33✔
1008
                                lf->get_filename(),
11✔
1009
                                fmt::to_string(lf->get_text_format()),
22✔
1010
                            });
1011
                    } else {
1012
                        auto view_content
UNCOV
1013
                            = lnav::console::user_message::error(
×
1014
                                  "unable to parse markdown file")
UNCOV
1015
                                  .with_reason(parse_res.unwrapErr())
×
UNCOV
1016
                                  .to_attr_line();
×
UNCOV
1017
                        view_content.append("\n").append(
×
UNCOV
1018
                            attr_line_t::from_ansi_str(
×
1019
                                read_file_res.rfr_content.c_str()));
1020

UNCOV
1021
                        iter->fvs_text_source->replace_with(view_content);
×
1022
                    }
1023
                } else {
11✔
UNCOV
1024
                    log_error("unable to read markdown file: %s -- %s",
×
1025
                              lf->get_path_for_key().c_str(),
1026
                              read_res.unwrapErr().c_str());
1027
                }
1028
            } else if (file_needs_reformatting(lf) && !new_data) {
476✔
1029
                if (iter->fvs_file_size == st.st_size
19✔
1030
                    && iter->fvs_file_indexed_size == lf->get_index_size()
19✔
1031
                    && iter->fvs_mtime == st.st_mtime
19✔
1032
                    && (!iter->fvs_error.empty()
57✔
1033
                        || iter->fvs_text_source != nullptr))
19✔
1034
                {
1035
                    ++iter;
15✔
1036
                    continue;
15✔
1037
                }
1038
                log_info("pretty file has been updated, re-rendering: %s",
4✔
1039
                         lf->get_path_for_key().c_str());
1040
                iter->fvs_text_source = nullptr;
4✔
1041
                iter->fvs_error.clear();
4✔
1042

1043
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
1044
                if (read_res.isOk()) {
4✔
1045
                    auto read_file_res = read_res.unwrap();
4✔
1046
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
1047
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
1048
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
1049
                        data_scanner ds(orig_al.al_string);
4✔
1050
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
1051
                        attr_line_t pretty_al;
4✔
1052

1053
                        pp.append_to(pretty_al);
4✔
1054
                        iter->fvs_mtime = st.st_mtime;
4✔
1055
                        iter->fvs_file_indexed_size = lf->get_index_size();
4✔
1056
                        iter->fvs_file_size = st.st_size;
4✔
1057
                        iter->fvs_text_source
4✔
1058
                            = std::make_unique<plain_text_source>();
8✔
1059
                        iter->fvs_text_source->set_text_format(
4✔
1060
                            lf->get_text_format());
1061
                        iter->fvs_text_source->register_view(this->tss_view);
4✔
1062
                        iter->fvs_text_source->replace_with_mutable(
4✔
1063
                            pretty_al, lf->get_text_format());
1064
                    } else {
4✔
UNCOV
1065
                        log_error(
×
1066
                            "unable to read file to pretty-print: %s -- file "
1067
                            "is not valid UTF-8",
1068
                            lf->get_path_for_key().c_str());
UNCOV
1069
                        iter->fvs_mtime = st.st_mtime;
×
1070
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
UNCOV
1071
                        iter->fvs_file_size = st.st_size;
×
UNCOV
1072
                        iter->fvs_error = "file is not valid UTF-8";
×
1073
                    }
1074
                } else {
4✔
UNCOV
1075
                    auto errmsg = read_res.unwrapErr();
×
UNCOV
1076
                    log_error("unable to read file to pretty-print: %s -- %s",
×
1077
                              lf->get_path_for_key().c_str(),
1078
                              errmsg.c_str());
UNCOV
1079
                    iter->fvs_mtime = st.st_mtime;
×
UNCOV
1080
                    iter->fvs_file_indexed_size = lf->get_index_size();
×
UNCOV
1081
                    iter->fvs_file_size = st.st_size;
×
UNCOV
1082
                    iter->fvs_error = errmsg;
×
1083
                }
1084
            }
4✔
UNCOV
1085
        } catch (const line_buffer::error& e) {
×
1086
            iter = this->tss_files.erase(iter);
×
UNCOV
1087
            lf->close();
×
1088
            this->detach_observer(lf);
×
1089
            closed_files.emplace_back(lf);
×
1090
            continue;
×
1091
        }
×
1092

1093
        ++iter;
461✔
1094
    }
1,108✔
1095
    if (!closed_files.empty()) {
3,915✔
1096
        callback.closed_files(closed_files);
90✔
1097
        if (!this->tss_files.empty()) {
90✔
UNCOV
1098
            this->tss_files.front().load_into(*this->tss_view);
×
1099
        }
1100
        this->tss_view->set_needs_update();
90✔
1101
    }
1102

1103
    if (retval.rr_new_data) {
3,915✔
1104
        this->tss_view->search_new_data();
76✔
1105
    }
1106

1107
    return retval;
3,915✔
1108
}
3,932✔
1109

1110
void
UNCOV
1111
textfile_sub_source::set_top_from_off(file_off_t off)
×
1112
{
UNCOV
1113
    auto lf = this->current_file();
×
1114

UNCOV
1115
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1116
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
UNCOV
1117
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
UNCOV
1118
            std::distance(lf->cbegin(), new_top_iter));
×
1119

UNCOV
1120
        if (new_top_opt) {
×
UNCOV
1121
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
UNCOV
1122
            if (this->tss_view->is_selectable()) {
×
UNCOV
1123
                this->tss_view->set_top(
×
UNCOV
1124
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1125
            }
1126
        }
1127
    };
1128
}
1129

1130
void
UNCOV
1131
textfile_sub_source::quiesce()
×
1132
{
UNCOV
1133
    for (auto& lf : this->tss_files) {
×
UNCOV
1134
        lf.fvs_file->quiesce();
×
1135
    }
1136
}
1137

1138
std::optional<vis_line_t>
1139
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1140
{
1141
    const auto curr_iter = this->current_file_state();
6✔
1142
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
UNCOV
1143
        return std::nullopt;
×
1144
    }
1145

1146
    if (this->tss_view_mode == view_mode::rendered
12✔
1147
        && curr_iter->fvs_text_source)
6✔
1148
    {
1149
        return curr_iter->fvs_text_source->row_for_anchor(id);
4✔
1150
    }
1151

1152
    if (!curr_iter->fvs_metadata.m_sections_root) {
2✔
UNCOV
1153
        return std::nullopt;
×
1154
    }
1155

1156
    const auto& lf = curr_iter->fvs_file;
2✔
1157
    const auto& meta = curr_iter->fvs_metadata;
2✔
1158
    std::optional<vis_line_t> retval;
2✔
1159

1160
    auto is_ptr = startswith(id, "#/");
2✔
1161
    if (is_ptr) {
2✔
1162
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1163
        std::vector<lnav::document::section_key_t> path;
2✔
1164

1165
        while (!hier_sf.empty()) {
8✔
1166
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
6✔
1167
            auto scan_res
1168
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
6✔
1169
            if (scan_res && scan_res->range().empty()) {
6✔
1170
                path.emplace_back(scan_res->value());
2✔
1171
            } else {
1172
                stack_buf allocator;
4✔
1173
                path.emplace_back(
4✔
1174
                    json_ptr::decode(comp_pair.first, allocator).to_string());
8✔
1175
            }
4✔
1176
            hier_sf = comp_pair.second;
6✔
1177
        }
1178

1179
        auto lookup_res = lnav::document::hier_node::lookup_path(
2✔
1180
            meta.m_sections_root.get(), path);
2✔
1181
        if (lookup_res) {
2✔
1182
            auto ll_opt = lf->line_for_offset(lookup_res.value()->hn_start);
2✔
1183
            if (ll_opt != lf->end()) {
2✔
1184
                retval
1185
                    = vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1186
            }
1187
        }
1188

1189
        return retval;
2✔
1190
    }
2✔
1191

UNCOV
1192
    lnav::document::hier_node::depth_first(
×
1193
        meta.m_sections_root.get(),
1194
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1195
            for (const auto& child_pair : node->hn_named_children) {
×
1196
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1197

UNCOV
1198
                if (child_anchor != id) {
×
1199
                    continue;
×
1200
                }
1201

1202
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1203
                if (ll_opt != lf->end()) {
×
UNCOV
1204
                    retval = vis_line_t(
×
1205
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1206
                }
1207
                break;
×
1208
            }
1209
        });
×
1210

1211
    return retval;
×
1212
}
1213

1214
static void
1215
anchor_generator(std::unordered_set<std::string>& retval,
×
1216
                 std::vector<std::string>& comps,
1217
                 size_t& max_depth,
1218
                 lnav::document::hier_node* hn)
1219
{
1220
    if (hn->hn_named_children.empty()) {
×
1221
        if (hn->hn_children.empty()) {
×
1222
            if (retval.size() >= 250 || comps.empty()) {
×
1223
            } else if (comps.size() == 1) {
×
1224
                retval.emplace(text_anchors::to_anchor_string(comps.front()));
×
1225
            } else {
UNCOV
1226
                retval.emplace(
×
UNCOV
1227
                    fmt::format(FMT_STRING("#/{}"),
×
UNCOV
1228
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1229
            }
UNCOV
1230
            max_depth = std::max(max_depth, comps.size());
×
1231
        } else {
UNCOV
1232
            int index = 0;
×
1233
            for (const auto& child : hn->hn_children) {
×
UNCOV
1234
                comps.emplace_back(fmt::to_string(index));
×
1235
                anchor_generator(retval, comps, max_depth, child.get());
×
1236
                comps.pop_back();
×
1237
            }
1238
        }
1239
    } else {
1240
        for (const auto& [child_name, child_node] : hn->hn_named_children) {
×
1241
            comps.emplace_back(child_name);
×
1242
            anchor_generator(retval, comps, max_depth, child_node);
×
UNCOV
1243
            comps.pop_back();
×
1244
        }
UNCOV
1245
        if (max_depth > 1) {
×
UNCOV
1246
            retval.emplace(
×
1247
                fmt::format(FMT_STRING("#/{}"),
×
1248
                            fmt::join(comps.begin(), comps.end(), "/")));
×
1249
        }
1250
    }
1251
}
1252

1253
std::unordered_set<std::string>
1254
textfile_sub_source::get_anchors()
×
1255
{
UNCOV
1256
    std::unordered_set<std::string> retval;
×
1257

1258
    const auto curr_iter = this->current_file_state();
×
1259
    if (curr_iter == this->tss_files.end()) {
×
1260
        return retval;
×
1261
    }
1262

UNCOV
1263
    if (this->tss_view_mode == view_mode::rendered
×
UNCOV
1264
        && curr_iter->fvs_text_source)
×
1265
    {
UNCOV
1266
        return curr_iter->fvs_text_source->get_anchors();
×
1267
    }
1268

UNCOV
1269
    const auto& meta = curr_iter->fvs_metadata;
×
UNCOV
1270
    if (meta.m_sections_root == nullptr) {
×
UNCOV
1271
        return retval;
×
1272
    }
1273

UNCOV
1274
    std::vector<std::string> comps;
×
UNCOV
1275
    size_t max_depth = 0;
×
UNCOV
1276
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1277

UNCOV
1278
    return retval;
×
1279
}
1280

1281
struct tfs_time_cmp {
UNCOV
1282
    bool operator()(int32_t lhs, const timeval& rhs) const
×
1283
    {
UNCOV
1284
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
×
UNCOV
1285
        return ll->get_timeval() < rhs;
×
1286
    }
1287

1288
    logfile* ttc_logfile;
1289
    std::vector<uint32_t>& ttc_index;
1290
};
1291

1292
std::optional<vis_line_t>
UNCOV
1293
textfile_sub_source::row_for_time(timeval time_bucket)
×
1294
{
UNCOV
1295
    auto lf = this->current_file();
×
UNCOV
1296
    if (!lf || !lf->has_line_metadata()) {
×
1297
        return std::nullopt;
×
1298
    }
1299

UNCOV
1300
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
UNCOV
1301
    auto& tfs = lfo->lfo_filter_state.tfs_index;
×
UNCOV
1302
    auto lb = std::lower_bound(
×
UNCOV
1303
        tfs.begin(), tfs.end(), time_bucket, tfs_time_cmp{lf.get(), tfs});
×
UNCOV
1304
    if (lb != tfs.end()) {
×
UNCOV
1305
        return vis_line_t{(int) std::distance(tfs.begin(), lb)};
×
1306
    }
1307

UNCOV
1308
    return std::nullopt;
×
1309
}
1310

1311
std::optional<text_time_translator::row_info>
1312
textfile_sub_source::time_for_row(vis_line_t row)
29✔
1313
{
1314
    auto lf = this->current_file();
29✔
1315
    if (!lf || !lf->has_line_metadata()) {
29✔
1316
        return std::nullopt;
25✔
1317
    }
1318

1319
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
4✔
1320
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
4✔
UNCOV
1321
        return std::nullopt;
×
1322
    }
1323
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
4✔
1324
    auto ll_iter = lf->begin() + row_id;
4✔
1325
    return row_info{
8✔
1326
        ll_iter->get_timeval(),
1327
        row_id,
1328
    };
4✔
1329
}
29✔
1330

1331
static std::optional<vis_line_t>
1332
to_vis_line(const std::shared_ptr<logfile>& lf, file_off_t off)
2✔
1333
{
1334
    auto ll_opt = lf->line_for_offset(off);
2✔
1335
    if (ll_opt != lf->end()) {
2✔
1336
        return vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1337
    }
1338

UNCOV
1339
    return std::nullopt;
×
1340
}
1341

1342
std::optional<vis_line_t>
1343
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
2✔
1344
{
1345
    const auto curr_iter = this->current_file_state();
2✔
1346
    if (curr_iter == this->tss_files.end()) {
2✔
UNCOV
1347
        return std::nullopt;
×
1348
    }
1349

1350
    const auto& lf = curr_iter->fvs_file;
2✔
1351
    log_debug("adjacent_anchor: %s:L%d:%s",
2✔
1352
              lf->get_path_for_key().c_str(),
1353
              vl,
1354
              dir == text_anchors::direction::prev ? "prev" : "next");
1355
    if (this->tss_view_mode == view_mode::rendered
4✔
1356
        && curr_iter->fvs_text_source)
2✔
1357
    {
UNCOV
1358
        return curr_iter->fvs_text_source->adjacent_anchor(vl, dir);
×
1359
    }
1360

1361
    if (!curr_iter->fvs_metadata.m_sections_root) {
2✔
1362
        log_debug("  no metadata available");
×
1363
        return std::nullopt;
×
1364
    }
1365

1366
    auto& md = curr_iter->fvs_metadata;
2✔
1367
    const auto* lfo
1368
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
1369
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
2✔
1370
        || md.m_sections_root == nullptr)
2✔
1371
    {
UNCOV
1372
        return std::nullopt;
×
1373
    }
1374
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
2✔
1375
    const auto line_offsets = lf->get_file_range(ll_iter, false);
2✔
1376
    log_debug(
2✔
1377
        "  range %d:%d", line_offsets.fr_offset, line_offsets.next_offset());
1378
    auto path_for_line
1379
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
2✔
1380

1381
    if (path_for_line.empty()) {
2✔
UNCOV
1382
        log_debug("  no path found");
×
UNCOV
1383
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
UNCOV
1384
        if (!neighbors_res) {
×
UNCOV
1385
            return std::nullopt;
×
1386
        }
1387

UNCOV
1388
        switch (dir) {
×
UNCOV
1389
            case direction::prev: {
×
UNCOV
1390
                if (neighbors_res->cnr_previous) {
×
UNCOV
1391
                    return to_vis_line(
×
UNCOV
1392
                        lf, neighbors_res->cnr_previous.value()->hn_start);
×
1393
                }
1394
                break;
×
1395
            }
1396
            case direction::next: {
×
UNCOV
1397
                if (neighbors_res->cnr_next) {
×
1398
                    return to_vis_line(
×
UNCOV
1399
                        lf, neighbors_res->cnr_next.value()->hn_start);
×
1400
                }
UNCOV
1401
                if (!md.m_sections_root->hn_children.empty()) {
×
UNCOV
1402
                    return to_vis_line(
×
UNCOV
1403
                        lf, md.m_sections_root->hn_children[0]->hn_start);
×
1404
                }
1405
                break;
×
1406
            }
1407
        }
UNCOV
1408
        return std::nullopt;
×
1409
    }
1410

1411
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
2✔
1412
    const auto last_key = std::move(path_for_line.back());
2✔
1413
    path_for_line.pop_back();
2✔
1414

1415
    const auto parent_opt = lnav::document::hier_node::lookup_path(
2✔
1416
        md.m_sections_root.get(), path_for_line);
2✔
1417
    if (!parent_opt) {
2✔
UNCOV
1418
        log_debug("  no parent for path: %s",
×
1419
                  fmt::to_string(path_for_line).c_str());
UNCOV
1420
        return std::nullopt;
×
1421
    }
1422
    const auto parent = parent_opt.value();
2✔
1423

1424
    const auto child_hn = parent->lookup_child(last_key);
2✔
1425
    if (!child_hn) {
2✔
1426
        // XXX "should not happen"
UNCOV
1427
        log_debug("  child not found");
×
UNCOV
1428
        return std::nullopt;
×
1429
    }
1430

1431
    auto neighbors_res = parent->child_neighbors(
6✔
1432
        child_hn.value(), line_offsets.next_offset() + 1);
2✔
1433
    if (!neighbors_res) {
2✔
1434
        log_debug("  no neighbors found");
×
UNCOV
1435
        return std::nullopt;
×
1436
    }
1437

1438
    log_debug("  neighbors p:%d n:%d",
2✔
1439
              neighbors_res->cnr_previous.has_value(),
1440
              neighbors_res->cnr_next.has_value());
1441
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
2✔
1442
        auto neighbor_sub
1443
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
2✔
1444
        if (neighbor_sub) {
2✔
UNCOV
1445
            neighbors_res->cnr_previous = neighbor_sub;
×
1446
        }
1447
    }
1448

1449
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
2✔
1450
        auto neighbor_sub
1451
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
2✔
1452
        if (neighbor_sub) {
2✔
1453
            neighbors_res->cnr_next = neighbor_sub;
2✔
1454
        }
1455
    }
1456

1457
    switch (dir) {
2✔
UNCOV
1458
        case direction::prev: {
×
UNCOV
1459
            if (neighbors_res->cnr_previous) {
×
UNCOV
1460
                return to_vis_line(
×
UNCOV
1461
                    lf, neighbors_res->cnr_previous.value()->hn_start);
×
1462
            }
UNCOV
1463
            break;
×
1464
        }
1465
        case direction::next: {
2✔
1466
            if (neighbors_res->cnr_next) {
2✔
1467
                return to_vis_line(lf,
2✔
1468
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1469
            }
UNCOV
1470
            break;
×
1471
        }
1472
    }
1473

UNCOV
1474
    return std::nullopt;
×
1475
}
2✔
1476

1477
std::optional<std::string>
1478
textfile_sub_source::anchor_for_row(vis_line_t vl)
11✔
1479
{
1480
    const auto curr_iter = this->current_file_state();
11✔
1481
    if (curr_iter == this->tss_files.end()) {
11✔
UNCOV
1482
        return std::nullopt;
×
1483
    }
1484

1485
    if (this->tss_view_mode == view_mode::rendered
22✔
1486
        && curr_iter->fvs_text_source)
11✔
1487
    {
1488
        return curr_iter->fvs_text_source->anchor_for_row(vl);
3✔
1489
    }
1490

1491
    if (!curr_iter->fvs_metadata.m_sections_root) {
8✔
UNCOV
1492
        return std::nullopt;
×
1493
    }
1494

1495
    const auto& lf = curr_iter->fvs_file;
8✔
1496
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
8✔
1497
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
8✔
UNCOV
1498
        return std::nullopt;
×
1499
    }
1500
    auto& md = curr_iter->fvs_metadata;
8✔
1501
    auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
8✔
1502
    auto line_offsets = lf->get_file_range(ll_iter, false);
8✔
1503
    auto path_for_line
1504
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
8✔
1505

1506
    if (path_for_line.empty()) {
8✔
1507
        return std::nullopt;
4✔
1508
    }
1509

1510
    if ((path_for_line.size() == 1
4✔
1511
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1512
        && path_for_line.back().is<std::string>())
6✔
1513
    {
1514
        return text_anchors::to_anchor_string(
2✔
1515
            path_for_line.back().get<std::string>());
2✔
1516
    }
1517

1518
    auto comps
1519
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1520
              return elem.match(
UNCOV
1521
                  [](const std::string& str) {
×
1522
                      stack_buf allocator;
5✔
1523
                      return json_ptr::encode(str, allocator).to_string();
10✔
1524
                  },
5✔
1525
                  [](size_t index) { return fmt::to_string(index); });
13✔
1526
          });
2✔
1527

1528
    return fmt::format(FMT_STRING("#/{}"),
8✔
1529
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1530
}
8✔
1531

1532
bool
1533
textfile_sub_source::to_front(const std::string& filename)
1✔
1534
{
1535
    auto lf_opt = this->tss_files
1✔
1536
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1537
                      return elem.fvs_file->get_filename() == filename;
1✔
1538
                  });
1✔
1539
    if (!lf_opt) {
1✔
1540
        return false;
1✔
1541
    }
1542

UNCOV
1543
    this->to_front(lf_opt.value()->fvs_file);
×
1544

UNCOV
1545
    return true;
×
1546
}
1547

1548
logline*
1549
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1550
{
1551
    auto lf = this->current_file();
3✔
1552
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1553
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1554
}
3✔
1555

1556
void
1557
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1558
{
1559
    this->tss_view_mode = vm;
2✔
1560
    this->tss_view->set_needs_update();
2✔
1561
}
2✔
1562

1563
textfile_sub_source::view_mode
1564
textfile_sub_source::get_effective_view_mode() const
39✔
1565
{
1566
    auto retval = view_mode::raw;
39✔
1567

1568
    const auto curr_iter = this->current_file_state();
39✔
1569
    if (curr_iter != this->tss_files.end()) {
39✔
1570
        if (this->tss_view_mode == view_mode::rendered
78✔
1571
            && curr_iter->fvs_text_source)
39✔
1572
        {
1573
            retval = view_mode::rendered;
3✔
1574
        }
1575
    }
1576

1577
    return retval;
39✔
1578
}
1579

1580
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
579✔
1581
                                                 text_sub_source* log_src)
579✔
1582
    : tho_src(src), tho_log_src(log_src)
579✔
1583
{
1584
}
579✔
1585

1586
bool
1587
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
56✔
1588
                                             int y,
1589
                                             int bottom,
1590
                                             attr_line_t& value_out)
1591
{
1592
    const std::vector<attr_line_t>* lines = nullptr;
56✔
1593
    auto curr_file = this->tho_src->current_file();
56✔
1594
    if (curr_file == nullptr) {
56✔
UNCOV
1595
        if (this->tho_log_src->text_line_count() == 0) {
×
UNCOV
1596
            lines = lnav::messages::view::no_files();
×
1597
        } else {
UNCOV
1598
            lines = lnav::messages::view::only_log_files();
×
1599
        }
1600
    } else if (curr_file->size() == 0) {
56✔
UNCOV
1601
        lines = lnav::messages::view::empty_file();
×
1602
    } else if (this->tho_src->text_line_count() == 0) {
56✔
UNCOV
1603
        hasher h;
×
UNCOV
1604
        this->tho_src->update_filter_hash_state(h);
×
UNCOV
1605
        auto curr_state = h.to_array();
×
UNCOV
1606
        if (this->tho_static_lines.empty()
×
UNCOV
1607
            || curr_state != this->tho_filter_state)
×
1608
        {
1609
            auto msg = lnav::console::user_message::info(
UNCOV
1610
                "All log messages are currently hidden");
×
UNCOV
1611
            auto min_time = this->tho_src->get_min_row_time();
×
UNCOV
1612
            if (min_time) {
×
UNCOV
1613
                msg.with_note(attr_line_t("Logs before ")
×
UNCOV
1614
                                  .append_quoted(
×
UNCOV
1615
                                      lnav::to_rfc3339_string(min_time.value()))
×
UNCOV
1616
                                  .append(" are not being shown"));
×
1617
            }
UNCOV
1618
            auto max_time = this->tho_src->get_max_row_time();
×
UNCOV
1619
            if (max_time) {
×
UNCOV
1620
                msg.with_note(attr_line_t("Logs after ")
×
UNCOV
1621
                                  .append_quoted(
×
UNCOV
1622
                                      lnav::to_rfc3339_string(max_time.value()))
×
UNCOV
1623
                                  .append(" are not being shown"));
×
1624
            }
UNCOV
1625
            auto& fs = this->tho_src->get_filters();
×
UNCOV
1626
            for (const auto& filt : fs) {
×
1627
                auto hits
UNCOV
1628
                    = this->tho_src->get_filtered_count_for(filt->get_index());
×
UNCOV
1629
                if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
UNCOV
1630
                    continue;
×
1631
                }
UNCOV
1632
                auto cmd = attr_line_t(":" + filt->to_command());
×
UNCOV
1633
                readline_command_highlighter(cmd, std::nullopt);
×
UNCOV
1634
                msg.with_note(
×
UNCOV
1635
                    attr_line_t("Filter ")
×
UNCOV
1636
                        .append_quoted(cmd)
×
UNCOV
1637
                        .append(" matched ")
×
UNCOV
1638
                        .append(lnav::roles::number(fmt::to_string(hits)))
×
UNCOV
1639
                        .append(" message(s) "));
×
1640
            }
UNCOV
1641
            this->tho_static_lines = msg.to_attr_line().split_lines();
×
UNCOV
1642
            this->tho_filter_state = curr_state;
×
1643
        }
1644

UNCOV
1645
        lines = &this->tho_static_lines;
×
1646
    }
1647

1648
    if (lines != nullptr && y < (ssize_t) lines->size()) {
56✔
UNCOV
1649
        value_out = lines->at(y);
×
UNCOV
1650
        value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
UNCOV
1651
        if (y == (ssize_t) lines->size() - 1) {
×
UNCOV
1652
            value_out.with_attr_for_all(
×
UNCOV
1653
                VC_STYLE.value(text_attrs::with_underline()));
×
1654
        }
UNCOV
1655
        return true;
×
1656
    }
1657

1658
    if (y != 0) {
56✔
1659
        return false;
8✔
1660
    }
1661

1662
    const auto lf = this->tho_src->current_file();
48✔
1663
    if (lf == nullptr) {
48✔
UNCOV
1664
        return false;
×
1665
    }
1666

1667
    if (lf->get_text_format() != text_format_t::TF_MARKDOWN
48✔
1668
        && this->tho_src->get_effective_view_mode()
48✔
1669
            == textfile_sub_source::view_mode::rendered)
1670
    {
1671
        auto ta = text_attrs::with_underline();
3✔
1672
        value_out.append("\u24d8"_info)
3✔
1673
            .append(" The following is a rendered view of the content.  Use ")
3✔
1674
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
3✔
1675
            .append(" to view the raw version of this text")
3✔
1676
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
6✔
1677
            .with_attr_for_all(VC_STYLE.value(ta));
3✔
1678
        return true;
3✔
1679
    }
3✔
1680

1681
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
45✔
1682
        return false;
40✔
1683
    }
1684

1685
    {
1686
        attr_line_builder alb(value_out);
5✔
1687
        {
1688
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
5✔
1689
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
15✔
1690
        }
5✔
1691
        size_t byte_off = 0;
5✔
1692
        for (size_t lpc = 0; lpc < 16; lpc++) {
85✔
1693
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
80✔
1694
            if (byte_off == 8) {
80✔
1695
                alb.append(" ");
5✔
1696
            }
1697
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
240✔
1698
            byte_off += 1;
80✔
1699
        }
80✔
1700
        {
1701
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
5✔
1702
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
15✔
1703
        }
5✔
1704
    }
1705
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
5✔
1706
    return true;
5✔
1707
}
56✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc