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

tstack / lnav / 22507085525-2793

27 Feb 2026 10:49PM UTC coverage: 68.948% (-0.02%) from 68.966%
22507085525-2793

push

github

tstack
fix mixup of O_CLOEXEC with FD_CLOEXEC

52007 of 75429 relevant lines covered (68.95%)

440500.48 hits per line

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

68.15
/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 <algorithm>
31
#include <chrono>
32
#include <iterator>
33
#include <memory>
34
#include <unordered_set>
35

36
#include "textfile_sub_source.hh"
37

38
#include <date/date.h>
39

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

62
using namespace lnav::roles::literals;
63

64
static bool
65
file_needs_reformatting(const std::shared_ptr<logfile>& lf)
564✔
66
{
67
    static const auto& cfg = injector::get<const lnav::textfile::config&>();
564✔
68

69
    const auto& opts = lf->get_open_options();
564✔
70
    if (opts.loo_piper && !opts.loo_piper->is_finished()) {
564✔
71
        return false;
×
72
    }
73

74
    switch (lf->get_text_format().value_or(text_format_t::TF_BINARY)) {
564✔
75
        case text_format_t::TF_BINARY:
130✔
76
        case text_format_t::TF_DIFF:
77
            return false;
130✔
78
        default:
434✔
79
            if (lf->get_content_size() < 16 * 1024
434✔
80
                && lf->get_longest_line_length()
868✔
81
                    > cfg.c_max_unformatted_line_length)
434✔
82
            {
83
                return true;
27✔
84
            }
85
            return false;
407✔
86
    }
87
}
88

89
size_t
90
textfile_sub_source::text_line_count()
20,648✔
91
{
92
    size_t retval = 0;
20,648✔
93

94
    if (!this->tss_files.empty()) {
20,648✔
95
        retval
96
            = this->current_file_state()->text_line_count(this->tss_view_mode);
13,014✔
97
    }
98

99
    return retval;
20,648✔
100
}
101

102
size_t
103
textfile_sub_source::text_line_width(textview_curses& tc)
5,801✔
104
{
105
    const auto iter = this->current_file_state();
5,801✔
106
    if (iter == this->tss_files.end()) {
5,801✔
107
        return 0;
5,216✔
108
    }
109

110
    return iter->text_line_width(this->tss_view_mode, tc);
585✔
111
}
112

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

124
    const auto curr_iter = this->current_file_state();
2,097✔
125
    const auto& lf = curr_iter->fvs_file;
2,097✔
126
    if (this->tss_view_mode == view_mode::rendered
4,194✔
127
        && curr_iter->fvs_text_source)
2,097✔
128
    {
129
        curr_iter->fvs_text_source->text_value_for_line(
1,368✔
130
            tc, line, value_out, flags);
131
        return {};
1,368✔
132
    }
133

134
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
729✔
135
        this->tss_hex_line.clear();
1✔
136
        auto fsize = lf->get_content_size();
1✔
137
        auto fr = file_range{line * 16};
1✔
138
        fr.fr_size = std::min((file_ssize_t) 16, fsize - fr.fr_offset);
1✔
139

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

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

164
        value_out = this->tss_hex_line.get_string();
1✔
165
        return {};
1✔
166
    }
1✔
167

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

176
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
728✔
177
    auto read_opts = subline_options{};
728✔
178
    read_opts.scrub_invalid_utf8 = false;
728✔
179
    auto read_result = lf->read_line(ll, read_opts);
728✔
180
    this->tss_line_indent_size = 0;
728✔
181
    this->tss_plain_line_attrs.clear();
728✔
182
    if (read_result.isOk()) {
728✔
183
        auto sbr = read_result.unwrap();
728✔
184
        value_out = to_string(sbr);
728✔
185
        const auto& meta = sbr.get_metadata();
728✔
186
        if (meta.m_valid_utf && meta.m_has_ansi) {
728✔
187
            scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
35✔
188
        }
189
        for (const auto& ch : value_out) {
2,028✔
190
            if (ch == ' ') {
1,894✔
191
                this->tss_line_indent_size += 1;
1,295✔
192
            } else if (ch == '\t') {
599✔
193
                do {
194
                    this->tss_line_indent_size += 1;
39✔
195
                } while (this->tss_line_indent_size % 8);
39✔
196
            } else {
197
                break;
594✔
198
            }
199
        }
200
        if (lf->has_line_metadata() && this->tas_display_time_offset) {
728✔
201
            auto relstr = this->get_time_offset_for_line(tc, vis_line_t(line));
1✔
202
            value_out
203
                = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
4✔
204
        }
1✔
205
    }
728✔
206

207
    return {};
728✔
208
}
728✔
209

210
void
211
textfile_sub_source::text_attrs_for_line(textview_curses& tc,
2,097✔
212
                                         int row,
213
                                         string_attrs_t& value_out)
