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

tstack / lnav / 21219323786-2766

21 Jan 2026 05:28PM UTC coverage: 68.961% (-0.04%) from 68.999%
21219323786-2766

push

github

tstack
[log_format] display an error if there is a JSON timestamp property, but it does not parse correctly

41 of 50 new or added lines in 3 files covered. (82.0%)

40 existing lines in 6 files now uncovered.

51838 of 75170 relevant lines covered (68.96%)

437089.4 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 "lnav.events.hh"
51
#include "md2attr_line.hh"
52
#include "msg.text.hh"
53
#include "pretty_printer.hh"
54
#include "readline_highlighters.hh"
55
#include "scn/scan.h"
56
#include "sql_util.hh"
57
#include "sqlitepp.hh"
58
#include "textfile_sub_source.cfg.hh"
59
#include "yajlpp/yajlpp_def.hh"
60

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

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

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

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

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

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

98
    return retval;
20,537✔
99
}
100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

356
    return retval;
×
357
}
358

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

543
static attr_line_t
544
to_display(const std::shared_ptr<logfile>& lf)
22✔
545
{
546
    attr_line_t retval;
22✔
547

548
    if (lf->get_open_options().loo_piper) {
22✔
549
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
550
            retval.append("\u21bb "_list_glyph);
×
551
        }
552
    }
553
    retval.append(lf->get_unique_path());
22✔
554

555
    return retval;
22✔
556
}
×
557

558
void
559
textfile_sub_source::text_crumbs_for_line(
11✔
560
    int line, std::vector<breadcrumb::crumb>& crumbs)
561
{
562
    text_sub_source::text_crumbs_for_line(line, crumbs);
11✔
563

564
    if (this->empty()) {
11✔
565
        return;
×
566
    }
567

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

590
            if (!lf_opt) {
×
591
                return;
×
592
            }
593

594
            this->to_front(lf_opt.value());
×
595
            this->tss_view->reload_data();
×
596
        });
×
597
    if (lf->size() == 0) {
11✔
598
        return;
×
599
    }
600

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

612
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
613

614
        crumbs.emplace_back(
2✔
615
            std::string(ts),
4✔
616
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
617
            [](const auto& key) {});
2✔
618
    }
619

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

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

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

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

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

743
textfile_sub_source::rescan_result_t
744
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
4,462✔
745
                                  std::optional<ui_clock::time_point> deadline)
746
{
747
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,462✔
748

749
    file_iterator iter;
4,462✔
750
    rescan_result_t retval;
4,462✔
751
    size_t files_scanned = 0;
4,462✔
752

753
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,462✔
754
        return retval;
124✔
755
    }
756

757
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,338✔
758

759
    std::vector<std::shared_ptr<logfile>> closed_files;
4,338✔
760
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
5,619✔
761
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,281✔
762
        {
763
            log_info("rescan_files() deadline reached, breaking...");
×
764
            retval.rr_scan_completed = false;
×
765
            this->tss_last_scan_aborted = true;
×
766
            break;
×
767
        }
768

769
        std::shared_ptr<logfile> lf = iter->fvs_file;
1,281✔
770

771
        if (lf->is_closed()) {
1,281✔
772
            iter = this->tss_files.erase(iter);
103✔
773
            this->detach_observer(lf);
103✔
774
            closed_files.emplace_back(lf);
103✔
775
            retval.rr_rescan_needed = true;
103✔
776
            continue;
103✔
777
        }
778

779
        if (last_aborted && lf->size() > 0) {
1,178✔
780
            retval.rr_scan_completed = false;
×
781
            ++iter;
×
782
            continue;
×
783
        }
784
        files_scanned += 1;
1,178✔
785

