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

tstack / lnav / 18882433814-2607

28 Oct 2025 04:45PM UTC coverage: 68.89% (-1.1%) from 70.002%
18882433814-2607

push

github

tstack
[hasher] fixup hasher::to_string() for sse2

5 of 5 new or added lines in 1 file covered. (100.0%)

2926 existing lines in 57 files now uncovered.

50208 of 72881 relevant lines covered (68.89%)

424460.54 hits per line

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

64.33
/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
#include <memory>
32

33
#include "textfile_sub_source.hh"
34

35
#include <date/date.h>
36

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

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

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

65
    const auto& opts = lf->get_open_options();
534✔
66
    if (opts.loo_piper && !opts.loo_piper->is_finished()) {
534✔
UNCOV
67
        return false;
×
68
    }
69

70
    switch (lf->get_text_format()) {
534✔
71
        case text_format_t::TF_BINARY:
43✔
72
        case text_format_t::TF_DIFF:
73
            return false;
43✔
74
        default:
491✔
75
            if (lf->get_stat().st_size < 16 * 1024
491✔
76
                && lf->get_longest_line_length()
982✔
77
                    > cfg.c_max_unformatted_line_length)
491✔
78
            {
79
                return true;
27✔
80
            }
81
            return false;
464✔
82
    }
83
}
84

85
size_t
86
textfile_sub_source::text_line_count()
22,223✔
87
{
88
    size_t retval = 0;
22,223✔
89

90
    if (!this->tss_files.empty()) {
22,223✔
91
        retval
92
            = this->current_file_state()->text_line_count(this->tss_view_mode);
15,029✔
93
    }
94

95
    return retval;
22,223✔
96
}
97

98
line_info
99
textfile_sub_source::text_value_for_line(textview_curses& tc,
3,400✔
100
                                         int line,
101
                                         std::string& value_out,
102
                                         text_sub_source::line_flags_t flags)
103
{
104
    if (this->tss_files.empty() || line < 0) {
3,400✔
UNCOV
105
        value_out.clear();
×
UNCOV
106
        return {};
×
107
    }
108

109
    const auto curr_iter = this->current_file_state();
3,400✔
110
    const auto& lf = curr_iter->fvs_file;
3,400✔
111
    if (this->tss_view_mode == view_mode::rendered
6,800✔
112
        && curr_iter->fvs_text_source)
3,400✔
113
    {
114
        curr_iter->fvs_text_source->text_value_for_line(
1,294✔
115
            tc, line, value_out, flags);
116
        return {};
1,294✔
117
    }
118

119
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
2,106✔
120
        this->tss_hex_line.clear();
1,430✔
121
        auto fsize = lf->get_content_size();
1,430✔
122
        auto fr = file_range{line * 16};
1,430✔
123
        fr.fr_size = std::min((file_ssize_t) 16, fsize - fr.fr_offset);
1,430✔
124

125
        auto read_res = lf->read_range(fr);
1,430✔
126
        if (read_res.isErr()) {
1,430✔
UNCOV
127
            log_error("%s: failed to read range %lld:%lld -- %s",
×
128
                      lf->get_path_for_key().c_str(),
129
                      fr.fr_offset,
130
                      fr.fr_size,
131
                      read_res.unwrapErr().c_str());
UNCOV
132
            return {};
×
133
        }
134

135
        auto sbr = read_res.unwrap();
1,430✔
136
        auto sf = sbr.to_string_fragment();
1,430✔
137
        attr_line_builder alb(this->tss_hex_line);
1,430✔
138
        {
139
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
1,430✔
140
            alb.appendf(FMT_STRING("{: >16x} "), fr.fr_offset);
4,290✔
141
        }
1,430✔
142
        alb.append_as_hexdump(sf);
1,430✔
143
        auto alt_row_index = line % 4;
1,430✔
144
        if (alt_row_index == 2 || alt_row_index == 3) {
1,430✔
145
            this->tss_hex_line.with_attr_for_all(
712✔
146
                VC_ROLE.value(role_t::VCR_ALT_ROW));
1,424✔
147
        }
148

149
        value_out = this->tss_hex_line.get_string();
1,430✔
150
        return {};
1,430✔
151
    }
1,430✔
152

153
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
676✔
154
    if (lfo == nullptr
676✔
155
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
676✔
156
    {
UNCOV
157
        value_out.clear();
×
UNCOV
158
        return {};
×
159
    }
160

161
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
676✔
162
    auto read_result = lf->read_line(ll);
676✔
163
    this->tss_line_indent_size = 0;
676✔
164
    this->tss_plain_line_attrs.clear();
676✔
165
    if (read_result.isOk()) {
676✔
166
        auto sbr = read_result.unwrap();
676✔
167
        value_out = to_string(sbr);
676✔
168
        if (sbr.get_metadata().m_has_ansi) {
676✔
169
            scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
35✔
170
        }
171
        for (const auto& ch : value_out) {
1,860✔
172
            if (ch == ' ') {
1,728✔
173
                this->tss_line_indent_size += 1;
1,182✔
174
            } else if (ch == '\t') {
546✔
175
                do {
176
                    this->tss_line_indent_size += 1;
15✔
177
                } while (this->tss_line_indent_size % 8);
15✔
178
            } else {
179
                break;
544✔
180
            }
181
        }
182
        if (lf->has_line_metadata() && this->tas_display_time_offset) {
676✔
183
            auto relstr = this->get_time_offset_for_line(tc, vis_line_t(line));
1✔
184
            value_out
185
                = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
4✔
186
        }
1✔
187
    }
676✔
188

189
    return {};
676✔
190
}
676✔
191

192
void
193
textfile_sub_source::text_attrs_for_line(textview_curses& tc,
3,400✔
194
                                         int row,
195
                                         string_attrs_t& value_out)