214
{
215
    const auto curr_iter = this->current_file_state();
2,097✔
216
    if (curr_iter == this->tss_files.end()) {
2,097✔
217
        return;
×
218
    }
219
    const auto& lf = curr_iter->fvs_file;
2,097✔
220

221
    auto lr = line_range{0, -1};
2,097✔
222
    if (this->tss_view_mode == view_mode::rendered
4,194✔
223
        && curr_iter->fvs_text_source)
2,097✔
224
    {
225
        curr_iter->fvs_text_source->text_attrs_for_line(tc, row, value_out);
1,368✔
226
    } else if (lf->get_text_format() == text_format_t::TF_BINARY) {
729✔
227
        value_out = this->tss_hex_line.get_attrs();
1✔
228
    } else {
229
        value_out = this->tss_plain_line_attrs;
728✔
230
        auto* lfo
231
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
728✔
232
        if (lfo != nullptr && row >= 0
728✔
233
            && row < (ssize_t) lfo->lfo_filter_state.tfs_index.size())
1,456✔
234
        {
235
            auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[row];
728✔
236

237
            value_out.emplace_back(lr, SA_LEVEL.value(ll->get_msg_level()));
728✔
238
            if (lf->has_line_metadata() && this->tas_display_time_offset) {
728✔
239
                auto time_offset_end = 13;
1✔
240
                lr.lr_start = 0;
1✔
241
                lr.lr_end = time_offset_end;
1✔
242

243
                shift_string_attrs(value_out, 0, time_offset_end);
1✔
244

245
                value_out.emplace_back(lr,
1✔
246
                                       VC_ROLE.value(role_t::VCR_OFFSET_TIME));
2✔
247
                value_out.emplace_back(line_range(12, 13),
1✔
248
                                       VC_GRAPHIC.value(NCACS_VLINE));
2✔
249

250
                auto bar_role = role_t::VCR_NONE;
1✔
251

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

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

320
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
2,097✔
321
}
322

323
size_t
324
textfile_sub_source::text_size_for_line(textview_curses& tc,
×
325
                                        int line,
326
                                        text_sub_source::line_flags_t flags)
327
{
328
    size_t retval = 0;
×
329

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

357
    return retval;
×
358
}
359

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

377
void
378
textfile_sub_source::rotate_left()
×
379
{
380
    if (this->tss_files.size() > 1) {
×
381
        this->tss_files.emplace_back(std::move(this->tss_files.front()));
×
382
        this->tss_files.pop_front();
×
383
        this->tss_files.back().save_from(*this->tss_view);
×
384
        this->tss_files.front().load_into(*this->tss_view);
×
385
        this->set_time_offset(false);
×
386
        this->tss_view->reload_data();
×
387
        this->tss_view->redo_search();
×
388
        this->tss_view->set_needs_update();
×
389
    }
390
}
391

392
void
393
textfile_sub_source::rotate_right()
×
394
{
395
    if (this->tss_files.size() > 1) {
×
396
        this->tss_files.front().save_from(*this->tss_view);
×
397
        auto fvs = std::move(this->tss_files.back());
×
398
        this->tss_files.emplace_front(std::move(fvs));
×
399
        this->tss_files.pop_back();
×
400
        this->tss_files.front().load_into(*this->tss_view);
×
401
        this->set_time_offset(false);
×
402
        this->tss_view->reload_data();
×
403
        this->tss_view->redo_search();
×
404
        this->tss_view->set_needs_update();
×
405
    }
406
}
407

408
void
409
textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
529✔
410
{
411
    auto iter = std::find(this->tss_files.begin(), this->tss_files.end(), lf);
529✔
412
    if (iter != this->tss_files.end()) {
529✔
413
        this->tss_files.erase(iter);
×
414
        this->detach_observer(lf);
×
415
    }
416
    this->set_time_offset(false);
529✔
417
    if (!this->tss_files.empty()) {
529✔
418
        this->tss_files.front().load_into(*this->tss_view);
×
419
    }
420
    this->tss_view->reload_data();
529✔
421
}
529✔
422

423
void
424
textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
632✔
425
{
426
    auto* lfo = new line_filter_observer(this->get_filters(), lf);
632✔
427
    lf->set_logline_observer(lfo);
632✔
428
    this->tss_files.emplace_back(lf);
632✔
429
}
632✔
430

431
void
432
textfile_sub_source::text_filters_changed()
9✔
433
{
434
    auto lf = this->current_file();
9✔
435
    if (lf == nullptr || lf->get_text_format() == text_format_t::TF_BINARY) {
9✔
436
        return;
6✔
437
    }
438

439
    auto* lfo = (line_filter_observer*) lf->get_logline_observer();
3✔
440
    uint32_t filter_in_mask, filter_out_mask;
441

442
    lfo->clear_deleted_filter_state();
3✔
443
    lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
3✔
444

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

465
    this->tss_view->redo_search();
3✔
466

467
    auto iter = std::lower_bound(lfo->lfo_filter_state.tfs_index.begin(),
3✔
468
                                 lfo->lfo_filter_state.tfs_index.end(),
469
                                 this->tss_content_line);
3✔
470
    auto vl = vis_line_t(
471
        std::distance(lfo->lfo_filter_state.tfs_index.begin(), iter));
6✔
472
    this->tss_view->set_selection(vl);
3✔
473
}
9✔
474

475
void
476
textfile_sub_source::scroll_invoked(textview_curses* tc)
773✔
477
{
478
    const auto curr_iter = this->current_file_state();
773✔
479
    if (curr_iter == this->tss_files.end()
773✔
480
        || curr_iter->fvs_file->get_text_format() == text_format_t::TF_BINARY)
773✔
481
    {
482
        return;
683✔
483
    }
484

485
    const auto& lf = curr_iter->fvs_file;
112✔
486
    if (this->tss_view_mode == view_mode::rendered
224✔
487
        && curr_iter->fvs_text_source)
112✔
488
    {
489
        return;
22✔
490
    }
491

492
    auto line = tc->get_selection();
90✔
493
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
90✔
494
    if (!line || lfo == nullptr || line < 0_vl
180✔
495
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
180✔
496
    {
497
        return;
×
498
    }
499

500
    this->tss_content_line = lfo->lfo_filter_state.tfs_index[line.value()];
90✔
501
}
502

503
int
504
textfile_sub_source::get_filtered_count() const
280✔
505
{
506
    const auto curr_iter = this->current_file_state();
280✔
507
    int retval = 0;
280✔
508

509
    if (curr_iter != this->tss_files.end()) {
280✔
510
        if (this->tss_view_mode == view_mode::raw
552✔
511
            || !curr_iter->fvs_text_source)
276✔
512
        {
513
            const auto& lf = curr_iter->fvs_file;
218✔
514
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
218✔
515
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
218✔
516
        }
517
    }
518
    return retval;
280✔
519
}
520

521
int
522
textfile_sub_source::get_filtered_count_for(size_t filter_index) const
×
523
{
524
    std::shared_ptr<logfile> lf = this->current_file();
×
525

526
    if (lf == nullptr) {
×
527
        return 0;
×
528
    }
529

530
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
531
    return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
×
532
}
533

534
std::optional<text_format_t>
535
textfile_sub_source::get_text_format() const
1,588✔
536
{
537
    if (this->tss_files.empty()) {
1,588✔
538
        return text_format_t::TF_PLAINTEXT;
×
539
    }
540

541
    return this->tss_files.front().fvs_file->get_text_format();
1,588✔
542
}
543

544
static attr_line_t
545
to_display(const std::shared_ptr<logfile>& lf)
22✔
546
{
547
    const auto& loo = lf->get_open_options();
22✔
548
    attr_line_t retval;
22✔
549

550
    if (loo.loo_piper) {
22✔
551
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
552
            retval.append("\u21bb "_list_glyph);
×
553
        }
554
    } else if (loo.loo_child_poller && loo.loo_child_poller->is_alive()) {
18✔
555
        retval.append("\u21bb "_list_glyph);
×
556
    }
557
    retval.append(lf->get_unique_path());
22✔
558

559
    return retval;
22✔
560
}
×
561

562
void
563
textfile_sub_source::text_crumbs_for_line(
11✔
564
    int line, std::vector<breadcrumb::crumb>& crumbs)
565
{
566
    text_sub_source::text_crumbs_for_line(line, crumbs);
11✔
567

568
    if (this->empty()) {
11✔
569
        return;
×
570
    }
571

572
    const auto curr_iter = this->current_file_state();
11✔
573
    const auto& lf = curr_iter->fvs_file;
11✔
574
    crumbs.emplace_back(
11✔
575
        lf->get_unique_path(),
11✔
576
        to_display(lf),
11✔
577
        [this]() {
×
578
            return this->tss_files | lnav::itertools::map([](const auto& lf) {
22✔
579
                       return breadcrumb::possibility{
580
                           lf.fvs_file->get_path_for_key(),
11✔
581
                           to_display(lf.fvs_file),
11✔
582
                       };
11✔
583
                   });
22✔
584
        },
585
        [this](const auto& key) {
11✔
586
            auto lf_opt = this->tss_files
×
587
                | lnav::itertools::map([](const auto& x) { return x.fvs_file; })
×
588
                | lnav::itertools::find_if([&key](const auto& elem) {
×
589
                              return key.template get<std::string>()
590
                                  == elem->get_path_for_key();
×
591
                          })
592
                | lnav::itertools::deref();
×
593

594
            if (!lf_opt) {
×
595
                return;
×
596
            }
597

598
            this->to_front(lf_opt.value());
×
599
            this->tss_view->reload_data();
×
600
        });
×
601
    if (lf->size() == 0) {
11✔
602
        return;
×
603
    }
604

605
    if (lf->has_line_metadata()) {
11✔
606
        auto* lfo
607
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
608
        if (line < 0
2✔
609
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
2✔
610
        {
611
            return;
×
612
        }
613
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
2✔
614
        char ts[64];
615

616
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
617

618
        crumbs.emplace_back(
2✔
619
            std::string(ts),
4✔
620
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
621
            [](const auto& key) {});
2✔
622
    }
623

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

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

693
                    crumbs.back().c_expected_input
1✔
694
                        = curr_node.value()
2✔
695
                              ->hn_parent->hn_named_children.empty()
1✔
696
                        ? breadcrumb::crumb::expected_input_t::index
1✔
697
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
698
                    crumbs.back().with_possible_range(
2✔
699
                        node | lnav::itertools::map([](const auto hn) {
1✔
700
                            return hn->hn_parent->hn_children.size();
1✔
701
                        })
702
                        | lnav::itertools::unwrap_or(size_t{0}));
2✔
703
                }
704
            });
8✔
705

706
        auto path = crumbs | lnav::itertools::skip(initial_size)
12✔
707
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
12✔
708
        auto node = lnav::document::hier_node::lookup_path(
6✔
709
            curr_iter->fvs_metadata.m_sections_root.get(), path);
6✔
710

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

747
textfile_sub_source::rescan_result_t
748
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
4,513✔
749
                                  std::optional<ui_clock::time_point> deadline)
750
{
751
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,513✔
752

753
    file_iterator iter;
4,513✔
754
    rescan_result_t retval;
4,513✔
755
    size_t files_scanned = 0;
4,513✔
756

757
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,513✔
758
        return retval;
124✔
759
    }
760

761
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,389✔
762

763
    std::vector<std::shared_ptr<logfile>> closed_files;
4,389✔
764
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
5,679✔
765
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,290✔
766
        {
767
            log_info("rescan_files() deadline reached, breaking...");
×
768
            retval.rr_scan_completed = false;
×
769
            this->tss_last_scan_aborted = true;
×
770
            break;
×
771
        }
772

773
        std::shared_ptr<logfile> lf = iter->fvs_file;
1,290✔
774

775
        if (lf->is_closed()) {
1,290✔
776
            iter = this->tss_files.erase(iter);
103✔
777
            this->detach_observer(lf);
103✔
778
            closed_files.emplace_back(lf);
103✔
779
            retval.rr_rescan_needed = true;
103✔
780
            continue;
103✔
781
        }
782

783
        if (last_aborted && lf->size() > 0) {
1,187✔
784
            retval.rr_scan_completed = false;
×
785
            ++iter;
×
786
            continue;
×
787
        }
788
        files_scanned += 1;
1,187✔
789

790
        try {
791
            const auto& st = lf->get_stat();
1,187✔
792
            uint32_t old_size = lf->size();
1,187✔
793
            auto new_text_data = lf->rebuild_index(deadline);
1,187✔
794

795
            if (lf->get_format() != nullptr) {
1,187✔
796
                iter = this->tss_files.erase(iter);
529✔
797
                this->detach_observer(lf);
529✔
798
                callback.promote_file(lf);
529✔
799
                continue;
627✔
800
            }
801

802
            bool new_data = false;
658✔
803
            switch (new_text_data) {
658✔
804
                case logfile::rebuild_result_t::NEW_LINES:
90✔
805
                case logfile::rebuild_result_t::NEW_ORDER:
806
                    new_data = true;
90✔
807
                    retval.rr_new_data += 1;
90✔
808
                    break;
90✔
809
                case logfile::rebuild_result_t::NO_NEW_LINES:
568✔
810
                    this->move_to_init_location(iter);
568✔
811
                    break;
568✔
812
                default:
×
813
                    break;
×
814
            }
815
            callback.scanned_file(lf);
658✔
816

817
            if (lf->is_indexing()
658✔
818
                && lf->get_text_format() != text_format_t::TF_BINARY)
658✔
819
            {
820
                if (!new_data) {
652✔
821
                    // Only invalidate the meta if the file is small, or we
822
                    // found some meta previously.
823
                    if ((st.st_mtime != iter->fvs_mtime
563✔
824
                         || st.st_size != iter->fvs_file_size
550✔
825
                         || lf->get_index_size() != iter->fvs_file_indexed_size)
550✔
826
                        && (st.st_size < 10 * 1024 || iter->fvs_file_size == 0
1,113✔
827
                            || !iter->fvs_metadata.m_sections_tree.empty()))
×
828
                    {
829
                        log_debug(
13✔
830
                            "text file has changed, invalidating metadata.  "
831
                            "old: {mtime: %ld size: %lld isize: %lld}, new: "
832
                            "{mtime: %ld size: %lld isize: %lld}",
833
                            iter->fvs_mtime,
834
                            iter->fvs_file_size,
835
                            iter->fvs_file_indexed_size,
836
                            st.st_mtime,
837
                            st.st_size,
838
                            lf->get_index_size());
839
                        iter->fvs_metadata = {};
13✔
840
                        iter->fvs_error.clear();
13✔
841
                    }
842
                }
843

844
                if (!iter->fvs_metadata.m_sections_root
652✔
845
                    && iter->fvs_error.empty())
652✔
846
                {
847
                    auto read_res
848
                        = lf->read_file(logfile::read_format_t::with_framing);
101✔
849

850
                    if (read_res.isOk()) {
101✔
851
                        auto read_file_res = read_res.unwrap();
101✔
852
                        auto tf_opt = lf->get_text_format();
101✔
853

854
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf
202✔
855
                            || !tf_opt)
101✔
856
                        {
857
                            log_error(
18✔
858
                                "%s: file has no text format, skipping meta "
859
                                "discovery",
860
                                lf->get_path_for_key().c_str());
861
                            iter->fvs_mtime = st.st_mtime;
18✔
862
                            iter->fvs_file_size = st.st_size;
18✔
863
                            iter->fvs_file_indexed_size = lf->get_index_size();
18✔
864
                            iter->fvs_error = "skipping meta discovery";
18✔
865
                        } else {
866
                            auto content
867
                                = attr_line_t(read_file_res.rfr_content);
83✔
868

869
                            log_info("generating metadata for: %s (size=%zu)",
83✔
870
                                     lf->get_path_for_key().c_str(),
871
                                     content.length());
872
                            scrub_ansi_string(content.get_string(),
83✔
873
                                              &content.get_attrs());
83✔
874

875
                            auto text_meta = extract_text_meta(
876
                                content.get_string(), tf_opt.value());
83✔
877
                            if (text_meta) {
83✔
878
                                lf->set_filename(text_meta->tfm_filename);
3✔
879
                                lf->set_include_in_session(true);
3✔
880
                                callback.renamed_file(lf);
3✔
881
                            }
882

883
                            iter->fvs_mtime = st.st_mtime;
83✔
884
                            iter->fvs_file_size = st.st_size;
83✔
885
                            iter->fvs_file_indexed_size = lf->get_index_size();
83✔
886
                            iter->fvs_metadata
83✔
887
                                = lnav::document::discover(content)
83✔
888
                                      .with_text_format(tf_opt.value())
83✔
889
                                      .perform();
166✔
890
                            log_info("  metadata indents size: %zu",
83✔
891
                                     iter->fvs_metadata.m_indents.size());
892
                        }
83✔
893
                    } else {
101✔
894
                        auto errmsg = read_res.unwrapErr();
×
895
                        log_error(
×
896
                            "%s: unable to read file for meta discover -- %s",
897
                            lf->get_path_for_key().c_str(),
898
                            errmsg.c_str());
899
                        iter->fvs_mtime = st.st_mtime;
×
900
                        iter->fvs_file_size = st.st_size;
×
901
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
902
                        iter->fvs_error = errmsg;
×
903
                    }
904
                }
101✔
905
            }
906

907
            uint32_t filter_in_mask, filter_out_mask;
908

909
            this->get_filters().get_enabled_mask(filter_in_mask,
658✔
910
                                                 filter_out_mask);
911
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
658✔
912
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,756✔
913
                if (this->tss_apply_filters
8,196✔
914
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
4,098✔
915
                {
916
                    continue;
×
917
                }
918
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
4,098✔
919
            }
920

921
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
658✔
922
                if (iter->fvs_text_source) {
94✔
923
                    if (iter->fvs_file_size == st.st_size
80✔
924
                        && iter->fvs_file_indexed_size == lf->get_index_size()
80✔
925
                        && iter->fvs_mtime == st.st_mtime)
160✔
926
                    {
927
                        ++iter;
79✔
928
                        continue;
79✔
929
                    }
930
                    log_info("markdown file has been updated, re-rendering: %s",
1✔
931
                             lf->get_path_for_key().c_str());
932
                    iter->fvs_text_source = nullptr;
1✔
933
                }
934

935
                auto read_res = lf->read_file(logfile::read_format_t::plain);
15✔
936
                if (read_res.isOk()) {
15✔
937
                    auto read_file_res = read_res.unwrap();
15✔
938
                    auto md_file = md4cpp::parse_file(
15✔
939
                        lf->get_filename(), read_file_res.rfr_content);
940
                    log_info("%s: rendering markdown content of size %zu",
15✔
941
                             lf->get_basename().c_str(),
942
                             read_file_res.rfr_content.size());
943
                    md2attr_line mdal;
15✔
944

945
                    mdal.with_source_path(lf->get_actual_path());
15✔
946
                    if (this->tss_view->tc_interactive) {
15✔
947
                        mdal.add_lnav_script_icons();
×
948
                    }
949
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
15✔
950

951
                    iter->fvs_mtime = st.st_mtime;
15✔
952
                    iter->fvs_file_indexed_size = lf->get_index_size();
15✔
953
                    iter->fvs_file_size = st.st_size;
15✔
954
                    iter->fvs_text_source
15✔
955
                        = std::make_unique<plain_text_source>();
30✔
956
                    iter->fvs_text_source->set_text_format(
15✔
957
                        lf->get_text_format());
958
                    if (parse_res.isOk()) {
15✔
959
                        auto& lf_meta = lf->get_embedded_metadata();
13✔
960

961
                        iter->fvs_text_source->replace_with(parse_res.unwrap());
13✔
962
                        if (!md_file.f_frontmatter.empty()) {
13✔
963
                            lf_meta["net.daringfireball.markdown.frontmatter"]
10✔
964
                                = {
965
                                    md_file.f_frontmatter_format,
5✔
966
                                    md_file.f_frontmatter.to_string(),
967
                                };
10✔
968
                        }
969

970
                        lnav::events::publish(
13✔
971
                            lnav_db,
972
                            lnav::events::file::format_detected{
39✔
973
                                lf->get_filename(),
13✔
974
                                fmt::to_string(lf->get_text_format().value_or(
26✔
975
                                    text_format_t::TF_BINARY)),
26✔
976
                            });
977
                    } else {
978
                        auto view_content
979
                            = lnav::console::user_message::error(
4✔
980
                                  "unable to parse markdown file")
981
                                  .with_reason(parse_res.unwrapErr())
4✔
982
                                  .to_attr_line();
2✔
983
                        view_content.append("\n").append(
4✔
984
                            attr_line_t::from_ansi_str(
4✔
985
                                read_file_res.rfr_content.c_str()));
986

987
                        iter->fvs_text_source->replace_with(view_content);
2✔
988
                    }
2✔
989
                    iter->fvs_text_source->register_view(this->tss_view);
15✔
990
                } else {
15✔
991
                    log_error("unable to read markdown file: %s -- %s",
×
992
                              lf->get_path_for_key().c_str(),
993
                              read_res.unwrapErr().c_str());
994
                }
995
            } else if (file_needs_reformatting(lf) && !new_data) {
579✔
996
                if (iter->fvs_file_size == st.st_size
23✔
997
                    && iter->fvs_file_indexed_size == lf->get_index_size()
23✔
998
                    && iter->fvs_mtime == st.st_mtime
23✔
999
                    && (!iter->fvs_error.empty()
69✔
1000
                        || iter->fvs_text_source != nullptr))
23✔
1001
                {
1002
                    ++iter;
19✔
1003
                    continue;
19✔
1004
                }
1005
                log_info("pretty file has been updated, re-rendering: %s",
4✔
1006
                         lf->get_path_for_key().c_str());
1007
                iter->fvs_text_source = nullptr;
4✔
1008
                iter->fvs_error.clear();
4✔
1009

1010
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
1011
                if (read_res.isOk()) {
4✔
1012
                    auto read_file_res = read_res.unwrap();
4✔
1013
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
1014
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
1015
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
1016
                        data_scanner ds(orig_al.al_string);
4✔
1017
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
1018
                        attr_line_t pretty_al;
4✔
1019

1020
                        pp.append_to(pretty_al);
4✔
1021
                        iter->fvs_mtime = st.st_mtime;
4✔
1022
                        iter->fvs_file_indexed_size = lf->get_index_size();
4✔
1023
                        iter->fvs_file_size = st.st_size;
4✔
1024
                        iter->fvs_text_source
4✔
1025
                            = std::make_unique<plain_text_source>();
8✔
1026
                        iter->fvs_text_source->set_text_format(
4✔
1027
                            lf->get_text_format());
1028
                        iter->fvs_text_source->register_view(this->tss_view);
4✔
1029
                        iter->fvs_text_source->replace_with_mutable(
4✔
1030
                            pretty_al, lf->get_text_format());
1031
                    } else {
4✔
1032
                        log_error(
×
1033
                            "unable to read file to pretty-print: %s -- file "
1034
                            "is not valid UTF-8",
1035
                            lf->get_path_for_key().c_str());
1036
                        iter->fvs_mtime = st.st_mtime;
×
1037
                        iter->fvs_file_indexed_size = lf->get_index_size();
×
1038
                        iter->fvs_file_size = st.st_size;
×
1039
                        iter->fvs_error = "file is not valid UTF-8";
×
1040
                    }
1041
                } else {
4✔
1042
                    auto errmsg = read_res.unwrapErr();
×
1043
                    log_error("unable to read file to pretty-print: %s -- %s",
×
1044
                              lf->get_path_for_key().c_str(),
1045
                              errmsg.c_str());
1046
                    iter->fvs_mtime = st.st_mtime;
×
1047
                    iter->fvs_file_indexed_size = lf->get_index_size();
×
1048
                    iter->fvs_file_size = st.st_size;
×
1049
                    iter->fvs_error = errmsg;
×
1050
                }