786
        try {
787
            const auto& st = lf->get_stat();
1,178✔
788
            uint32_t old_size = lf->size();
1,178✔
789
            auto new_text_data = lf->rebuild_index(deadline);
1,178✔
790

791
            if (lf->get_format() != nullptr) {
1,178✔
792
                iter = this->tss_files.erase(iter);
520✔
793
                this->detach_observer(lf);
520✔
794
                callback.promote_file(lf);
520✔
795
                continue;
618✔
796
            }
797

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

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

840
                if (!iter->fvs_metadata.m_sections_root
652✔
841
                    && iter->fvs_error.empty())
652✔
842
                {
843
                    auto read_res
844
                        = lf->read_file(logfile::read_format_t::with_framing);
101✔
845

846
                    if (read_res.isOk()) {
101✔
847
                        auto read_file_res = read_res.unwrap();
101✔
848
                        auto tf_opt = lf->get_text_format();
101✔
849

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

865
                            log_info("generating metadata for: %s (size=%zu)",
83✔
866
                                     lf->get_path_for_key().c_str(),
867
                                     content.length());
868
                            scrub_ansi_string(content.get_string(),
83✔
869
                                              &content.get_attrs());
83✔
870

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

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

903
            uint32_t filter_in_mask, filter_out_mask;
904

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

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

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

941
                    mdal.with_source_path(lf->get_actual_path());
15✔
942
                    if (this->tss_view->tc_interactive) {
15✔
943
                        mdal.add_lnav_script_icons();
×
944
                    }
945
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
15✔
946

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

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

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

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

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

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

1056
        ++iter;
560✔
1057
    }
1,281✔
1058
    if (!closed_files.empty()) {
4,338✔
1059
        callback.closed_files(closed_files);
99✔
1060
        if (!this->tss_files.empty()) {
99✔
1061
            this->tss_files.front().load_into(*this->tss_view);
×
1062
        }
1063
        this->tss_view->set_needs_update();
99✔
1064
    }
1065

1066
    if (retval.rr_new_data) {
4,338✔
1067
        this->tss_view->search_new_data();
86✔
1068
    }
1069

1070
    return retval;
4,338✔
1071
}
4,356✔
1072

1073
void
1074
textfile_sub_source::set_top_from_off(file_off_t off)
×
1075
{
1076
    auto lf = this->current_file();
×
1077

1078
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1079
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1080
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1081
            std::distance(lf->cbegin(), new_top_iter));
×
1082

1083
        if (new_top_opt) {
×
1084
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1085
            if (this->tss_view->is_selectable()) {
×
1086
                this->tss_view->set_top(
×
1087
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1088
            }
1089
        }
1090
    };
1091
}
1092

1093
void
1094
textfile_sub_source::quiesce()
×
1095
{
1096
    for (auto& lf : this->tss_files) {
×
1097
        lf.fvs_file->quiesce();
×
1098
    }
1099
}
1100

1101
size_t
1102
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
13,008✔
1103
{
1104
    size_t retval = 0;
13,008✔
1105

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

1124
    return retval;
13,008✔
1125
}
1126

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

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

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

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

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

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

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

1190
        return retval;
2✔
1191
    }
2✔
1192

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

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

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

1212
    return retval;
×
1213
}
1214

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

1223
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
6✔
1224
}
1225

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

1265
std::unordered_set<std::string>
1266
textfile_sub_source::get_anchors()
×
1267
{
1268
    std::unordered_set<std::string> retval;
×
1269

1270
    const auto curr_iter = this->current_file_state();
×
1271
    if (curr_iter == this->tss_files.end()) {
×
1272
        return retval;
×
1273
    }
1274

1275
    if (this->tss_view_mode == view_mode::rendered
×
1276
        && curr_iter->fvs_text_source)
×
1277
    {
1278
        return curr_iter->fvs_text_source->get_anchors();
×
1279
    }
1280

1281
    const auto& meta = curr_iter->fvs_metadata;
×
1282
    if (meta.m_sections_root == nullptr) {
×
1283
        return retval;
×
1284
    }
1285

1286
    std::vector<std::string> comps;
×
1287
    size_t max_depth = 0;
×
1288
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1289

1290
    return retval;
×
1291
}
1292

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

1300
    logfile* ttc_logfile;
1301
    std::vector<uint32_t>& ttc_index;
1302
};
1303

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

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

1320
    return std::nullopt;
×
1321
}
1✔
1322

1323
std::optional<text_time_translator::row_info>
1324
textfile_sub_source::time_for_row(vis_line_t row)
4,445✔
1325
{
1326
    auto lf = this->current_file();
4,445✔
1327
    if (!lf || !lf->has_line_metadata()) {
4,445✔
1328
        return std::nullopt;
4,292✔
1329
    }
1330

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

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

1351
    return std::nullopt;
×
1352
}
1353

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

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