196
{
197
    const auto curr_iter = this->current_file_state();
3,400✔
198
    if (curr_iter == this->tss_files.end()) {
3,400✔
UNCOV
199
        return;
×
200
    }
201
    const auto& lf = curr_iter->fvs_file;
3,400✔
202

203
    auto lr = line_range{0, -1};
3,400✔
204
    if (this->tss_view_mode == view_mode::rendered
6,800✔
205
        && curr_iter->fvs_text_source)
3,400✔
206
    {
207
        curr_iter->fvs_text_source->text_attrs_for_line(tc, row, value_out);
1,294✔
208
    } else if (lf->get_text_format() == text_format_t::TF_BINARY) {
2,106✔
209
        value_out = this->tss_hex_line.get_attrs();
1,430✔
210
    } else {
211
        value_out = this->tss_plain_line_attrs;
676✔
212
        auto* lfo
213
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
676✔
214
        if (lfo != nullptr && row >= 0
676✔
215
            && row < (ssize_t) lfo->lfo_filter_state.tfs_index.size())
1,352✔
216
        {
217
            auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[row];
676✔
218

219
            value_out.emplace_back(lr, SA_LEVEL.value(ll->get_msg_level()));
676✔
220
            if (lf->has_line_metadata() && this->tas_display_time_offset) {
676✔
221
                auto time_offset_end = 13;
1✔
222
                lr.lr_start = 0;
1✔
223
                lr.lr_end = time_offset_end;
1✔
224

225
                shift_string_attrs(value_out, 0, time_offset_end);
1✔
226

227
                value_out.emplace_back(lr,
1✔
228
                                       VC_ROLE.value(role_t::VCR_OFFSET_TIME));
2✔
229
                value_out.emplace_back(line_range(12, 13),
1✔
230
                                       VC_GRAPHIC.value(NCACS_VLINE));
2✔
231

232
                auto bar_role = role_t::VCR_NONE;
1✔
233

234
                switch (this->get_line_accel_direction(vis_line_t(row))) {
1✔
235
                    case log_accel::direction_t::A_STEADY:
1✔
236
                        break;
1✔
UNCOV
237
                    case log_accel::direction_t::A_DECEL:
×
UNCOV
238
                        bar_role = role_t::VCR_DIFF_DELETE;
×
UNCOV
239
                        break;
×
UNCOV
240
                    case log_accel::direction_t::A_ACCEL:
×
UNCOV
241
                        bar_role = role_t::VCR_DIFF_ADD;
×
UNCOV
242
                        break;
×
243
                }
244
                if (bar_role != role_t::VCR_NONE) {
1✔
UNCOV
245
                    value_out.emplace_back(line_range(12, 13),
×
UNCOV
246
                                           VC_ROLE.value(bar_role));
×
247
                }
248
            }
249

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

302
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
3,400✔
303
}
304

305
size_t
UNCOV
306
textfile_sub_source::text_size_for_line(textview_curses& tc,
×
307
                                        int line,
308
                                        text_sub_source::line_flags_t flags)
309
{
UNCOV
310
    size_t retval = 0;
×
311

UNCOV
312
    if (!this->tss_files.empty()) {
×
UNCOV
313
        const auto curr_iter = this->current_file_state();
×
UNCOV
314
        const auto& lf = curr_iter->fvs_file;
×
UNCOV
315
        if (this->tss_view_mode == view_mode::raw
×
UNCOV
316
            || !curr_iter->fvs_text_source)
×
317
        {
UNCOV
318
            auto* lfo = dynamic_cast<line_filter_observer*>(
×
UNCOV
319
                lf->get_logline_observer());
×
UNCOV
320
            if (lfo == nullptr || line < 0
×
321
                || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
×
322
            {
323
            } else {
324
                auto read_res = lf->read_line(
325
                    lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
×
326
                if (read_res.isOk()) {
×
327
                    auto sbr = read_res.unwrap();
×
UNCOV
328
                    auto str = to_string(sbr);
×
329
                    scrub_ansi_string(str, nullptr);
×
330
                    retval = string_fragment::from_str(str).column_width();
×
331
                }
332
            }
333
        } else {
UNCOV
334
            retval = curr_iter->fvs_text_source->text_size_for_line(
×
335
                tc, line, flags);
336
        }
337
    }
338

339
    return retval;
×
340
}
341

342
void
UNCOV
343
textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
×
344
{
345
    const auto iter
UNCOV
346
        = std::find(this->tss_files.begin(), this->tss_files.end(), lf);
×
UNCOV
347
    if (iter == this->tss_files.end()) {
×
UNCOV
348
        return;
×
349
    }
350
    this->tss_files.front().save_from(*this->tss_view);
×
UNCOV
351
    auto fvs = std::move(*iter);
×
UNCOV
352
    this->tss_files.erase(iter);
×
UNCOV
353
    this->tss_files.emplace_front(std::move(fvs));
×
354
    this->set_time_offset(false);
×
UNCOV
355
    fvs.load_into(*this->tss_view);
×
UNCOV
356
    this->tss_view->reload_data();
×
357
}
358

359
void
UNCOV
360
textfile_sub_source::rotate_left()
×
361
{
362
    if (this->tss_files.size() > 1) {
×
363
        this->tss_files.emplace_back(std::move(this->tss_files.front()));
×
364
        this->tss_files.pop_front();
×
365
        this->tss_files.back().save_from(*this->tss_view);
×
366
        this->tss_files.front().load_into(*this->tss_view);
×
367
        this->set_time_offset(false);
×
UNCOV
368
        this->tss_view->reload_data();
×
UNCOV
369
        this->tss_view->redo_search();
×
UNCOV
370
        this->tss_view->set_needs_update();
×
371
    }
372
}
373

374
void
375
textfile_sub_source::rotate_right()
×
376
{
377
    if (this->tss_files.size() > 1) {
×
378
        this->tss_files.front().save_from(*this->tss_view);
×
379
        auto fvs = std::move(this->tss_files.back());
×
380
        this->tss_files.emplace_front(std::move(fvs));
×
381
        this->tss_files.pop_back();
×
UNCOV
382
        this->tss_files.front().load_into(*this->tss_view);
×
UNCOV
383
        this->set_time_offset(false);
×
UNCOV
384
        this->tss_view->reload_data();
×
UNCOV
385
        this->tss_view->redo_search();
×
386
        this->tss_view->set_needs_update();
×
387
    }
388
}
389

390
void
391
textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
501✔
392
{
393
    auto iter = std::find(this->tss_files.begin(), this->tss_files.end(), lf);
501✔
394
    if (iter != this->tss_files.end()) {
501✔
395
        this->tss_files.erase(iter);
×
396
        this->detach_observer(lf);
×
397
    }
398
    this->set_time_offset(false);
501✔
399
    if (!this->tss_files.empty()) {
501✔
UNCOV
400
        this->tss_files.front().load_into(*this->tss_view);
×
401
    }
402
    this->tss_view->reload_data();
501✔
403
}
501✔
404

405
void
406
textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
598✔
407
{
408
    auto* lfo = new line_filter_observer(this->get_filters(), lf);
598✔
409
    lf->set_logline_observer(lfo);
598✔
410
    this->tss_files.emplace_back(lf);
598✔
411
}
598✔
412

413
void
414
textfile_sub_source::text_filters_changed()
9✔
415
{
416
    auto lf = this->current_file();
9✔
417
    if (lf == nullptr || lf->get_text_format() == text_format_t::TF_BINARY) {
9✔
418
        return;
6✔
419
    }
420

421
    auto* lfo = (line_filter_observer*) lf->get_logline_observer();
3✔
422
    uint32_t filter_in_mask, filter_out_mask;
423

424
    lfo->clear_deleted_filter_state();
3✔
425
    lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
3✔
426

427
    this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
3✔
428
    lfo->lfo_filter_state.tfs_index.clear();
3✔
429
    for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
18✔
430
        if (this->tss_apply_filters) {
15✔
431
            if (lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
15✔
432
                continue;
5✔
433
            }
434
            if (lf->has_line_metadata()) {
10✔
UNCOV
435
                auto ll = lf->begin() + lpc;
×
UNCOV
436
                if (ll->get_timeval() < this->ttt_min_row_time) {
×
UNCOV
437
                    continue;
×
438
                }
UNCOV
439
                if (this->ttt_max_row_time < ll->get_timeval()) {
×
UNCOV
440
                    continue;
×
441
                }
442
            }
443
        }
444
        lfo->lfo_filter_state.tfs_index.push_back(lpc);
10✔
445
    }
446

447
    this->tss_view->redo_search();
3✔
448

449
    auto iter = std::lower_bound(lfo->lfo_filter_state.tfs_index.begin(),
3✔
450
                                 lfo->lfo_filter_state.tfs_index.end(),
451
                                 this->tss_content_line);
3✔
452
    auto vl = vis_line_t(
453
        std::distance(lfo->lfo_filter_state.tfs_index.begin(), iter));
6✔
454
    this->tss_view->set_selection(vl);
3✔
455
}
9✔
456

457
void
458
textfile_sub_source::scroll_invoked(textview_curses* tc)
719✔
459
{
460
    const auto curr_iter = this->current_file_state();
719✔
461
    if (curr_iter == this->tss_files.end()
719✔
462
        || curr_iter->fvs_file->get_text_format() == text_format_t::TF_BINARY)
719✔
463
    {
464
        return;
645✔
465
    }
466

467
    const auto& lf = curr_iter->fvs_file;
93✔
468
    if (this->tss_view_mode == view_mode::rendered
186✔
469
        && curr_iter->fvs_text_source)
93✔
470
    {
471
        return;
19✔
472
    }
473

474
    auto line = tc->get_selection();
74✔
475
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
74✔
476
    if (!line || lfo == nullptr || line < 0_vl
148✔
477
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
148✔
478
    {
UNCOV
479
        return;
×
480
    }
481

482
    this->tss_content_line = lfo->lfo_filter_state.tfs_index[line.value()];
74✔
483
}
484

485
int
486
textfile_sub_source::get_filtered_count() const
263✔
487
{
488
    const auto curr_iter = this->current_file_state();
263✔
489
    int retval = 0;
263✔
490

491
    if (curr_iter != this->tss_files.end()) {
263✔
492
        if (this->tss_view_mode == view_mode::raw
518✔
493
            || !curr_iter->fvs_text_source)
259✔
494
        {
495
            const auto& lf = curr_iter->fvs_file;
208✔
496
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
208✔
497
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
208✔
498
        }
499
    }
500
    return retval;
263✔
501
}
502

503
int
UNCOV
504
textfile_sub_source::get_filtered_count_for(size_t filter_index) const
×
505
{
UNCOV
506
    std::shared_ptr<logfile> lf = this->current_file();
×
507

UNCOV
508
    if (lf == nullptr) {
×
UNCOV
509
        return 0;
×
510
    }
511

UNCOV
512
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
UNCOV
513
    return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
×
514
}
515

516
text_format_t
517
textfile_sub_source::get_text_format() const
2,903✔
518
{
519
    if (this->tss_files.empty()) {
2,903✔
520
        return text_format_t::TF_UNKNOWN;
×
521
    }
522

523
    return this->tss_files.front().fvs_file->get_text_format();
2,903✔
524
}
525

526
static attr_line_t
527
to_display(const std::shared_ptr<logfile>& lf)
22✔
528
{
529
    attr_line_t retval;
22✔
530

531
    if (lf->get_open_options().loo_piper) {
22✔
532
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
UNCOV
533
            retval.append("\u21bb "_list_glyph);
×
534
        }
535
    }
536
    retval.append(lf->get_unique_path());
22✔
537

538
    return retval;
22✔
UNCOV
539
}
×
540

541
void
542
textfile_sub_source::text_crumbs_for_line(
11✔
543
    int line, std::vector<breadcrumb::crumb>& crumbs)
544
{
545
    text_sub_source::text_crumbs_for_line(line, crumbs);
11✔
546

547
    if (this->empty()) {
11✔
UNCOV
548
        return;
×
549
    }
550

551
    const auto curr_iter = this->current_file_state();
11✔
552
    const auto& lf = curr_iter->fvs_file;
11✔
553
    crumbs.emplace_back(
11✔
554
        lf->get_unique_path(),
11✔
555
        to_display(lf),
11✔
UNCOV
556
        [this]() {
×
557
            return this->tss_files | lnav::itertools::map([](const auto& lf) {
22✔
558
                       return breadcrumb::possibility{
559
                           lf.fvs_file->get_path_for_key(),
11✔
560
                           to_display(lf.fvs_file),
11✔
561
                       };
11✔
562
                   });
22✔
563
        },
564
        [this](const auto& key) {
11✔
UNCOV
565
            auto lf_opt = this->tss_files
×
UNCOV
566
                | lnav::itertools::map([](const auto& x) { return x.fvs_file; })
×
567
                | lnav::itertools::find_if([&key](const auto& elem) {
×
568
                              return key.template get<std::string>()
UNCOV
569
                                  == elem->get_path_for_key();
×
570
                          })
UNCOV
571
                | lnav::itertools::deref();
×
572

UNCOV
573
            if (!lf_opt) {
×
UNCOV
574
                return;
×
575
            }
576

577
            this->to_front(lf_opt.value());
×
578
            this->tss_view->reload_data();
×
UNCOV
579
        });
×
580
    if (lf->size() == 0) {
11✔
UNCOV
581
        return;
×
582
    }
583

584
    if (lf->has_line_metadata()) {
11✔
585
        auto* lfo
586
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
587
        if (line < 0
2✔
588
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
2✔
589
        {
590
            return;
×
591
        }
592
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
2✔
593
        char ts[64];
594

595
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
596

597
        crumbs.emplace_back(
2✔
598
            std::string(ts),
4✔
599
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
600
            [](const auto& key) {});
2✔
601
    }
602

603
    if (this->tss_view_mode == view_mode::rendered
22✔
604
        && curr_iter->fvs_text_source)
11✔
605
    {
606
        curr_iter->fvs_text_source->text_crumbs_for_line(line, crumbs);
2✔
607
    } else if (curr_iter->fvs_metadata.m_sections_tree.empty()) {
9✔
608
    } else {
609
        auto* lfo
610
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
6✔
611
        if (line < 0
6✔
612
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
6✔
613
        {
UNCOV
614
            return;
×
615
        }
616
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
6✔
617
        auto ll_next_iter = ll_iter + 1;
6✔
618
        auto end_offset = (ll_next_iter == lf->end())
6✔
619
            ? lf->get_index_size() - 1
12✔
620
            : ll_next_iter->get_offset() - 1;
6✔
621
        const auto initial_size = crumbs.size();
6✔
622

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

672
                    crumbs.back().c_expected_input
1✔
673
                        = curr_node.value()
2✔
674
                              ->hn_parent->hn_named_children.empty()
1✔
675
                        ? breadcrumb::crumb::expected_input_t::index
1✔
676
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
677
                    crumbs.back().with_possible_range(
2✔
678
                        node | lnav::itertools::map([](const auto hn) {
1✔
679
                            return hn->hn_parent->hn_children.size();
1✔
680
                        })
681
                        | lnav::itertools::unwrap_or(size_t{0}));
2✔
682
                }
683
            });
8✔
684

685
        auto path = crumbs | lnav::itertools::skip(initial_size)
12✔
686
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
12✔
687
        auto node = lnav::document::hier_node::lookup_path(
6✔
688
            curr_iter->fvs_metadata.m_sections_root.get(), path);
6✔
689

690
        if (node && !node.value()->hn_children.empty()) {
6✔
691
            auto poss_provider = [curr_node = node.value()]() {
2✔
692
                std::vector<breadcrumb::possibility> retval;
2✔
693
                for (const auto& child : curr_node->hn_named_children) {
14✔
694
                    retval.emplace_back(child.first);
12✔
695
                }
696
                return retval;
2✔
697
            };
698
            auto path_performer = [this, curr_node = node.value()](
4✔
699
                                      const breadcrumb::crumb::key_t& value) {
UNCOV
700
                value.match(
×
UNCOV
701
                    [this, curr_node](const std::string& str) {
×
702
                        auto child_iter
UNCOV
703
                            = curr_node->hn_named_children.find(str);
×
UNCOV
704
                        if (child_iter != curr_node->hn_named_children.end()) {
×
UNCOV
705
                            this->set_top_from_off(
×
UNCOV
706
                                child_iter->second->hn_start);
×
707
                        }
UNCOV
708
                    },
×
UNCOV
709
                    [this, curr_node](size_t index) {
×
UNCOV
710
                        if (index >= curr_node->hn_children.size()) {
×
711
                            return;
×
712
                        }
UNCOV
713
                        auto* child = curr_node->hn_children[index].get();
×
714
                        this->set_top_from_off(child->hn_start);
×
715
                    });
716
            };
2✔
717
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
2✔
718
            crumbs.back().c_expected_input
2✔
719
                = node.value()->hn_named_children.empty()
4✔
720
                ? breadcrumb::crumb::expected_input_t::index
2✔
721
                : breadcrumb::crumb::expected_input_t::index_or_exact;
722
        }
723
    }
6✔
724
}
725

726
textfile_sub_source::rescan_result_t
727
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
4,264✔
728
                                  std::optional<ui_clock::time_point> deadline)
729
{
730
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,264✔
731

732
    file_iterator iter;
4,264✔
733
    rescan_result_t retval;
4,264✔
734
    size_t files_scanned = 0;
4,264✔
735

736
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,264✔
737
        return retval;
122✔
738
    }
739

740
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,142✔
741

742
    std::vector<std::shared_ptr<logfile>> closed_files;
4,142✔
743
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
5,355✔
744
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,213✔
745
        {
UNCOV
746
            log_info("rescan_files() deadline reached, breaking...");
×
UNCOV
747
            retval.rr_scan_completed = false;
×
UNCOV
748
            this->tss_last_scan_aborted = true;
×
UNCOV
749
            break;
×
750
        }
751

752
        std::shared_ptr<logfile> lf = iter->fvs_file;
1,213✔
753

754
        if (lf->is_closed()) {
1,213✔
755
            iter = this->tss_files.erase(iter);
97✔
756
            this->detach_observer(lf);
97✔
757
            closed_files.emplace_back(lf);
97✔
758
            retval.rr_rescan_needed = true;
97✔
759
            continue;
97✔
760
        }
761

762
        if (last_aborted && lf->size() > 0) {
1,116✔
UNCOV
763
            retval.rr_scan_completed = false;
×
UNCOV
764
            ++iter;
×
UNCOV
765
            continue;
×
766
        }
767
        files_scanned += 1;
1,116✔
768

769
        try {
770
            const auto& st = lf->get_stat();
1,116✔
771
            uint32_t old_size = lf->size();
1,116✔
772
            auto new_text_data = lf->rebuild_index(deadline);
1,116✔
773

774
            if (lf->get_format() != nullptr) {
1,116✔
775
                iter = this->tss_files.erase(iter);
501✔
776
                this->detach_observer(lf);
501✔
777
                callback.promote_file(lf);
501✔
778
                continue;
589✔
779
            }
780

781
            bool new_data = false;
615✔
782
            switch (new_text_data) {
615✔
783
                case logfile::rebuild_result_t::NEW_LINES:
83✔
784
                case logfile::rebuild_result_t::NEW_ORDER:
785
                    new_data = true;
83✔
786
                    retval.rr_new_data += 1;
83✔
787
                    break;
83✔
788
                case logfile::rebuild_result_t::NO_NEW_LINES:
532✔
789
                    this->move_to_init_location(iter);
532✔
790
                    break;
532✔
UNCOV
791
                default:
×
UNCOV
792
                    break;
×
793
            }
794
            callback.scanned_file(lf);
615✔
795

796
            if (lf->is_indexing()
615✔
797
                && lf->get_text_format() != text_format_t::TF_BINARY)
615✔
798
            {
799
                if (!new_data) {
588✔
800
                    // Only invalidate the meta if the file is small, or we
801
                    // found some meta previously.
802
                    if ((st.st_mtime != iter->fvs_mtime
510✔
803
                         || st.st_size != iter->fvs_file_size
497✔
804
                         || lf->get_index_size() != iter->fvs_file_indexed_size)
497✔
805
                        && (st.st_size < 10 * 1024 || iter->fvs_file_size == 0
1,007✔
UNCOV
806
                            || !iter->fvs_metadata.m_sections_tree.empty()))
×
807
                    {
808
                        log_debug(
13✔
809
                            "text file has changed, invalidating metadata.  "
810
                            "old: {mtime: %d size: %zu isize: %zu}, new: "
811
                            "{mtime: %d size: %zu isize: %zu}",
812
                            iter->fvs_mtime,
813
                            iter->fvs_file_size,
814
                            iter->fvs_file_indexed_size,
815
                            st.st_mtime,
816
                            st.st_size,
817
                            lf->get_index_size());
818
                        iter->fvs_metadata = {};
13✔
819
                        iter->fvs_error.clear();
13✔
820
                    }
821
                }
822

823
                if (!iter->fvs_metadata.m_sections_root
588✔
824
                    && iter->fvs_error.empty())
588✔
825
                {
826
                    auto read_res
827
                        = lf->read_file(logfile::read_format_t::with_framing);
91✔
828

829
                    if (read_res.isOk()) {
91✔
830
                        auto read_file_res = read_res.unwrap();
91✔
831

832
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf) {
91✔
UNCOV
833
                            log_error(
×
834
                                "%s: file has invalid UTF, skipping meta "
835
                                "discovery",
836
                                lf->get_path_for_key().c_str());
UNCOV
837
                            iter->fvs_mtime = st.st_mtime;
×
UNCOV
838
                            iter->fvs_file_size = lf->get_index_size();
×
839
                        } else {
840
                            auto content
841
                                = attr_line_t(read_file_res.rfr_content);
91✔
842

843
                            log_info("generating metadata for: %s (size=%zu)",
91✔
844
                                     lf->get_path_for_key().c_str(),
845
                                     content.length());
846
                            scrub_ansi_string(content.get_string(),
91✔
847
                                              &content.get_attrs());
91✔
848

849
                            auto text_meta = extract_text_meta(
850
                                content.get_string(), lf->get_text_format());
91✔
851
                            if (text_meta) {
91✔
852
                                lf->set_filename(text_meta->tfm_filename);
3✔
853
                                lf->set_include_in_session(true);
3✔
854
                                callback.renamed_file(lf);
3✔
855
                            }
856

857
                            iter->fvs_mtime = st.st_mtime;
91✔
858
                            iter->fvs_file_size = st.st_size;
91✔
859
                            iter->fvs_file_indexed_size = lf->get_index_size();
91✔
860
                            iter->fvs_metadata
91✔
861
                                = lnav::document::discover(content)
91✔
862
                                      .with_text_format(lf->get_text_format())
91✔
863
                                      .perform();
182✔
864
                        }
91✔
865
                    } else {
91✔
UNCOV
866
                        auto errmsg = read_res.unwrapErr();
×
UNCOV
867
                        log_error(
×
868
                            "%s: unable to read file for meta discover -- %s",
869
                            lf->get_path_for_key().c_str(),
870
                            errmsg.c_str());
UNCOV
871
                        iter->fvs_mtime = st.st_mtime;
×
UNCOV
872
                        iter->fvs_file_size = st.st_size;
×
UNCOV
873
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
874
                        iter->fvs_error = errmsg;
×
875
                    }
876
                }
91✔
877
            }
878

879
            uint32_t filter_in_mask, filter_out_mask;
880

881
            this->get_filters().get_enabled_mask(filter_in_mask,
615✔
882
                                                 filter_out_mask);
883
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
615✔
884
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,411✔
885
                if (this->tss_apply_filters
7,592✔
886
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
3,796✔
887
                {
UNCOV
888
                    continue;
×
889
                }
890
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
3,796✔
891
            }
892

893
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
615✔
894
                if (iter->fvs_text_source) {
81✔
895
                    if (iter->fvs_file_size == st.st_size
69✔
896
                        && iter->fvs_file_indexed_size == lf->get_index_size()
69✔
897
                        && iter->fvs_mtime == st.st_mtime)
138✔
898
                    {
899
                        ++iter;
69✔
900
                        continue;
69✔
901
                    }
UNCOV
902
                    log_info("markdown file has been updated, re-rendering: %s",
×
903
                             lf->get_path_for_key().c_str());
UNCOV
904
                    iter->fvs_text_source = nullptr;
×
905
                }
906

907
                auto read_res = lf->read_file(logfile::read_format_t::plain);
12✔
908
                if (read_res.isOk()) {
12✔
909
                    auto read_file_res = read_res.unwrap();
12✔
910
                    auto md_file = md4cpp::parse_file(
12✔
911
                        lf->get_filename(), read_file_res.rfr_content);
912
                    log_info("%s: rendering markdown content of size %d",
12✔
913
                             lf->get_basename().c_str(),
914
                             read_file_res.rfr_content.size());
915
                    md2attr_line mdal;
12✔
916

917
                    mdal.with_source_path(lf->get_actual_path());
12✔
918
                    if (this->tss_view->tc_interactive) {
12✔
UNCOV
919
                        mdal.add_lnav_script_icons();
×
920
                    }
921
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
12✔
922

923
                    iter->fvs_mtime = st.st_mtime;
12✔
924
                    iter->fvs_file_indexed_size = lf->get_index_size();
12✔
925
                    iter->fvs_file_size = st.st_size;
12✔
926
                    iter->fvs_text_source
12✔
927
                        = std::make_unique<plain_text_source>();
24✔
928
                    iter->fvs_text_source->set_text_format(
12✔
929
                        lf->get_text_format());
930
                    iter->fvs_text_source->register_view(this->tss_view);
12✔
931
                    if (parse_res.isOk()) {
12✔
932
                        auto& lf_meta = lf->get_embedded_metadata();
12✔
933

934
                        iter->fvs_text_source->replace_with(parse_res.unwrap());
12✔
935

936
                        if (!md_file.f_frontmatter.empty()) {
12✔
937
                            lf_meta["net.daringfireball.markdown.frontmatter"]
8✔
938
                                = {
939
                                    md_file.f_frontmatter_format,
4✔
940
                                    md_file.f_frontmatter.to_string(),
941
                                };
8✔
942
                        }
943

944
                        lnav::events::publish(
12✔
945
                            lnav_db,
946
                            lnav::events::file::format_detected{
36✔
947
                                lf->get_filename(),
12✔
948
                                fmt::to_string(lf->get_text_format()),
24✔
949
                            });
950
                    } else {
951
                        auto view_content
UNCOV
952
                            = lnav::console::user_message::error(
×
953
                                  "unable to parse markdown file")
UNCOV
954
                                  .with_reason(parse_res.unwrapErr())
×
UNCOV
955
                                  .to_attr_line();
×
UNCOV
956
                        view_content.append("\n").append(
×
UNCOV
957
                            attr_line_t::from_ansi_str(
×
958
                                read_file_res.rfr_content.c_str()));
959

960
                        iter->fvs_text_source->replace_with(view_content);
×
961
                    }
962
                } else {
12✔
963
                    log_error("unable to read markdown file: %s -- %s",
×
964
                              lf->get_path_for_key().c_str(),
965
                              read_res.unwrapErr().c_str());
966
                }
967
            } else if (file_needs_reformatting(lf) && !new_data) {
546✔
968
                if (iter->fvs_file_size == st.st_size
23✔
969
                    && iter->fvs_file_indexed_size == lf->get_index_size()
23✔
970
                    && iter->fvs_mtime == st.st_mtime
23✔
971
                    && (!iter->fvs_error.empty()
69✔
972
                        || iter->fvs_text_source != nullptr))
23✔
973
                {
974
                    ++iter;
19✔
975
                    continue;
19✔
976
                }
977
                log_info("pretty file has been updated, re-rendering: %s",
4✔
978
                         lf->get_path_for_key().c_str());
979
                iter->fvs_text_source = nullptr;
4✔
980
                iter->fvs_error.clear();
4✔
981

982
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
983
                if (read_res.isOk()) {
4✔
984
                    auto read_file_res = read_res.unwrap();
4✔
985
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
986
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
987
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
988
                        data_scanner ds(orig_al.al_string);
4✔
989
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
990
                        attr_line_t pretty_al;
4✔
991

992
                        pp.append_to(pretty_al);
4✔
993
                        iter->fvs_mtime = st.st_mtime;
4✔
994
                        iter->fvs_file_indexed_size = lf->get_index_size();
4✔
995
                        iter->fvs_file_size = st.st_size;
4✔
996
                        iter->fvs_text_source
4✔
997
                            = std::make_unique<plain_text_source>();
8✔
998
                        iter->fvs_text_source->set_text_format(
4✔
999
                            lf->get_text_format());
1000
                        iter->fvs_text_source->register_view(this->tss_view);
4✔
1001
                        iter->fvs_text_source->replace_with_mutable(
4✔
1002
                            pretty_al, lf->get_text_format());
1003
                    } else {
4✔
UNCOV
1004
                        log_error(
×
1005
                            "unable to read file to pretty-print: %s -- file "
1006
                            "is not valid UTF-8",
1007
                            lf->get_path_for_key().c_str());
UNCOV
1008
                        iter->fvs_mtime = st.st_mtime;
×
UNCOV
1009
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
UNCOV
1010
                        iter->fvs_file_size = st.st_size;
×
UNCOV
1011
                        iter->fvs_error = "file is not valid UTF-8";
×
1012
                    }
1013
                } else {
4✔
UNCOV
1014
                    auto errmsg = read_res.unwrapErr();
×
UNCOV
1015
                    log_error("unable to read file to pretty-print: %s -- %s",
×
1016
                              lf->get_path_for_key().c_str(),
1017
                              errmsg.c_str());
1018
                    iter->fvs_mtime = st.st_mtime;
×
1019
                    iter->fvs_file_indexed_size = lf->get_index_size();
×
UNCOV
1020
                    iter->fvs_file_size = st.st_size;
×
UNCOV
1021
                    iter->fvs_error = errmsg;
×
1022
                }
