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

tstack / lnav / 19612931027-2708

23 Nov 2025 02:52PM UTC coverage: 68.84% (-0.04%) from 68.876%
19612931027-2708

push

github

tstack
[tests] fix piper test

51153 of 74307 relevant lines covered (68.84%)

432164.33 hits per line

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

66.36
/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
#include <unordered_set>
33

34
#include "textfile_sub_source.hh"
35

36
#include <date/date.h>
37

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

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

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

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

71
    switch (lf->get_text_format()) {
558✔
72
        case text_format_t::TF_BINARY:
43✔
73
        case text_format_t::TF_DIFF:
74
            return false;
43✔
75
        default:
515✔
76
            if (lf->get_content_size() < 16 * 1024
515✔
77
                && lf->get_longest_line_length()
1,030✔
78
                    > cfg.c_max_unformatted_line_length)
515✔
79
            {
80
                return true;
27✔
81
            }
82
            return false;
488✔
83
    }
84
}
85

86
size_t
87
textfile_sub_source::text_line_count()
23,103✔
88
{
89
    size_t retval = 0;
23,103✔
90

91
    if (!this->tss_files.empty()) {
23,103✔
92
        retval
93
            = this->current_file_state()->text_line_count(this->tss_view_mode);
15,637✔
94
    }
95

96
    return retval;
23,103✔
97
}
98

99
size_t
100
textfile_sub_source::text_line_width(textview_curses& tc)
5,686✔
101
{
102
    const auto iter = this->current_file_state();
5,686✔
103
    if (iter == this->tss_files.end()) {
5,686✔
104
        return 0;
5,111✔
105
    }
106

107
    return iter->text_line_width(this->tss_view_mode, tc);
575✔
108
}
109

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

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

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

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

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

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

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

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

201
    return {};
713✔
202
}
713✔
203

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

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

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

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

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

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

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

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

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

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

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

351
    return retval;
×
352
}
353

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

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

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

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

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

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

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

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

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

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

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

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

479
    const auto& lf = curr_iter->fvs_file;
104✔
480
    if (this->tss_view_mode == view_mode::rendered
208✔
481
        && curr_iter->fvs_text_source)
104✔
482
    {
483
        return;
20✔
484
    }
485

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

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

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

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

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

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

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

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

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

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

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

550
    return retval;
22✔
551
}
×
552

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

559
    if (this->empty()) {
11✔
560
        return;
×
561
    }
562

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

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

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

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

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

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

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

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

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

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

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

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

744
    file_iterator iter;
4,423✔
745
    rescan_result_t retval;
4,423✔
746
    size_t files_scanned = 0;
4,423✔
747

748
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,423✔
749
        return retval;
124✔
750
    }
751

752
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,299✔
753

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

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

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

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

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

786
            if (lf->get_format() != nullptr) {
1,163✔
787
                iter = this->tss_files.erase(iter);
517✔
788
                this->detach_observer(lf);
517✔
789
                callback.promote_file(lf);
517✔
790
                continue;
611✔
791
            }
792

793
            bool new_data = false;
646✔
794
            switch (new_text_data) {
646✔
795
                case logfile::rebuild_result_t::NEW_LINES:
87✔
796
                case logfile::rebuild_result_t::NEW_ORDER:
797
                    new_data = true;
87✔
798
                    retval.rr_new_data += 1;
87✔
799
                    break;
87✔
800
                case logfile::rebuild_result_t::NO_NEW_LINES:
559✔
801
                    this->move_to_init_location(iter);
559✔
802
                    break;
559✔
803
                default:
×
804
                    break;
×
805
            }
806
            callback.scanned_file(lf);
646✔
807

808
            if (lf->is_indexing()
646✔
809
                && lf->get_text_format() != text_format_t::TF_BINARY)