1051
            }
4✔
1052
        } catch (const line_buffer::error& e) {
×
1053
            iter = this->tss_files.erase(iter);
×
1054
            lf->close();
×
1055
            this->detach_observer(lf);
×
1056
            closed_files.emplace_back(lf);
×
1057
            continue;
×
1058
        }
×
1059

1060
        ++iter;
560✔
1061
    }
1,290✔
1062
    if (!closed_files.empty()) {
4,389✔
1063
        callback.closed_files(closed_files);
99✔
1064
        if (!this->tss_files.empty()) {
99✔
1065
            this->tss_files.front().load_into(*this->tss_view);
×
1066
        }
1067
        this->tss_view->set_needs_update();
99✔
1068
    }
1069

1070
    if (retval.rr_new_data) {
4,389✔
1071
        this->tss_view->search_new_data();
86✔
1072
    }
1073

1074
    return retval;
4,389✔
1075
}
4,407✔
1076

1077
void
1078
textfile_sub_source::set_top_from_off(file_off_t off)
×
1079
{
1080
    auto lf = this->current_file();
×
1081

1082
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1083
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1084
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1085
            std::distance(lf->cbegin(), new_top_iter));
×
1086

1087
        if (new_top_opt) {
×
1088
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1089
            if (this->tss_view->is_selectable()) {
×
1090
                this->tss_view->set_top(
×
1091
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1092
            }
1093
        }
1094
    };