1023
            }
4✔
UNCOV
1024
        } catch (const line_buffer::error& e) {
×
UNCOV
1025
            iter = this->tss_files.erase(iter);
×
1026
            lf->close();
×
1027
            this->detach_observer(lf);
×
1028
            closed_files.emplace_back(lf);
×
1029
            continue;
×
UNCOV
1030
        }
×
1031

1032
        ++iter;
527✔
1033
    }
1,213✔
1034
    if (!closed_files.empty()) {
4,142✔
1035
        callback.closed_files(closed_files);
93✔
1036
        if (!this->tss_files.empty()) {
93✔
1037
            this->tss_files.front().load_into(*this->tss_view);
×
1038
        }
1039
        this->tss_view->set_needs_update();
93✔
1040
    }
1041

1042
    if (retval.rr_new_data) {
4,142✔
1043
        this->tss_view->search_new_data();
79✔
1044
    }
1045

1046
    return retval;
4,142✔
1047
}
4,159✔
1048

1049
void
UNCOV
1050
textfile_sub_source::set_top_from_off(file_off_t off)
×
1051
{
UNCOV
1052
    auto lf = this->current_file();
×
1053

UNCOV
1054
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
UNCOV
1055
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
UNCOV
1056
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
UNCOV
1057
            std::distance(lf->cbegin(), new_top_iter));