646✔
810
            {
811
                if (!new_data) {
619✔
812
                    // Only invalidate the meta if the file is small, or we
813
                    // found some meta previously.
814
                    if ((st.st_mtime != iter->fvs_mtime
537✔
815
                         || st.st_size != iter->fvs_file_size
524✔
816
                         || lf->get_index_size() != iter->fvs_file_indexed_size)
524✔
817
                        && (st.st_size < 10 * 1024 || iter->fvs_file_size == 0
1,061✔
818
                            || !iter->fvs_metadata.m_sections_tree.empty()))
×
819
                    {
820
                        log_debug(
13✔
821
                            "text file has changed, invalidating metadata.  "
822
                            "old: {mtime: %ld size: %lld isize: %lld}, new: "
823
                            "{mtime: %ld size: %lld isize: %lld}",
824
                            iter->fvs_mtime,
825
                            iter->fvs_file_size,
826
                            iter->fvs_file_indexed_size,
827
                            st.st_mtime,
828
                            st.st_size,
829
                            lf->get_index_size());
830
                        iter->fvs_metadata = {};
13✔
831
                        iter->fvs_error.clear();
13✔
832
                    }
833
                }
834

835
                if (!iter->fvs_metadata.m_sections_root
619✔
836
                    && iter->fvs_error.empty())
619✔
837
                {
838
                    auto read_res
839
                        = lf->read_file(logfile::read_format_t::with_framing);
95✔
840

841
                    if (read_res.isOk()) {
95✔
842
                        auto read_file_res = read_res.unwrap();
95✔
843

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

855
                            log_info("generating metadata for: %s (size=%zu)",
95✔
856
                                     lf->get_path_for_key().c_str(),
857
                                     content.length());
858
                            scrub_ansi_string(content.get_string(),
95✔
859
                                              &content.get_attrs());
95✔
860

861
                            auto text_meta = extract_text_meta(
862
                                content.get_string(), lf->get_text_format());
95✔
863
                            if (text_meta) {
95✔
864
                                lf->set_filename(text_meta->tfm_filename);
3✔
865
                                lf->set_include_in_session(true);
3✔
866
                                callback.renamed_file(lf);
3✔
867
                            }
868

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

891
            uint32_t filter_in_mask, filter_out_mask;
892

893
            this->get_filters().get_enabled_mask(filter_in_mask,
646✔
894
                                                 filter_out_mask);
895
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
646✔
896
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,657✔
897
                if (this->tss_apply_filters
8,022✔
898
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
4,011✔
899
                {
900
                    continue;
×
901
                }
902
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
4,011✔
903
            }
904

905
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
646✔
906
                if (iter->fvs_text_source) {
88✔
907
                    if (iter->fvs_file_size == st.st_size
75✔
908
                        && iter->fvs_file_indexed_size == lf->get_index_size()
75✔
909
                        && iter->fvs_mtime == st.st_mtime)
150✔
910
                    {
911
                        ++iter;
75✔
912
                        continue;
75✔
913
                    }
914
                    log_info("markdown file has been updated, re-rendering: %s",
×
915
                             lf->get_path_for_key().c_str());
916
                    iter->fvs_text_source = nullptr;
×
917
                }
918

919
                auto read_res = lf->read_file(logfile::read_format_t::plain);
13✔
920
                if (read_res.isOk()) {
13✔
921
                    auto read_file_res = read_res.unwrap();
13✔
922
                    auto md_file = md4cpp::parse_file(
13✔
923
                        lf->get_filename(), read_file_res.rfr_content);
924
                    log_info("%s: rendering markdown content of size %zu",
13✔
925
                             lf->get_basename().c_str(),
926
                             read_file_res.rfr_content.size());
927
                    md2attr_line mdal;
13✔
928

929
                    mdal.with_source_path(lf->get_actual_path());
13✔
930
                    if (this->tss_view->tc_interactive) {
13✔
931
                        mdal.add_lnav_script_icons();
×
932
                    }
933
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
13✔
934

935
                    iter->fvs_mtime = st.st_mtime;
13✔
936
                    iter->fvs_file_indexed_size = lf->get_index_size();
13✔
937
                    iter->fvs_file_size = st.st_size;
13✔
938
                    iter->fvs_text_source
13✔
939
                        = std::make_unique<plain_text_source>();
26✔
940
                    iter->fvs_text_source->set_text_format(
13✔
941
                        lf->get_text_format());
942
                    iter->fvs_text_source->register_view(this->tss_view);
13✔
943
                    if (parse_res.isOk()) {
13✔
944
                        auto& lf_meta = lf->get_embedded_metadata();
13✔
945

946
                        iter->fvs_text_source->replace_with(parse_res.unwrap());
13✔
947

948
                        if (!md_file.f_frontmatter.empty()) {
13✔
949
                            lf_meta["net.daringfireball.markdown.frontmatter"]
10✔
950
                                = {
951
                                    md_file.f_frontmatter_format,
5✔
952
                                    md_file.f_frontmatter.to_string(),
953
                                };
10✔
954
                        }
955

956
                        lnav::events::publish(
13✔
957
                            lnav_db,
958
                            lnav::events::file::format_detected{
39✔
959
                                lf->get_filename(),
13✔
960
                                fmt::to_string(lf->get_text_format()),
26✔
961
                            });
962
                    } else {
963
                        auto view_content
964
                            = lnav::console::user_message::error(
×
965
                                  "unable to parse markdown file")
966
                                  .with_reason(parse_res.unwrapErr())
×
967
                                  .to_attr_line();
×
968
                        view_content.append("\n").append(
×
969
                            attr_line_t::from_ansi_str(
×
970
                                read_file_res.rfr_content.c_str()));
971

972
                        iter->fvs_text_source->replace_with(view_content);
×
973
                    }
974
                } else {
13✔
975
                    log_error("unable to read markdown file: %s -- %s",
×
976
                              lf->get_path_for_key().c_str(),
977
                              read_res.unwrapErr().c_str());
978
                }
979
            } else if (file_needs_reformatting(lf) && !new_data) {
571✔
980
                if (iter->fvs_file_size == st.st_size
23✔
981
                    && iter->fvs_file_indexed_size == lf->get_index_size()
23✔
982
                    && iter->fvs_mtime == st.st_mtime
23✔
983
                    && (!iter->fvs_error.empty()
69✔
984
                        || iter->fvs_text_source != nullptr))
23✔
985
                {
986
                    ++iter;
19✔
987
                    continue;
19✔
988
                }
989
                log_info("pretty file has been updated, re-rendering: %s",
4✔
990
                         lf->get_path_for_key().c_str());
991
                iter->fvs_text_source = nullptr;
4✔
992
                iter->fvs_error.clear();
4✔
993

994
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
995
                if (read_res.isOk()) {
4✔
996
                    auto read_file_res = read_res.unwrap();
4✔
997
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
998
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
999
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
1000
                        data_scanner ds(orig_al.al_string);
4✔
1001
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
1002
                        attr_line_t pretty_al;
4✔
1003

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

1044
        ++iter;
552✔
1045
    }
1,264✔
1046
    if (!closed_files.empty()) {
4,299✔
1047
        callback.closed_files(closed_files);
97✔
1048
        if (!this->tss_files.empty()) {
97✔
1049
            this->tss_files.front().load_into(*this->tss_view);
×
1050
        }
1051
        this->tss_view->set_needs_update();
97✔
1052
    }
1053

1054
    if (retval.rr_new_data) {
4,299✔
1055
        this->tss_view->search_new_data();
83✔
1056
    }
1057

1058
    return retval;
4,299✔
1059
}
4,317✔
1060

1061
void
1062
textfile_sub_source::set_top_from_off(file_off_t off)
×
1063
{
1064
    auto lf = this->current_file();
×
1065

1066
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1067
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1068
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1069
            std::distance(lf->cbegin(), new_top_iter));
×
1070

1071
        if (new_top_opt) {
×
1072
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1073
            if (this->tss_view->is_selectable()) {
×
1074
                this->tss_view->set_top(
×
1075
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1076
            }
1077
        }
1078
    };
1079
}
1080

1081
void
1082
textfile_sub_source::quiesce()
×
1083
{
1084
    for (auto& lf : this->tss_files) {
×
1085
        lf.fvs_file->quiesce();
×
1086
    }
1087
}
1088

1089
size_t
1090
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
15,639✔
1091
{
1092
    size_t retval = 0;
15,639✔
1093

1094
    if (mode == view_mode::raw || !this->fvs_text_source) {
15,639✔
1095
        const auto& lf = this->fvs_file;
9,992✔
1096
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
9,992✔
1097
            const auto fsize = lf->get_content_size();
2,975✔
1098
            retval = fsize / 16;
2,975✔
1099
            if (fsize % 16) {
2,975✔
1100
                retval += 1;
2,975✔
1101
            }
1102
        } else {
1103
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
7,017✔
1104
            if (lfo != nullptr) {
7,017✔
1105
                retval = lfo->lfo_filter_state.tfs_index.size();
7,017✔
1106
            }
1107
        }
1108
    } else {
1109
        retval = this->fvs_text_source->text_line_count();
5,647✔
1110
    }
1111

1112
    return retval;
15,639✔
1113
}
1114