1095
}
1096

1097
void
1098
textfile_sub_source::quiesce()
×
1099
{
1100
    for (auto& lf : this->tss_files) {
×
1101
        lf.fvs_file->quiesce();
×
1102
    }
1103
}
1104

1105
size_t
1106
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
13,016✔
1107
{
1108
    size_t retval = 0;
13,016✔
1109

1110
    if (mode == view_mode::raw || !this->fvs_text_source) {
13,016✔
1111
        const auto& lf = this->fvs_file;
7,208✔
1112
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
7,208✔
1113
            const auto fsize = lf->get_content_size();
28✔
1114
            retval = fsize / 16;
28✔
1115
            if (fsize % 16) {
28✔
1116
                retval += 1;
28✔
1117
            }
1118
        } else {
1119
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
7,180✔
1120
            if (lfo != nullptr) {
7,180✔
1121
                retval = lfo->lfo_filter_state.tfs_index.size();
7,180✔
1122
            }
1123
        }
1124
    } else {
1125
        retval = this->fvs_text_source->text_line_count();
5,808✔
1126
    }
1127

1128
    return retval;
13,016✔
1129
}
1130

1131
size_t
1132
textfile_sub_source::file_view_state::text_line_width(view_mode mode,
585✔
1133
                                                      textview_curses& tc) const