×
1058

UNCOV
1059
        if (new_top_opt) {
×
1060
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
UNCOV
1061
            if (this->tss_view->is_selectable()) {
×
1062
                this->tss_view->set_top(
×
1063
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1064
            }
1065
        }
1066
    };
1067
}
1068

1069
void
1070
textfile_sub_source::quiesce()
×
1071
{
UNCOV
1072
    for (auto& lf : this->tss_files) {
×
UNCOV
1073
        lf.fvs_file->quiesce();
×
1074
    }
1075
}
1076

1077
size_t
1078
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
15,031✔
1079
{
1080
    size_t retval = 0;
15,031✔
1081

1082
    if (mode == view_mode::raw || !this->fvs_text_source) {
15,031✔
1083
        const auto& lf = this->fvs_file;
9,716✔
1084
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
9,716✔
1085
            const auto fsize = lf->get_content_size();
2,975✔
1086
            retval = fsize / 16;
2,975✔
1087
            if (fsize % 16) {
2,975✔
1088
                retval += 1;
2,975✔
1089
            }
1090
        } else {
1091
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
6,741✔
1092
            if (lfo != nullptr) {
6,741✔
1093
                retval = lfo->lfo_filter_state.tfs_index.size();
6,741✔
1094
            }
1095
        }
1096
    } else {
1097
        retval = this->fvs_text_source->text_line_count();
5,315✔
1098
    }