1115
size_t
1116
textfile_sub_source::file_view_state::text_line_width(view_mode mode,
575✔
1117
                                                      textview_curses& tc) const
1118
{
1119
    size_t retval = 0;
575✔
1120
    if (mode == view_mode::raw || !this->fvs_text_source) {
575✔
1121
        const auto& lf = this->fvs_file;
450✔
1122
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
450✔
1123
            retval = 88;
22✔
1124
        } else {
1125
            retval = lf->get_longest_line_length();
428✔
1126
        }
1127
    } else {
1128
        retval = this->fvs_text_source->text_line_width(tc);
125✔
1129
    }
1130
    return retval;
575✔
1131
}
1132

1133
std::optional<vis_line_t>
1134
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
7✔
1135
                                                     const std::string& id)
1136
{
1137
    if (mode == view_mode::rendered && this->fvs_text_source) {
7✔
1138
        return this->fvs_text_source->row_for_anchor(id);
5✔
1139
    }
1140

1141
    if (!this->fvs_metadata.m_sections_root) {
2✔
1142
        return std::nullopt;
×
1143
    }
1144

1145
    const auto& lf = this->fvs_file;
2✔
1146
    const auto& meta = this->fvs_metadata;
2✔
1147
    std::optional<vis_line_t> retval;
2✔
1148

1149
    auto is_ptr = startswith(id, "#/");
2✔
1150
    if (is_ptr) {
2✔
1151
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1152
        std::vector<lnav::document::section_key_t> path;
2✔
1153

1154
        while (!hier_sf.empty()) {
8✔
1155
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
6✔
1156
            auto scan_res
1157
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
6✔
1158
            if (scan_res && scan_res->range().empty()) {
6✔
1159
                path.emplace_back(scan_res->value());
2✔
1160
            } else {
1161
                stack_buf allocator;
4✔
1162
                path.emplace_back(
4✔
1163
                    json_ptr::decode(comp_pair.first, allocator).to_string());
8✔
1164
            }
4✔
1165
            hier_sf = comp_pair.second;
6✔
1166
        }
1167

1168
        auto lookup_res = lnav::document::hier_node::lookup_path(
2✔
1169
            meta.m_sections_root.get(), path);
2✔
1170
        if (lookup_res) {
2✔
1171
            auto ll_opt = lf->line_for_offset(lookup_res.value()->hn_start);
2✔
1172
            if (ll_opt != lf->end()) {
2✔
1173
                retval
1174
                    = vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1175
            }
1176
        }
1177

1178
        return retval;
2✔
1179
    }
2✔
1180

1181
    lnav::document::hier_node::depth_first(
×
1182
        meta.m_sections_root.get(),
1183
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1184
            for (const auto& child_pair : node->hn_named_children) {
×
1185
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1186

1187
                if (child_anchor != id) {
×
1188
                    continue;
×
1189
                }
1190

1191
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1192
                if (ll_opt != lf->end()) {
×
1193
                    retval = vis_line_t(
×
1194
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1195
                }
1196
                break;
×
1197
            }
1198
        });
×
1199

1200
    return retval;
×
1201
}
1202