1134
{
1135
    size_t retval = 0;
585✔
1136
    if (mode == view_mode::raw || !this->fvs_text_source) {
585✔
1137
        const auto& lf = this->fvs_file;
455✔
1138
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
455✔
1139
            retval = 88;
5✔
1140
        } else {
1141
            retval = lf->get_longest_line_length();
450✔
1142
        }
1143
    } else {
1144
        retval = this->fvs_text_source->text_line_width(tc);
130✔
1145
    }
1146
    return retval;
585✔
1147
}
1148

1149
std::optional<vis_line_t>
1150
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
7✔
1151
                                                     const std::string& id)
1152
{
1153
    if (mode == view_mode::rendered && this->fvs_text_source) {
7✔
1154
        return this->fvs_text_source->row_for_anchor(id);
5✔
1155
    }
1156

1157
    if (!this->fvs_metadata.m_sections_root) {
2✔
1158
        return std::nullopt;
×
1159
    }
1160

1161
    const auto& lf = this->fvs_file;
2✔
1162
    const auto& meta = this->fvs_metadata;
2✔
1163
    std::optional<vis_line_t> retval;
2✔
1164

1165
    auto is_ptr = startswith(id, "#/");
2✔
1166
    if (is_ptr) {
2✔
1167
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1168
        std::vector<lnav::document::section_key_t> path;
2✔
1169

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

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

1194
        return retval;
2✔
1195
    }
2✔
1196

1197
    lnav::document::hier_node::depth_first(
×
1198
        meta.m_sections_root.get(),
1199
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1200
            for (const auto& child_pair : node->hn_named_children) {
×
1201
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1202

1203
                if (child_anchor != id) {
×
1204
                    continue;
×
1205
                }
1206

1207
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1208
                if (ll_opt != lf->end()) {
×
1209
                    retval = vis_line_t(
×
1210
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1211
                }
1212
                break;
×
1213
            }
1214
        });
×
1215

1216
    return retval;
×
1217
}
1218

1219
std::optional<vis_line_t>
1220
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1221
{
1222
    const auto curr_iter = this->current_file_state();
6✔
1223
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
1224
        return std::nullopt;
×
1225
    }
1226

1227
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
6✔
1228
}
1229

1230
static void
1231
anchor_generator(std::unordered_set<std::string>& retval,
×
1232
                 std::vector<std::string>& comps,
1233
                 size_t& max_depth,
1234
                 lnav::document::hier_node* hn)
1235
{
1236
    if (hn->hn_named_children.empty()) {
×
1237
        if (hn->hn_children.empty()) {
×
1238
            if (retval.size() >= 250 || comps.empty()) {
×
1239
            } else if (comps.size() == 1) {
×
1240
                retval.emplace(text_anchors::to_anchor_string(comps.front()));
×
1241
            } else {
1242
                retval.emplace(
×
1243
                    fmt::format(FMT_STRING("#/{}"),
×
1244
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1245
            }
1246
            max_depth = std::max(max_depth, comps.size());
×
1247
        } else {
1248
            int index = 0;
×
1249
            for (const auto& child : hn->hn_children) {
×
1250
                comps.emplace_back(fmt::to_string(index));
×
1251
                anchor_generator(retval, comps, max_depth, child.get());
×
1252
                comps.pop_back();
×
1253
            }
1254
        }
1255
    } else {
1256
        for (const auto& [child_name, child_node] : hn->hn_named_children) {
×
1257
            comps.emplace_back(child_name);
×
1258
            anchor_generator(retval, comps, max_depth, child_node);
×
1259
            comps.pop_back();
×
1260
        }
1261
        if (max_depth > 1) {
×
1262
            retval.emplace(
×
1263
                fmt::format(FMT_STRING("#/{}"),
×
1264
                            fmt::join(comps.begin(), comps.end(), "/")));
×
1265
        }
1266
    }
1267
}
1268

1269
std::unordered_set<std::string>
1270
textfile_sub_source::get_anchors()
×
1271
{
1272
    std::unordered_set<std::string> retval;
×
1273

1274
    const auto curr_iter = this->current_file_state();
×
1275
    if (curr_iter == this->tss_files.end()) {
×
1276
        return retval;
×
1277
    }
1278

1279
    if (this->tss_view_mode == view_mode::rendered
×
1280
        && curr_iter->fvs_text_source)
×
1281
    {
1282
        return curr_iter->fvs_text_source->get_anchors();
×
1283
    }
1284

1285
    const auto& meta = curr_iter->fvs_metadata;
×
1286
    if (meta.m_sections_root == nullptr) {
×
1287
        return retval;
×
1288
    }
1289

1290
    std::vector<std::string> comps;
×
1291
    size_t max_depth = 0;
×
1292
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1293

1294
    return retval;
×
1295
}
1296

1297
struct tfs_time_cmp {
1298
    bool operator()(int32_t lhs, const timeval& rhs) const
1✔
1299
    {
1300
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
1✔
1301
        return ll->get_timeval() < rhs;
1✔
1302
    }
1303

1304
    logfile* ttc_logfile;
1305
    std::vector<uint32_t>& ttc_index;
1306
};
1307

1308
std::optional<vis_line_t>
1309
textfile_sub_source::row_for_time(timeval time_bucket)
1✔
1310
{
1311
    auto lf = this->current_file();
1✔
1312
    if (!lf || !lf->has_line_metadata()) {
1✔
1313
        return std::nullopt;
×
1314
    }
1315

1316
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
1✔
1317
    auto& tfs = lfo->lfo_filter_state.tfs_index;
1✔
1318
    auto lb = std::lower_bound(
1✔
1319
        tfs.begin(), tfs.end(), time_bucket, tfs_time_cmp{lf.get(), tfs});
1✔
1320
    if (lb != tfs.end()) {
1✔
1321
        return vis_line_t{(int) std::distance(tfs.begin(), lb)};
2✔
1322
    }
1323

1324
    return std::nullopt;
×
1325
}
1✔
1326

1327
std::optional<text_time_translator::row_info>
1328
textfile_sub_source::time_for_row(vis_line_t row)
4,497✔
1329
{
1330
    auto lf = this->current_file();
4,497✔
1331
    if (!lf || !lf->has_line_metadata()) {
4,497✔
1332
        return std::nullopt;
4,344✔
1333
    }
1334

1335
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
153✔
1336
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
153✔
1337
        return std::nullopt;
41✔
1338
    }
1339
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
112✔
1340
    auto ll_iter = lf->begin() + row_id;
112✔
1341
    return row_info{
224✔
1342
        ll_iter->get_timeval(),
1343
        row_id,
1344
    };
112✔
1345
}
4,497✔
1346

1347
static std::optional<vis_line_t>
1348
to_vis_line(const std::shared_ptr<logfile>& lf, file_off_t off)
3✔
1349
{
1350
    auto ll_opt = lf->line_for_offset(off);
3✔
1351
    if (ll_opt != lf->end()) {
3✔
1352
        return vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
6✔
1353
    }
1354

1355
    return std::nullopt;
×
1356
}
1357

1358
std::optional<vis_line_t>
1359
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
3✔
1360
{
1361
    const auto curr_iter = this->current_file_state();
3✔
1362
    if (curr_iter == this->tss_files.end()) {
3✔
1363
        return std::nullopt;
×
1364
    }
1365

1366
    const auto& lf = curr_iter->fvs_file;
3✔
1367
    log_debug("adjacent_anchor: %s:L%d:%s",
3✔
1368
              lf->get_path_for_key().c_str(),
1369
              (int) vl,
1370
              dir == text_anchors::direction::prev ? "prev" : "next");
1371
    if (this->tss_view_mode == view_mode::rendered
6✔
1372
        && curr_iter->fvs_text_source)
3✔
1373
    {
1374
        return curr_iter->fvs_text_source->adjacent_anchor(vl, dir);
×
1375
    }
1376

1377
    if (!curr_iter->fvs_metadata.m_sections_root) {
3✔
1378
        log_debug("  no metadata available");
×
1379
        return std::nullopt;
×
1380
    }
1381

1382
    auto& md = curr_iter->fvs_metadata;
3✔
1383
    const auto* lfo
1384
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1385
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
3✔
1386
        || md.m_sections_root == nullptr)
3✔
1387
    {
1388
        return std::nullopt;
×
1389
    }
1390
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
3✔
1391
    const auto line_offsets = lf->get_file_range(ll_iter, false);
3✔
1392
    log_debug(
3✔
1393
        "  range %lld:%zu", line_offsets.fr_offset, line_offsets.next_offset());
1394
    auto path_for_line
1395
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
3✔
1396

1397
    if (path_for_line.empty()) {
3✔
1398
        log_debug("  no path found");
×
1399
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
1400
        if (!neighbors_res) {
×
1401
            return std::nullopt;
×
1402
        }
1403

1404
        switch (dir) {
×
1405
            case direction::prev: {
×
1406
                if (neighbors_res->cnr_previous) {
×
1407
                    return to_vis_line(
×
1408
                        lf, neighbors_res->cnr_previous.value()->hn_start);
×
1409
                }
1410
                break;
×
1411
            }
1412
            case direction::next: {
×
1413
                if (neighbors_res->cnr_next) {
×
1414
                    return to_vis_line(
×
1415
                        lf, neighbors_res->cnr_next.value()->hn_start);
×
1416
                }
1417
                if (!md.m_sections_root->hn_children.empty()) {
×
1418
                    return to_vis_line(
×
1419
                        lf, md.m_sections_root->hn_children[0]->hn_start);
×
1420
                }
1421
                break;
×
1422
            }
1423
        }
1424
        return std::nullopt;
×
1425
    }
1426

1427
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
3✔
1428
    const auto last_key = std::move(path_for_line.back());
3✔
1429
    path_for_line.pop_back();
3✔
1430

1431
    const auto parent_opt = lnav::document::hier_node::lookup_path(
3✔
1432
        md.m_sections_root.get(), path_for_line);
3✔
1433
    if (!parent_opt) {
3✔
1434
        log_debug("  no parent for path: %s",
×
1435
                  fmt::to_string(path_for_line).c_str());
1436
        return std::nullopt;
×
1437
    }
1438
    const auto parent = parent_opt.value();
3✔
1439

1440
    const auto child_hn = parent->lookup_child(last_key);
3✔
1441
    if (!child_hn) {
3✔
1442
        // XXX "should not happen"
1443
        log_debug("  child not found");
×
1444
        return std::nullopt;
×
1445
    }
1446

1447
    auto neighbors_res = parent->child_neighbors(
9✔
1448
        child_hn.value(), line_offsets.next_offset() + 1);
3✔
1449
    if (!neighbors_res) {
3✔
1450
        log_debug("  no neighbors found");
×
1451
        return std::nullopt;
×
1452
    }
1453

1454
    log_debug("  neighbors p:%d n:%d",
3✔
1455
              neighbors_res->cnr_previous.has_value(),
1456
              neighbors_res->cnr_next.has_value());
1457
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
3✔
1458
        auto neighbor_sub
1459
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
3✔
1460
        if (neighbor_sub) {
3✔
1461
            neighbors_res->cnr_previous = neighbor_sub;
×
1462
        }
1463
    }
1464

1465
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
3✔
1466
        auto neighbor_sub
1467
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
3✔
1468
        if (neighbor_sub) {
3✔
1469
            neighbors_res->cnr_next = neighbor_sub;
3✔
1470
        }
1471
    }
1472

1473
    switch (dir) {
3✔
1474
        case direction::prev: {
1✔
1475
            if (neighbors_res->cnr_previous) {
1✔
1476
                return to_vis_line(
1✔
1477
                    lf, neighbors_res->cnr_previous.value()->hn_start);
1✔
1478
            }
1479
            break;
×
1480
        }
1481
        case direction::next: {
2✔
1482
            if (neighbors_res->cnr_next) {
2✔
1483
                return to_vis_line(lf,
2✔
1484
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1485
            }
1486
            break;
×
1487
        }
1488
    }
1489

1490
    return std::nullopt;
×
1491
}
3✔
1492