1099

1100
    return retval;
15,031✔
1101
}
1102

1103
std::optional<vis_line_t>
1104
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
6✔
1105
                                                     const std::string& id)
1106
{
1107
    if (mode == view_mode::rendered && this->fvs_text_source) {
6✔
1108
        return this->fvs_text_source->row_for_anchor(id);
4✔
1109
    }
1110

1111
    if (!this->fvs_metadata.m_sections_root) {
2✔
UNCOV
1112
        return std::nullopt;
×
1113
    }
1114

1115
    const auto& lf = this->fvs_file;
2✔
1116
    const auto& meta = this->fvs_metadata;
2✔
1117
    std::optional<vis_line_t> retval;
2✔
1118

1119
    auto is_ptr = startswith(id, "#/");
2✔
1120
    if (is_ptr) {
2✔
1121
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1122
        std::vector<lnav::document::section_key_t> path;
2✔
1123

1124
        while (!hier_sf.empty()) {
8✔
1125
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
6✔
1126
            auto scan_res
1127
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
6✔
1128
            if (scan_res && scan_res->range().empty()) {
6✔
1129
                path.emplace_back(scan_res->value());
2✔
1130
            } else {
1131
                stack_buf allocator;
4✔
1132
                path.emplace_back(
4✔
1133
                    json_ptr::decode(comp_pair.first, allocator).to_string());
8✔
1134
            }
4✔
1135
            hier_sf = comp_pair.second;
6✔
1136
        }
1137

1138
        auto lookup_res = lnav::document::hier_node::lookup_path(
2✔
1139
            meta.m_sections_root.get(), path);
2✔
1140
        if (lookup_res) {
2✔
1141
            auto ll_opt = lf->line_for_offset(lookup_res.value()->hn_start);
2✔
1142
            if (ll_opt != lf->end()) {
2✔
1143
                retval
1144
                    = vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1145
            }
1146
        }
1147

1148
        return retval;
2✔
1149
    }
2✔
1150

1151
    lnav::document::hier_node::depth_first(
×
1152
        meta.m_sections_root.get(),
UNCOV
1153
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1154
            for (const auto& child_pair : node->hn_named_children) {
×
UNCOV
1155
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1156

UNCOV
1157
                if (child_anchor != id) {
×
1158
                    continue;
×
1159
                }
1160

UNCOV
1161
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1162
                if (ll_opt != lf->end()) {
×
UNCOV
1163
                    retval = vis_line_t(
×
UNCOV
1164
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1165
                }
UNCOV
1166
                break;
×
1167
            }
1168
        });
×
1169

1170
    return retval;
×
1171
}
1172