1203
std::optional<vis_line_t>
1204
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1205
{
1206
    const auto curr_iter = this->current_file_state();
6✔
1207
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
1208
        return std::nullopt;
×
1209
    }
1210

1211
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
6✔
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 {
1226
                retval.emplace(
×
1227
                    fmt::format(FMT_STRING("#/{}"),
×
1228
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1229
            }
1230
            max_depth = std::max(max_depth, comps.size());
×
1231
        } else {
1232
            int index = 0;
×
1233
            for (const auto& child : hn->hn_children) {
×
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);
×
1243
            comps.pop_back();
×
1244
        }
1245
        if (max_depth > 1) {
×
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
{
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

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

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

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

1278
    return retval;
×
1279
}
1280

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

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

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

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

1308
    return std::nullopt;
×
1309
}
1✔
1310

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

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

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

1339
    return std::nullopt;
×
1340
}
1341

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

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

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

1366
    auto& md = curr_iter->fvs_metadata;
3✔
1367
    const auto* lfo
1368
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1369
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
3✔
1370
        || md.m_sections_root == nullptr)
3✔
1371
    {
1372
        return std::nullopt;
×
1373
    }
1374
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
3✔
1375
    const auto line_offsets = lf->get_file_range(ll_iter, false);
3✔
1376
    log_debug(
3✔
1377
        "  range %lld:%zu", 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());