1493
std::optional<std::string>
1494
textfile_sub_source::anchor_for_row(vis_line_t vl)
12✔
1495
{
1496
    const auto curr_iter = this->current_file_state();
12✔
1497
    if (curr_iter == this->tss_files.end()) {
12✔
1498
        return std::nullopt;
×
1499
    }
1500

1501
    if (this->tss_view_mode == view_mode::rendered
24✔
1502
        && curr_iter->fvs_text_source)
12✔
1503
    {
1504
        return curr_iter->fvs_text_source->anchor_for_row(vl);
3✔
1505
    }
1506

1507
    if (!curr_iter->fvs_metadata.m_sections_root) {
9✔
1508
        return std::nullopt;
×
1509
    }
1510

1511
    const auto& lf = curr_iter->fvs_file;
9✔
1512
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
9✔
1513
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
9✔
1514
        return std::nullopt;
×
1515
    }
1516
    auto& md = curr_iter->fvs_metadata;
9✔
1517
    auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
9✔
1518
    auto line_offsets = lf->get_file_range(ll_iter, false);
9✔
1519
    auto path_for_line
1520
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
9✔
1521

1522
    if (path_for_line.empty()) {
9✔
1523
        return std::nullopt;
5✔
1524
    }
1525

1526
    if ((path_for_line.size() == 1
4✔
1527
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1528
        && path_for_line.back().is<std::string>())
6✔
1529
    {
1530
        return text_anchors::to_anchor_string(
2✔
1531
            path_for_line.back().get<std::string>());
2✔
1532
    }
1533

1534
    auto comps
1535
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1536
              return elem.match(
1537
                  [](const std::string& str) {
×
1538
                      stack_buf allocator;
5✔
1539
                      return json_ptr::encode(str, allocator).to_string();
10✔
1540
                  },
5✔
1541
                  [](size_t index) { return fmt::to_string(index); });
13✔
1542
          });
2✔
1543

1544
    return fmt::format(FMT_STRING("#/{}"),
8✔
1545
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1546
}
9✔
1547

1548
bool
1549
textfile_sub_source::to_front(const std::string& filename)
1✔
1550
{
1551
    auto lf_opt = this->tss_files
1✔
1552
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1553
                      return elem.fvs_file->get_filename() == filename;
1✔
1554
                  });
1✔
1555
    if (!lf_opt) {
1✔
1556
        return false;
1✔
1557
    }
1558

1559
    this->to_front(lf_opt.value()->fvs_file);
×
1560

1561
    return true;
×
1562
}
1563

1564
logline*
1565
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1566
{
1567
    auto lf = this->current_file();
3✔
1568
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1569
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1570
}
3✔
1571

1572
void
1573
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1574
{
1575
    this->tss_view_mode = vm;
2✔
1576
    this->tss_view->set_needs_update();
2✔
1577
}
2✔
1578

1579
textfile_sub_source::view_mode
1580
textfile_sub_source::get_effective_view_mode() const
384✔
1581
{
1582
    auto retval = view_mode::raw;
384✔
1583

1584
    const auto curr_iter = this->current_file_state();
384✔
1585
    if (curr_iter != this->tss_files.end()) {
384✔
1586
        if (this->tss_view_mode == view_mode::rendered
768✔
1587
            && curr_iter->fvs_text_source)
384✔
1588
        {
1589
            retval = view_mode::rendered;
19✔
1590
        }
1591
    }
1592

1593
    return retval;
384✔
1594
}
1595

1596
void
1597
textfile_sub_source::move_to_init_location(file_iterator& iter)
568✔
1598
{
1599
    if (iter->fvs_consumed_init_location) {
568✔
1600
        return;
466✔
1601
    }
1602

1603
    auto& lf = iter->fvs_file;
102✔
1604
    std::optional<vis_line_t> new_sel_opt;
102✔
1605
    require(lf->get_open_options().loo_init_location.valid());
102✔
1606
    lf->get_open_options().loo_init_location.match(
102✔
1607
        [this, &new_sel_opt, &lf](default_for_text_format def) {
×
1608
            if (!this->tss_apply_default_init_location) {
99✔
1609
                return;
99✔
1610
            }
1611
            auto tf = lf->get_text_format().value_or(text_format_t::TF_BINARY);
×
1612
            switch (tf) {
×
1613
                case text_format_t::TF_PLAINTEXT:
×
1614
                case text_format_t::TF_LOG: {
1615
                    log_info("file open request to tail");
×
1616
                    auto inner_height = lf->size();
×
1617
                    if (inner_height > 0) {
×
1618
                        new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1619
                    }
1620
                    break;
×
1621
                }
1622
                default:
×
1623
                    log_info("file open is %s, moving to top",
×
1624
                             fmt::to_string(tf).c_str());
1625
                    new_sel_opt = 0_vl;
×
1626
                    break;
×
1627
            }
1628
        },
1629
        [&new_sel_opt, &lf](file_location_tail tail) {
×
1630
            log_info("file open request to tail");
×
1631
            auto inner_height = lf->size();
×
1632
            if (inner_height > 0) {
×
1633
                new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1634
            }
1635
        },
×
1636
        [this, &new_sel_opt, &iter](int vl) {
×
1637
            log_info("file open request to jump to line: %d", vl);
2✔
1638
            auto height = iter->text_line_count(this->tss_view_mode);
2✔
1639
            if (vl < 0) {
2✔
1640
                vl += height;
2✔
1641
                if (vl < 0) {
2✔
1642
                    vl = 0;
1✔
1643
                }
1644
            }
1645
            if (vl < height) {
2✔
1646
                new_sel_opt = vis_line_t(vl);
2✔
1647
            }
1648
        },
2✔
1649
        [this, &new_sel_opt, &iter](const std::string& loc) {
102✔
1650
            log_info("file open request to jump to anchor: %s", loc.c_str());
1✔
1651
            new_sel_opt = iter->row_for_anchor(this->tss_view_mode, loc);
1✔
1652
        });
1✔
1653

1654
    if (new_sel_opt) {
102✔
1655
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1656
        log_info("  setting requested selection: %d",
3✔
1657
                 (int) new_sel_opt.value());
1658
        iter->fvs_selection = new_sel_opt;
3✔
1659
        log_info("  actual top is now: %d", (int) iter->fvs_top);
3✔
1660
        log_info("  actual selection is now: %d",
3✔
1661
                 (int) iter->fvs_selection.value());
1662

1663
        if (this->current_file() == lf) {
3✔
1664
            this->tss_view->set_selection(iter->fvs_selection.value());
3✔
1665
        }
1666
    }
1667
    iter->fvs_consumed_init_location = true;
102✔
1668
}
1669

1670
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
645✔
1671
                                                 text_sub_source* log_src)
645✔
1672
    : tho_src(src), tho_log_src(log_src)
645✔
1673
{
1674
}
645✔
1675

1676
bool
1677
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
8,543✔
1678
                                             media_t media,
1679
                                             int y,
1680
                                             int bottom,
1681
                                             attr_line_t& value_out)
1682
{
1683
    if (media == media_t::display) {
8,543✔
1684
        const std::vector<attr_line_t>* lines = nullptr;
8,487✔
1685
        auto curr_file = this->tho_src->current_file();
8,487✔
1686
        if (curr_file == nullptr) {
8,487✔
1687
            if (this->tho_log_src->text_line_count() == 0) {
×
1688
                lines = lnav::messages::view::no_files();
×
1689
            } else {
1690
                lines = lnav::messages::view::only_log_files();
×
1691
            }
1692
        } else if (!curr_file->get_notes().empty()) {
8,487✔
1693
            this->tho_static_lines = curr_file->get_notes()
×
1694
                                         .values()
×
1695
                                         .front()
×
1696
                                         .to_attr_line()
×
1697
                                         .split_lines();
×
1698
            lines = &this->tho_static_lines;
×
1699
        } else if (curr_file->size() == 0) {
8,487✔
1700
            lines = lnav::messages::view::empty_file();
×
1701
        } else if (this->tho_src->text_line_count() == 0) {
8,487✔
1702
            hasher h;
×
1703
            this->tho_src->update_filter_hash_state(h);
×
1704
            auto curr_state = h.to_array();
×
1705
            if (this->tho_static_lines.empty()
×
1706
                || curr_state != this->tho_filter_state)
×
1707
            {
1708
                auto msg = lnav::console::user_message::info(
1709
                    "All text lines are currently hidden");
×
1710
                auto min_time = this->tho_src->get_min_row_time();
×
1711
                if (min_time) {
×
1712
                    msg.with_note(attr_line_t("Lines before ")
×
1713
                                      .append_quoted(lnav::to_rfc3339_string(
×
1714
                                          min_time.value()))
×
1715
                                      .append(" are not being shown"));
×
1716
                }
1717
                auto max_time = this->tho_src->get_max_row_time();
×
1718
                if (max_time) {
×
1719
                    msg.with_note(attr_line_t("Lines after ")
×
1720
                                      .append_quoted(lnav::to_rfc3339_string(
×
1721
                                          max_time.value()))
×
1722
                                      .append(" are not being shown"));
×
1723
                }
1724
                auto& fs = this->tho_src->get_filters();
×
1725
                for (const auto& filt : fs) {
×
1726
                    auto hits = this->tho_src->get_filtered_count_for(
×
1727
                        filt->get_index());
1728
                    if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
1729
                        continue;
×
1730
                    }
1731
                    auto cmd = attr_line_t(":" + filt->to_command());
×
1732
                    readline_command_highlighter(cmd, std::nullopt);
×
1733
                    msg.with_note(
×
1734
                        attr_line_t("Filter ")
×
1735
                            .append_quoted(cmd)
×
1736
                            .append(" matched ")
×
1737
                            .append(lnav::roles::number(fmt::to_string(hits)))
×
1738
                            .append(" line(s) "));
×
1739
                }
1740
                this->tho_static_lines = msg.to_attr_line().split_lines();
×
1741
                this->tho_filter_state = curr_state;
×
1742
            }
1743

1744
            lines = &this->tho_static_lines;
×
1745
        }
1746

1747
        if (lines != nullptr && y < (ssize_t) lines->size()) {
8,487✔
1748
            value_out = lines->at(y);
×
1749
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
1750
            if (y == (ssize_t) lines->size() - 1) {
×
1751
                value_out.with_attr_for_all(
×
1752
                    VC_STYLE.value(text_attrs::with_underline()));
×
1753
            }
1754
            return true;
×
1755
        }
1756
    }
8,487✔
1757

1758
    if (y != 0) {
8,543✔
1759
        return false;
8,024✔
1760
    }
1761

1762
    const auto lf = this->tho_src->current_file();
519✔
1763
    if (lf == nullptr) {
519✔
1764
        return false;
×
1765
    }
1766

1767
    if (media == media_t::display
519✔
1768
        && lf->get_text_format() != text_format_t::TF_MARKDOWN
464✔
1769
        && this->tho_src->get_effective_view_mode()
983✔
1770
            == textfile_sub_source::view_mode::rendered)
1771
    {
1772
        auto ta = text_attrs::with_underline();
19✔
1773
        value_out.append("\u24d8"_info)
19✔
1774
            .append(" The following is a rendered view of the content.  Use ")
19✔
1775
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
19✔
1776
            .append(" to view the raw version of this text")
19✔
1777
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
38✔
1778
            .with_attr_for_all(VC_STYLE.value(ta));
19✔
1779
        return true;
19✔
1780
    }
1781

1782
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
500✔
1783
        return false;
494✔
1784
    }
1785

1786
    {
1787
        attr_line_builder alb(value_out);
6✔
1788
        {
1789
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
6✔
1790
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
18✔
1791
        }
6✔
1792
        size_t byte_off = 0;
6✔
1793
        for (size_t lpc = 0; lpc < 16; lpc++) {
102✔
1794
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
96✔
1795
            if (byte_off == 8) {
96✔
1796
                alb.append(" ");
6✔
1797
            }
1798
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
288✔
1799
            byte_off += 1;
96✔
1800
        }
96✔
1801
        {
1802
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
6✔
1803
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
18✔
1804
        }
6✔
1805
    }
1806
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
6✔
1807
    return true;
6✔
1808
}
519✔
1809

1810
std::optional<attr_line_t>
1811
textfile_header_overlay::list_header_for_overlay(const listview_curses& lv,
4✔
1812
                                                 media_t media,
1813
                                                 vis_line_t line)
1814
{
1815
    return this->tho_hex_line_header;
4✔
1816
}
1817

1818
void
1819
textfile_header_overlay::list_value_for_overlay(
10,336✔
1820
    const listview_curses& lv,
1821
    vis_line_t line,
1822
    std::vector<attr_line_t>& value_out)
1823
{
1824
    if (line != lv.get_selection()) {
10,336✔
1825
        return;
10,330✔
1826
    }
1827

1828
    if (this->tho_src->empty() || line < 0) {
239✔
1829
        value_out.clear();
×
1830
        return;
×
1831
    }
1832

1833
    const auto curr_iter = this->tho_src->current_file_state();
239✔
1834
    if (this->tho_src->tss_view_mode == textfile_sub_source::view_mode::rendered
478✔
1835
        && curr_iter->fvs_text_source)
239✔
1836
    {
1837
        return;
26✔
1838
    }
1839
    const auto& lf = curr_iter->fvs_file;
213✔
1840
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
213✔
1841
        return;
6✔
1842
    }
1843

1844
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
207✔
1845
    if (lfo == nullptr
207✔
1846
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
207✔
1847
    {
1848
        value_out.clear();
×
1849
        return;
×
1850
    }
1851

1852
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
207✔
1853
    if (ll->is_valid_utf()) {
207✔
1854
        return;
201✔
1855
    }
1856

1857
    auto read_opts = subline_options{};
6✔
1858
    read_opts.scrub_invalid_utf8 = false;
6✔
1859
    auto read_result = lf->read_line(ll, read_opts);
6✔
1860
    if (read_result.isErr()) {
6✔
1861
        return;
×
1862
    }
1863

1864
    auto sbr = read_result.unwrap();
6✔
1865
    attr_line_t al;
6✔
1866
    attr_line_builder alb(al);
6✔
1867
    alb.append_as_hexdump(sbr.to_string_fragment());
6✔
1868
    this->tho_hex_line_header
1869
        = attr_line_t(" Line ")
6✔
1870
              .append(lnav::roles::number(fmt::to_string(line + 1)))
12✔
1871
              .append(" at file offset ")
6✔
1872
              .append(lnav::roles::number(fmt::to_string(ll->get_offset())))
12✔
1873
              .append(
6✔
1874
                  " contains invalid UTF-8 content, the following is a hex "
1875
                  "dump of the line");
6✔
1876
    al.split_lines(value_out);
6✔
1877
}
6✔
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