1173
std::optional<vis_line_t>
1174
textfile_sub_source::row_for_anchor(const std::string& id)
5✔
1175
{
1176
    const auto curr_iter = this->current_file_state();
5✔
1177
    if (curr_iter == this->tss_files.end() || id.empty()) {
5✔
UNCOV
1178
        return std::nullopt;
×
1179
    }
1180

1181
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
5✔
1182
}
1183

1184
static void
UNCOV
1185
anchor_generator(std::unordered_set<std::string>& retval,
×
1186
                 std::vector<std::string>& comps,
1187
                 size_t& max_depth,
1188
                 lnav::document::hier_node* hn)
1189
{
1190
    if (hn->hn_named_children.empty()) {
×
UNCOV
1191
        if (hn->hn_children.empty()) {
×
1192
            if (retval.size() >= 250 || comps.empty()) {
×
1193
            } else if (comps.size() == 1) {
×
1194
                retval.emplace(text_anchors::to_anchor_string(comps.front()));
×
1195
            } else {
UNCOV
1196
                retval.emplace(
×
UNCOV
1197
                    fmt::format(FMT_STRING("#/{}"),
×
UNCOV
1198
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1199
            }
UNCOV
1200
            max_depth = std::max(max_depth, comps.size());
×
1201
        } else {
UNCOV
1202
            int index = 0;
×
1203
            for (const auto& child : hn->hn_children) {
×
UNCOV
1204
                comps.emplace_back(fmt::to_string(index));
×
1205
                anchor_generator(retval, comps, max_depth, child.get());
×
1206
                comps.pop_back();
×
1207
            }
1208
        }
1209
    } else {
1210
        for (const auto& [child_name, child_node] : hn->hn_named_children) {
×
1211
            comps.emplace_back(child_name);
×
UNCOV
1212
            anchor_generator(retval, comps, max_depth, child_node);
×
1213
            comps.pop_back();
×
1214
        }
UNCOV
1215
        if (max_depth > 1) {
×
1216
            retval.emplace(
×
1217
                fmt::format(FMT_STRING("#/{}"),
×
1218
                            fmt::join(comps.begin(), comps.end(), "/")));
×
1219
        }
1220
    }
1221
}
1222

1223
std::unordered_set<std::string>
UNCOV
1224
textfile_sub_source::get_anchors()
×
1225
{
UNCOV
1226
    std::unordered_set<std::string> retval;
×
1227

UNCOV
1228
    const auto curr_iter = this->current_file_state();
×
1229
    if (curr_iter == this->tss_files.end()) {
×
UNCOV
1230
        return retval;
×
1231
    }
1232

UNCOV
1233
    if (this->tss_view_mode == view_mode::rendered
×
UNCOV
1234
        && curr_iter->fvs_text_source)
×
1235
    {
UNCOV
1236
        return curr_iter->fvs_text_source->get_anchors();
×
1237
    }
1238

UNCOV
1239
    const auto& meta = curr_iter->fvs_metadata;
×
1240
    if (meta.m_sections_root == nullptr) {
×
UNCOV
1241
        return retval;
×
1242
    }
1243

1244
    std::vector<std::string> comps;
×
UNCOV
1245
    size_t max_depth = 0;
×
UNCOV
1246
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1247

1248
    return retval;
×
1249
}
1250

1251
struct tfs_time_cmp {
1252
    bool operator()(int32_t lhs, const timeval& rhs) const
×
1253
    {
UNCOV
1254
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
×
1255
        return ll->get_timeval() < rhs;
×
1256
    }
1257

1258
    logfile* ttc_logfile;
1259
    std::vector<uint32_t>& ttc_index;
1260
};
1261

1262
std::optional<vis_line_t>
UNCOV
1263
textfile_sub_source::row_for_time(timeval time_bucket)
×
1264
{
UNCOV
1265
    auto lf = this->current_file();
×
UNCOV
1266
    if (!lf || !lf->has_line_metadata()) {
×
UNCOV
1267
        return std::nullopt;
×
1268
    }
1269

UNCOV
1270
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
UNCOV
1271
    auto& tfs = lfo->lfo_filter_state.tfs_index;
×
UNCOV
1272
    auto lb = std::lower_bound(
×
UNCOV
1273
        tfs.begin(), tfs.end(), time_bucket, tfs_time_cmp{lf.get(), tfs});
×
UNCOV
1274
    if (lb != tfs.end()) {
×
UNCOV
1275
        return vis_line_t{(int) std::distance(tfs.begin(), lb)};
×
1276
    }
1277

UNCOV
1278
    return std::nullopt;
×
1279
}
1280

1281
std::optional<text_time_translator::row_info>
1282
textfile_sub_source::time_for_row(vis_line_t row)
31✔
1283
{
1284
    auto lf = this->current_file();
31✔
1285
    if (!lf || !lf->has_line_metadata()) {
31✔
1286
        return std::nullopt;
27✔
1287
    }
1288

1289
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
4✔
1290
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
4✔
UNCOV
1291
        return std::nullopt;
×
1292
    }
1293
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
4✔
1294
    auto ll_iter = lf->begin() + row_id;
4✔
1295
    return row_info{
8✔
1296
        ll_iter->get_timeval(),
1297
        row_id,
1298
    };
4✔
1299
}
31✔
1300

1301
static std::optional<vis_line_t>
1302
to_vis_line(const std::shared_ptr<logfile>& lf, file_off_t off)
2✔
1303
{
1304
    auto ll_opt = lf->line_for_offset(off);
2✔
1305
    if (ll_opt != lf->end()) {
2✔
1306
        return vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1307
    }
1308

1309
    return std::nullopt;
×
1310
}
1311

1312
std::optional<vis_line_t>
1313
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
2✔
1314
{
1315
    const auto curr_iter = this->current_file_state();
2✔
1316
    if (curr_iter == this->tss_files.end()) {
2✔
UNCOV
1317
        return std::nullopt;
×
1318
    }
1319

1320
    const auto& lf = curr_iter->fvs_file;
2✔
1321
    log_debug("adjacent_anchor: %s:L%d:%s",
2✔
1322
              lf->get_path_for_key().c_str(),
1323
              vl,
1324
              dir == text_anchors::direction::prev ? "prev" : "next");
1325
    if (this->tss_view_mode == view_mode::rendered
4✔
1326
        && curr_iter->fvs_text_source)
2✔
1327
    {
UNCOV
1328
        return curr_iter->fvs_text_source->adjacent_anchor(vl, dir);
×
1329
    }
1330

1331
    if (!curr_iter->fvs_metadata.m_sections_root) {
2✔
1332
        log_debug("  no metadata available");
×
UNCOV
1333
        return std::nullopt;
×
1334
    }
1335

1336
    auto& md = curr_iter->fvs_metadata;
2✔
1337
    const auto* lfo
1338
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
1339
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
2✔
1340
        || md.m_sections_root == nullptr)
2✔
1341
    {
UNCOV
1342
        return std::nullopt;
×
1343
    }
1344
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
2✔
1345
    const auto line_offsets = lf->get_file_range(ll_iter, false);
2✔
1346
    log_debug(
2✔
1347
        "  range %d:%d", line_offsets.fr_offset, line_offsets.next_offset());
1348
    auto path_for_line
1349
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
2✔
1350

1351
    if (path_for_line.empty()) {
2✔
1352
        log_debug("  no path found");
×
UNCOV
1353
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
UNCOV
1354
        if (!neighbors_res) {
×
1355
            return std::nullopt;
×
1356
        }
1357

UNCOV
1358
        switch (dir) {
×
UNCOV
1359
            case direction::prev: {
×
UNCOV
1360
                if (neighbors_res->cnr_previous) {
×
UNCOV
1361
                    return to_vis_line(
×
UNCOV
1362
                        lf, neighbors_res->cnr_previous.value()->hn_start);
×
1363
                }
UNCOV
1364
                break;
×
1365
            }
UNCOV
1366
            case direction::next: {
×
1367
                if (neighbors_res->cnr_next) {
×
UNCOV
1368
                    return to_vis_line(
×
UNCOV
1369
                        lf, neighbors_res->cnr_next.value()->hn_start);
×
1370
                }
UNCOV
1371
                if (!md.m_sections_root->hn_children.empty()) {
×
UNCOV
1372
                    return to_vis_line(
×
UNCOV
1373
                        lf, md.m_sections_root->hn_children[0]->hn_start);
×
1374
                }
1375
                break;
×
1376
            }
1377
        }
UNCOV
1378
        return std::nullopt;
×
1379
    }
1380

1381
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
2✔
1382
    const auto last_key = std::move(path_for_line.back());
2✔
1383
    path_for_line.pop_back();
2✔
1384

1385
    const auto parent_opt = lnav::document::hier_node::lookup_path(
2✔
1386
        md.m_sections_root.get(), path_for_line);
2✔
1387
    if (!parent_opt) {
2✔
UNCOV
1388
        log_debug("  no parent for path: %s",
×
1389
                  fmt::to_string(path_for_line).c_str());
UNCOV
1390
        return std::nullopt;
×
1391
    }
1392
    const auto parent = parent_opt.value();
2✔
1393

1394
    const auto child_hn = parent->lookup_child(last_key);
2✔
1395
    if (!child_hn) {
2✔
1396
        // XXX "should not happen"
UNCOV
1397
        log_debug("  child not found");
×
UNCOV
1398
        return std::nullopt;
×
1399
    }
1400

1401
    auto neighbors_res = parent->child_neighbors(
6✔
1402
        child_hn.value(), line_offsets.next_offset() + 1);
2✔
1403
    if (!neighbors_res) {
2✔
UNCOV
1404
        log_debug("  no neighbors found");
×
1405
        return std::nullopt;
×
1406
    }
1407

1408
    log_debug("  neighbors p:%d n:%d",
2✔
1409
              neighbors_res->cnr_previous.has_value(),
1410
              neighbors_res->cnr_next.has_value());
1411
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
2✔
1412
        auto neighbor_sub
1413
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
2✔
1414
        if (neighbor_sub) {
2✔
UNCOV
1415
            neighbors_res->cnr_previous = neighbor_sub;
×
1416
        }
1417
    }