3✔
1380

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

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

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

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

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

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

1438
    log_debug("  neighbors p:%d n:%d",
3✔
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>()) {
3✔
1442
        auto neighbor_sub
1443
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
3✔
1444
        if (neighbor_sub) {
3✔
1445
            neighbors_res->cnr_previous = neighbor_sub;
×
1446
        }
1447
    }
1448

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

1457
    switch (dir) {
3✔
1458
        case direction::prev: {
1✔
1459
            if (neighbors_res->cnr_previous) {
1✔
1460
                return to_vis_line(
1✔
1461
                    lf, neighbors_res->cnr_previous.value()->hn_start);
1✔
1462
            }
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
            }
1470
            break;
×
1471
        }
1472
    }
1473

1474
    return std::nullopt;
×
1475
}
3✔
1476

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

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

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

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

1506
    if (path_for_line.empty()) {
9✔
1507
        return std::nullopt;
5✔
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(
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
}
9✔
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

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

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
379✔
1565
{
1566
    auto retval = view_mode::raw;
379✔
1567

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

1577
    return retval;
379✔
1578
}
1579

1580
void
1581
textfile_sub_source::move_to_init_location(file_iterator& iter)
559✔
1582
{
1583
    if (iter->fvs_consumed_init_location) {
559✔
1584
        return;
459✔
1585
    }
1586

1587
    auto& lf = iter->fvs_file;
100✔
1588
    std::optional<vis_line_t> new_sel_opt;
100✔
1589
    require(lf->get_open_options().loo_init_location.valid());
100✔
1590
    lf->get_open_options().loo_init_location.match(
100✔
1591
        [this, &new_sel_opt, &lf](default_for_text_format def) {
×
1592
            if (!this->tss_apply_default_init_location) {
97✔
1593
                return;
97✔
1594
            }
1595
            switch (lf->get_text_format()) {
×
1596
                case text_format_t::TF_UNKNOWN:
×
1597
                case text_format_t::TF_LOG: {
1598
                    log_info("file open request to tail");
×
1599
                    auto inner_height = lf->size();
×
1600
                    if (inner_height > 0) {
×
1601
                        new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1602
                    }
1603
                    break;
×
1604
                }
1605
                default:
×
1606
                    log_info("file open is %s, moving to top",
×
1607
                             fmt::to_string(lf->get_text_format()).c_str());
1608
                    new_sel_opt = 0_vl;
×
1609
                    break;
×
1610
            }
1611
        },
1612
        [&new_sel_opt, &lf](file_location_tail tail) {
×
1613
            log_info("file open request to tail");
×
1614
            auto inner_height = lf->size();
×
1615
            if (inner_height > 0) {
×
1616
                new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1617
            }
1618
        },
×
1619
        [this, &new_sel_opt, &iter](int vl) {
×
1620
            log_info("file open request to jump to line: %d", vl);
2✔
1621
            auto height = iter->text_line_count(this->tss_view_mode);
2✔
1622
            if (vl < 0) {
2✔
1623
                vl += height;
2✔
1624
                if (vl < 0) {
2✔
1625
                    vl = 0;
1✔
1626
                }
1627
            }
1628
            if (vl < height) {
2✔
1629
                new_sel_opt = vis_line_t(vl);
2✔
1630
            }
1631
        },
2✔
1632
        [this, &new_sel_opt, &iter](const std::string& loc) {
100✔
1633
            log_info("file open request to jump to anchor: %s", loc.c_str());
1✔
1634
            new_sel_opt = iter->row_for_anchor(this->tss_view_mode, loc);
1✔
1635
        });
1✔
1636

1637
    if (new_sel_opt) {
100✔
1638
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1639
        log_info("  setting requested selection: %d",
3✔
1640
                 (int) new_sel_opt.value());
1641
        iter->fvs_selection = new_sel_opt.value();
3✔
1642
        log_info("  actual top is now: %d", (int) iter->fvs_top);
3✔
1643
        log_info("  actual selection is now: %d", (int) iter->fvs_selection);
3✔
1644

1645
        if (this->current_file() == lf) {
3✔
1646
            this->tss_view->set_selection(iter->fvs_selection);
3✔
1647
        }
1648
    }
1649
    iter->fvs_consumed_init_location = true;
100✔
1650
}
1651

1652
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
632✔
1653
                                                 text_sub_source* log_src)
632✔
1654
    : tho_src(src), tho_log_src(log_src)
632✔
1655
{
1656
}
632✔
1657

1658
bool
1659
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
9,863✔
1660
                                             media_t media,
1661
                                             int y,
1662
                                             int bottom,
1663
                                             attr_line_t& value_out)
1664
{
1665
    if (media == media_t::display) {
9,863✔
1666
        const std::vector<attr_line_t>* lines = nullptr;
9,805✔
1667
        auto curr_file = this->tho_src->current_file();
9,805✔
1668
        if (curr_file == nullptr) {
9,805✔
1669
            if (this->tho_log_src->text_line_count() == 0) {
×
1670
                lines = lnav::messages::view::no_files();
×
1671
            } else {
1672
                lines = lnav::messages::view::only_log_files();
×
1673
            }
1674
        } else if (!curr_file->get_notes().empty()) {
9,805✔
1675
            this->tho_static_lines = curr_file->get_notes()
×
1676
                                         .values()
×
1677
                                         .front()
×
1678
                                         .to_attr_line()
×
1679
                                         .split_lines();
×
1680
            lines = &this->tho_static_lines;
×
1681
        } else if (curr_file->size() == 0) {
9,805✔
1682
            lines = lnav::messages::view::empty_file();
×
1683
        } else if (this->tho_src->text_line_count() == 0) {
9,805✔
1684
            hasher h;
×
1685
            this->tho_src->update_filter_hash_state(h);
×
1686
            auto curr_state = h.to_array();
×
1687
            if (this->tho_static_lines.empty()
×
1688
                || curr_state != this->tho_filter_state)
×
1689
            {
1690
                auto msg = lnav::console::user_message::info(
1691
                    "All text lines are currently hidden");
×
1692
                auto min_time = this->tho_src->get_min_row_time();
×
1693
                if (min_time) {
×
1694
                    msg.with_note(attr_line_t("Lines before ")
×
1695
                                      .append_quoted(lnav::to_rfc3339_string(
×
1696
                                          min_time.value()))
×
1697
                                      .append(" are not being shown"));
×
1698
                }
1699
                auto max_time = this->tho_src->get_max_row_time();
×
1700
                if (max_time) {
×
1701
                    msg.with_note(attr_line_t("Lines after ")
×
1702
                                      .append_quoted(lnav::to_rfc3339_string(
×
1703
                                          max_time.value()))
×
1704
                                      .append(" are not being shown"));
×
1705
                }
1706
                auto& fs = this->tho_src->get_filters();
×
1707
                for (const auto& filt : fs) {
×
1708
                    auto hits = this->tho_src->get_filtered_count_for(
×
1709
                        filt->get_index());
1710
                    if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
1711
                        continue;
×
1712
                    }
1713
                    auto cmd = attr_line_t(":" + filt->to_command());
×
1714
                    readline_command_highlighter(cmd, std::nullopt);
×
1715
                    msg.with_note(
×
1716
                        attr_line_t("Filter ")
×
1717
                            .append_quoted(cmd)
×
1718
                            .append(" matched ")
×
1719
                            .append(lnav::roles::number(fmt::to_string(hits)))
×
1720
                            .append(" line(s) "));
×
1721
                }
1722
                this->tho_static_lines = msg.to_attr_line().split_lines();
×
1723
                this->tho_filter_state = curr_state;
×
1724
            }
1725

1726
            lines = &this->tho_static_lines;
×
1727
        }
1728

1729
        if (lines != nullptr && y < (ssize_t) lines->size()) {
9,805✔
1730
            value_out = lines->at(y);
×
1731
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
1732
            if (y == (ssize_t) lines->size() - 1) {
×
1733
                value_out.with_attr_for_all(
×
1734
                    VC_STYLE.value(text_attrs::with_underline()));
×
1735
            }
1736
            return true;
×
1737
        }
1738
    }
9,805✔
1739

1740
    if (y != 0) {
9,863✔
1741
        return false;
9,356✔
1742
    }
1743

1744
    const auto lf = this->tho_src->current_file();
507✔
1745
    if (lf == nullptr) {
507✔
1746
        return false;
×
1747
    }
1748

1749
    if (media == media_t::display
507✔
1750
        && lf->get_text_format() != text_format_t::TF_MARKDOWN
454✔
1751
        && this->tho_src->get_effective_view_mode()
961✔
1752
            == textfile_sub_source::view_mode::rendered)
1753
    {
1754
        auto ta = text_attrs::with_underline();
19✔
1755
        value_out.append("\u24d8"_info)
19✔
1756
            .append(" The following is a rendered view of the content.  Use ")
19✔
1757
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
19✔
1758
            .append(" to view the raw version of this text")
19✔
1759
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
38✔
1760
            .with_attr_for_all(VC_STYLE.value(ta));
19✔
1761
        return true;
19✔
1762
    }
1763

1764
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
488✔
1765
        return false;
461✔
1766
    }
1767

1768
    {
1769
        attr_line_builder alb(value_out);
27✔
1770
        {
1771
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1772
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
81✔
1773
        }
27✔
1774
        size_t byte_off = 0;
27✔
1775
        for (size_t lpc = 0; lpc < 16; lpc++) {
459✔
1776
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
432✔
1777
            if (byte_off == 8) {
432✔
1778
                alb.append(" ");
27✔
1779
            }
1780
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
1,296✔
1781
            byte_off += 1;
432✔
1782
        }
432✔
1783
        {
1784
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1785
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
81✔
1786
        }
27✔
1787
    }
1788
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
27✔
1789
    return true;
27✔
1790
}
507✔
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