1373
    if (!curr_iter->fvs_metadata.m_sections_root) {
3✔
1374
        log_debug("  no metadata available");
×
1375
        return std::nullopt;
×
1376
    }
1377

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

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

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

1423
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
3✔
1424
    const auto last_key = std::move(path_for_line.back());
3✔
1425
    path_for_line.pop_back();
3✔
1426

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

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

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

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

1461
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
3✔
1462
        auto neighbor_sub
1463
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
3✔
1464
        if (neighbor_sub) {
3✔
1465
            neighbors_res->cnr_next = neighbor_sub;
3✔
1466
        }
1467
    }
1468

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

1486
    return std::nullopt;
×
1487
}
3✔
1488

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

1497
    if (this->tss_view_mode == view_mode::rendered
24✔
1498
        && curr_iter->fvs_text_source)
12✔
1499
    {
1500
        return curr_iter->fvs_text_source->anchor_for_row(vl);
3✔
1501
    }
1502

1503
    if (!curr_iter->fvs_metadata.m_sections_root) {
9✔
1504
        return std::nullopt;
×
1505
    }
1506

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

1518
    if (path_for_line.empty()) {
9✔
1519
        return std::nullopt;
5✔
1520
    }
1521

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

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

1540
    return fmt::format(FMT_STRING("#/{}"),
8✔
1541
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1542
}
9✔
1543

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

1555
    this->to_front(lf_opt.value()->fvs_file);
×
1556

1557
    return true;
×
1558
}
1559

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

1568
void
1569
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1570
{
1571
    this->tss_view_mode = vm;
2✔
1572
    this->tss_view->set_needs_update();
2✔
1573
}
2✔
1574

1575
textfile_sub_source::view_mode
1576
textfile_sub_source::get_effective_view_mode() const
384✔
1577
{
1578
    auto retval = view_mode::raw;
384✔
1579

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

1589
    return retval;
384✔
1590
}
1591

1592
void
1593
textfile_sub_source::move_to_init_location(file_iterator& iter)
568✔
1594
{
1595
    if (iter->fvs_consumed_init_location) {
568✔
1596
        return;
466✔
1597
    }
1598

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

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

1659
        if (this->current_file() == lf) {
3✔
1660
            this->tss_view->set_selection(iter->fvs_selection.value());
3✔
1661
        }
1662
    }
1663
    iter->fvs_consumed_init_location = true;
102✔
1664
}
1665

1666
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
637✔
1667
                                                 text_sub_source* log_src)
637✔
1668
    : tho_src(src), tho_log_src(log_src)
637✔
1669
{
1670
}
637✔
1671

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

1740
            lines = &this->tho_static_lines;
×
1741
        }
1742

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

1754
    if (y != 0) {
8,543✔
1755
        return false;
8,024✔
1756
    }
1757

1758
    const auto lf = this->tho_src->current_file();
519✔
1759
    if (lf == nullptr) {
519✔
1760
        return false;
×
1761
    }
1762

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

1778
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
500✔
1779
        return false;
494✔
1780
    }
1781

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

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

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

1824
    if (this->tho_src->empty() || line < 0) {
240✔
1825
        value_out.clear();
×
1826
        return;
×
1827
    }
1828

1829
    const auto curr_iter = this->tho_src->current_file_state();
240✔
1830
    if (this->tho_src->tss_view_mode == textfile_sub_source::view_mode::rendered
480✔
1831
        && curr_iter->fvs_text_source)
240✔
1832
    {
1833
        return;
26✔
1834
    }
1835
    const auto& lf = curr_iter->fvs_file;
214✔
1836
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
214✔
1837
        return;
6✔
1838
    }
1839

1840
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
208✔
1841
    if (lfo == nullptr
208✔
1842
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
208✔
1843
    {
1844
        value_out.clear();
×
1845
        return;
×
1846
    }
1847

1848
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
208✔
1849
    if (ll->is_valid_utf()) {
208✔
1850
        return;
202✔
1851
    }
1852

1853
    auto read_opts = subline_options{};
6✔
1854
    read_opts.scrub_invalid_utf8 = false;
6✔
1855
    auto read_result = lf->read_line(ll, read_opts);
6✔
1856
    if (read_result.isErr()) {
6✔
1857
        return;
×
1858
    }
1859

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