1418

1419
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
2✔
1420
        auto neighbor_sub
1421
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
2✔
1422
        if (neighbor_sub) {
2✔
1423
            neighbors_res->cnr_next = neighbor_sub;
2✔
1424
        }
1425
    }
1426

1427
    switch (dir) {
2✔
UNCOV
1428
        case direction::prev: {
×
1429
            if (neighbors_res->cnr_previous) {
×
UNCOV
1430
                return to_vis_line(
×
UNCOV
1431
                    lf, neighbors_res->cnr_previous.value()->hn_start);
×
1432
            }
UNCOV
1433
            break;
×
1434
        }
1435
        case direction::next: {
2✔
1436
            if (neighbors_res->cnr_next) {
2✔
1437
                return to_vis_line(lf,
2✔
1438
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1439
            }
UNCOV
1440
            break;
×
1441
        }
1442
    }
1443

UNCOV
1444
    return std::nullopt;
×
1445
}
2✔
1446

1447
std::optional<std::string>
1448
textfile_sub_source::anchor_for_row(vis_line_t vl)
12✔
1449
{
1450
    const auto curr_iter = this->current_file_state();
12✔
1451
    if (curr_iter == this->tss_files.end()) {
12✔
UNCOV
1452
        return std::nullopt;
×
1453
    }
1454

1455
    if (this->tss_view_mode == view_mode::rendered
24✔
1456
        && curr_iter->fvs_text_source)
12✔
1457
    {
1458
        return curr_iter->fvs_text_source->anchor_for_row(vl);
3✔
1459
    }
1460

1461
    if (!curr_iter->fvs_metadata.m_sections_root) {
9✔
UNCOV
1462
        return std::nullopt;
×
1463
    }
1464

1465
    const auto& lf = curr_iter->fvs_file;
9✔
1466
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
9✔
1467
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
9✔
1468
        return std::nullopt;
×
1469
    }
1470
    auto& md = curr_iter->fvs_metadata;
9✔
1471
    auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
9✔
1472
    auto line_offsets = lf->get_file_range(ll_iter, false);
9✔
1473
    auto path_for_line
1474
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
9✔
1475

1476
    if (path_for_line.empty()) {
9✔
1477
        return std::nullopt;
5✔
1478
    }
1479

1480
    if ((path_for_line.size() == 1
4✔
1481
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1482
        && path_for_line.back().is<std::string>())
6✔
1483
    {
1484
        return text_anchors::to_anchor_string(
2✔
1485
            path_for_line.back().get<std::string>());
2✔
1486
    }
1487

1488
    auto comps
1489
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1490
              return elem.match(
UNCOV
1491
                  [](const std::string& str) {
×
1492
                      stack_buf allocator;
5✔
1493
                      return json_ptr::encode(str, allocator).to_string();
10✔
1494
                  },
5✔
1495
                  [](size_t index) { return fmt::to_string(index); });
13✔
1496
          });
2✔
1497

1498
    return fmt::format(FMT_STRING("#/{}"),
8✔
1499
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1500
}
9✔
1501

1502
bool
1503
textfile_sub_source::to_front(const std::string& filename)
1✔
1504
{
1505
    auto lf_opt = this->tss_files
1✔
1506
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1507
                      return elem.fvs_file->get_filename() == filename;
1✔
1508
                  });
1✔
1509
    if (!lf_opt) {
1✔
1510
        return false;
1✔
1511
    }
1512

UNCOV
1513
    this->to_front(lf_opt.value()->fvs_file);
×
1514

UNCOV
1515
    return true;
×
1516
}
1517

1518
logline*
1519
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1520
{
1521
    auto lf = this->current_file();
3✔
1522
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1523
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1524
}
3✔
1525

1526
void
1527
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1528
{
1529
    this->tss_view_mode = vm;
2✔
1530
    this->tss_view->set_needs_update();
2✔
1531
}
2✔
1532

1533
textfile_sub_source::view_mode
1534
textfile_sub_source::get_effective_view_mode() const
358✔
1535
{
1536
    auto retval = view_mode::raw;
358✔
1537

1538
    const auto curr_iter = this->current_file_state();
358✔
1539
    if (curr_iter != this->tss_files.end()) {
358✔
1540
        if (this->tss_view_mode == view_mode::rendered
716✔
1541
            && curr_iter->fvs_text_source)
358✔
1542
        {
1543
            retval = view_mode::rendered;
19✔
1544
        }
1545
    }
1546

1547
    return retval;
358✔
1548
}
1549

1550
void
1551
textfile_sub_source::move_to_init_location(file_iterator& iter)
532✔
1552
{
1553
    if (iter->fvs_consumed_init_location) {
532✔
1554
        return;
436✔
1555
    }
1556

1557
    auto& lf = iter->fvs_file;
96✔
1558
    std::optional<vis_line_t> new_sel_opt;
96✔
1559
    require(lf->get_open_options().loo_init_location.valid());
96✔
1560
    lf->get_open_options().loo_init_location.match(
96✔
1561
        [this, &new_sel_opt, &lf](default_for_text_format def) {
×
1562
            if (!this->tss_apply_default_init_location) {
93✔
1563
                return;
93✔
1564
            }
1565
            switch (lf->get_text_format()) {
×
UNCOV
1566
                case text_format_t::TF_UNKNOWN:
×
1567
                case text_format_t::TF_LOG: {
1568
                    log_info("file open request to tail");
×
1569
                    auto inner_height = lf->size();
×
1570
                    if (inner_height > 0) {
×
1571
                        new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1572
                    }
UNCOV
1573
                    break;
×
1574
                }
1575
                default:
×
1576
                    log_info("file open is %s, moving to top",
×
1577
                             fmt::to_string(lf->get_text_format()).c_str());
1578
                    new_sel_opt = 0_vl;
×
1579
                    break;
×
1580
            }
1581
        },
1582
        [&new_sel_opt, &lf](file_location_tail tail) {
×
1583
            log_info("file open request to tail");
×
1584
            auto inner_height = lf->size();
×
1585
            if (inner_height > 0) {
×
1586
                new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1587
            }
1588
        },
×
UNCOV
1589
        [this, &new_sel_opt, &iter](int vl) {
×
1590
            log_info("file open request to jump to line: %d", vl);
2✔
1591
            auto height = iter->text_line_count(this->tss_view_mode);
2✔
1592
            if (vl < 0) {
2✔
1593
                vl += height;
2✔
1594
                if (vl < 0) {
2✔
1595
                    vl = 0;
1✔
1596
                }
1597
            }
1598
            if (vl < height) {
2✔
1599
                new_sel_opt = vis_line_t(vl);
2✔
1600
            }
1601
        },
2✔
1602
        [this, &new_sel_opt, &iter](const std::string& loc) {
96✔
1603
            log_info("file open request to jump to anchor: %s", loc.c_str());
1✔
1604
            new_sel_opt = iter->row_for_anchor(this->tss_view_mode, loc);
1✔
1605
        });
1✔
1606

1607
    if (new_sel_opt) {
96✔
1608
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1609
        log_info("  setting requested selection: %d",
3✔
1610
                 (int) new_sel_opt.value());
1611
        iter->fvs_selection = new_sel_opt.value();
3✔
1612
        log_info("  actual top is now: %d", (int) iter->fvs_top);
3✔
1613
        log_info("  actual selection is now: %d", (int) iter->fvs_selection);
3✔
1614

1615
        if (this->current_file() == lf) {
3✔
1616
            this->tss_view->set_selection(iter->fvs_selection);
3✔
1617
        }
1618
    }
1619
    iter->fvs_consumed_init_location = true;
96✔
1620
}
1621

1622
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
607✔
1623
                                                 text_sub_source* log_src)
607✔
1624
    : tho_src(src), tho_log_src(log_src)
607✔
1625
{
1626
}
607✔
1627

1628
bool
1629
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
9,435✔
1630
                                             media_t media,
1631
                                             int y,
1632
                                             int bottom,
1633
                                             attr_line_t& value_out)
1634
{
1635
    if (media == media_t::display) {
9,435✔
1636
        const std::vector<attr_line_t>* lines = nullptr;
9,380✔
1637
        auto curr_file = this->tho_src->current_file();
9,380✔
1638
        if (curr_file == nullptr) {
9,380✔
UNCOV
1639
            if (this->tho_log_src->text_line_count() == 0) {
×
UNCOV
1640
                lines = lnav::messages::view::no_files();
×
1641
            } else {
UNCOV
1642
                lines = lnav::messages::view::only_log_files();
×
1643
            }
1644
        } else if (!curr_file->get_notes().empty()) {
9,380✔
UNCOV
1645
            this->tho_static_lines = curr_file->get_notes()
×
UNCOV
1646
                                         .begin()
×
UNCOV
1647
                                         ->second.to_attr_line()
×
UNCOV
1648
                                         .split_lines();
×
UNCOV
1649
            lines = &this->tho_static_lines;
×
1650
        } else if (curr_file->size() == 0) {
9,380✔
UNCOV
1651
            lines = lnav::messages::view::empty_file();
×
1652
        } else if (this->tho_src->text_line_count() == 0) {
9,380✔
UNCOV
1653
            hasher h;
×
UNCOV
1654
            this->tho_src->update_filter_hash_state(h);
×
UNCOV
1655
            auto curr_state = h.to_array();
×
UNCOV
1656
            if (this->tho_static_lines.empty()
×
UNCOV
1657
                || curr_state != this->tho_filter_state)
×
1658
            {
1659
                auto msg = lnav::console::user_message::info(
UNCOV
1660
                    "All text lines are currently hidden");
×
UNCOV
1661
                auto min_time = this->tho_src->get_min_row_time();
×
UNCOV
1662
                if (min_time) {
×
UNCOV
1663
                    msg.with_note(attr_line_t("Lines before ")
×
UNCOV
1664
                                      .append_quoted(lnav::to_rfc3339_string(
×
UNCOV
1665
                                          min_time.value()))
×
UNCOV
1666
                                      .append(" are not being shown"));
×
1667
                }
UNCOV
1668
                auto max_time = this->tho_src->get_max_row_time();
×
UNCOV
1669
                if (max_time) {
×
UNCOV
1670
                    msg.with_note(attr_line_t("Lines after ")
×
UNCOV
1671
                                      .append_quoted(lnav::to_rfc3339_string(
×
UNCOV
1672
                                          max_time.value()))
×
UNCOV
1673
                                      .append(" are not being shown"));
×
1674
                }
UNCOV
1675
                auto& fs = this->tho_src->get_filters();
×
UNCOV
1676
                for (const auto& filt : fs) {
×
UNCOV
1677
                    auto hits = this->tho_src->get_filtered_count_for(
×
1678
                        filt->get_index());
UNCOV
1679
                    if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
UNCOV
1680
                        continue;
×
1681
                    }
UNCOV
1682
                    auto cmd = attr_line_t(":" + filt->to_command());
×
UNCOV
1683
                    readline_command_highlighter(cmd, std::nullopt);
×
UNCOV
1684
                    msg.with_note(
×
UNCOV
1685
                        attr_line_t("Filter ")
×
UNCOV
1686
                            .append_quoted(cmd)
×
UNCOV
1687
                            .append(" matched ")
×
UNCOV
1688
                            .append(lnav::roles::number(fmt::to_string(hits)))
×
UNCOV
1689
                            .append(" line(s) "));
×
1690
                }
UNCOV
1691
                this->tho_static_lines = msg.to_attr_line().split_lines();
×
UNCOV
1692
                this->tho_filter_state = curr_state;
×
1693
            }
1694

UNCOV
1695
            lines = &this->tho_static_lines;
×
1696
        }
1697

1698
        if (lines != nullptr && y < (ssize_t) lines->size()) {
9,380✔
UNCOV
1699
            value_out = lines->at(y);
×
UNCOV
1700
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
UNCOV
1701
            if (y == (ssize_t) lines->size() - 1) {
×
UNCOV
1702
                value_out.with_attr_for_all(
×
UNCOV
1703
                    VC_STYLE.value(text_attrs::with_underline()));
×
1704
            }
UNCOV
1705
            return true;
×
1706
        }
1707
    }
9,380✔
1708

1709
    if (y != 0) {
9,435✔
1710
        return false;
8,958✔
1711
    }
1712

1713
    const auto lf = this->tho_src->current_file();
477✔
1714
    if (lf == nullptr) {
477✔
UNCOV
1715
        return false;
×
1716
    }
1717

1718
    if (media == media_t::display
477✔
1719
        && lf->get_text_format() != text_format_t::TF_MARKDOWN
427✔
1720
        && this->tho_src->get_effective_view_mode()
904✔
1721
            == textfile_sub_source::view_mode::rendered)
1722
    {
1723
        auto ta = text_attrs::with_underline();
19✔
1724
        value_out.append("\u24d8"_info)
19✔
1725
            .append(" The following is a rendered view of the content.  Use ")
19✔
1726
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
19✔
1727
            .append(" to view the raw version of this text")
19✔
1728
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
38✔
1729
            .with_attr_for_all(VC_STYLE.value(ta));
19✔
1730
        return true;
19✔
1731
    }
1732

1733
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
458✔
1734
        return false;
431✔
1735
    }
1736

1737
    {
1738
        attr_line_builder alb(value_out);
27✔
1739
        {
1740
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1741
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
81✔
1742
        }
27✔
1743
        size_t byte_off = 0;
27✔
1744
        for (size_t lpc = 0; lpc < 16; lpc++) {
459✔
1745
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
432✔
1746
            if (byte_off == 8) {
432✔
1747
                alb.append(" ");
27✔
1748
            }
1749
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
1,296✔
1750
            byte_off += 1;
432✔
1751
        }
432✔
1752
        {
1753
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1754
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
81✔
1755
        }
27✔
1756
    }
1757
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
27✔
1758
    return true;
27✔
1759
}
477✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc