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

tstack / lnav / 25607745727-3034

09 May 2026 05:46PM UTC coverage: 70.2% (-0.004%) from 70.204%
25607745727-3034

push

github

tstack
[build] change how expected output is added to dist

57548 of 81977 relevant lines covered (70.2%)

634070.89 hits per line

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

69.57
/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 "base/string_util.hh"
48
#include "bound_tags.hh"
49
#include "config.h"
50
#include "data_scanner.hh"
51
#include "file_collection.hh"
52
#include "lnav.events.hh"
53
#include "md2attr_line.hh"
54
#include "msg.text.hh"
55
#include "pretty_printer.hh"
56
#include "readline_highlighters.hh"
57
#include "scn/scan.h"
58
#include "sql_util.hh"
59
#include "sqlitepp.hh"
60
#include "textfile_sub_source.cfg.hh"
61
#include "yajlpp/yajlpp_def.hh"
62

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

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

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

75
    switch (lf->get_text_format().value_or(text_format_t::TF_BINARY)) {
589✔
76
        case text_format_t::TF_BINARY:
114✔
77
        case text_format_t::TF_DIFF:
78
            return false;
114✔
79
        default:
475✔
80
            if (lf->get_content_size() < 16 * 1024
475✔
81
                && lf->get_longest_line_length()
950✔
82
                    > cfg.c_max_unformatted_line_length)
475✔
83
            {
84
                return true;
19✔
85
            }
86
            return false;
456✔
87
    }
88
}
89

90
size_t
91
textfile_sub_source::text_line_count()
34,309✔
92
{
93
    size_t retval = 0;
34,309✔
94

95
    if (!this->tss_files.empty()) {
34,309✔
96
        retval = (*this->current_file_state())
24,536✔
97
                     ->text_line_count(this->tss_view_mode);
24,536✔
98
    }
99

100
    return retval;
34,309✔
101
}
102

103
size_t
104
textfile_sub_source::text_line_width(textview_curses& tc)
7,230✔
105
{
106
    const auto iter = this->current_file_state();
7,230✔
107
    if (iter == this->tss_files.end()) {
7,230✔
108
        return 0;
6,568✔
109
    }
110

111
    return (*iter)->text_line_width(this->tss_view_mode, tc);
662✔
112
}
113

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

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

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

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

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

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

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

177
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
791✔
178
    auto read_opts = subline_options{};
791✔
179
    read_opts.scrub_invalid_utf8 = false;
791✔
180
    auto read_result = lf->read_line(ll, read_opts);
791✔
181
    this->tss_line_indent_size = 0;
791✔
182
    this->tss_plain_line_attrs.clear();
791✔
183
    if (read_result.isOk()) {
791✔
184
        auto sbr = read_result.unwrap();
791✔
185
        value_out = to_string(sbr);
791✔
186
        const auto& meta = sbr.get_metadata();
791✔
187
        if (meta.m_valid_utf && meta.m_has_ansi) {
791✔
188
            scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
35✔
189
        }
190
        this->tss_line_indent_size = compute_indent_size(value_out);
791✔
191
        if (this->tss_line_indent_size == 0 && value_out.empty()) {
791✔
192
            for (auto next = line + 1;
140✔
193
                 next < (ssize_t) lfo->lfo_filter_state.tfs_index.size();
140✔
194
                 ++next)
195
            {
196
                auto next_ll
197
                    = lf->begin() + lfo->lfo_filter_state.tfs_index[next];
140✔
198
                auto next_result = lf->read_line(next_ll, read_opts);
140✔
199
                if (next_result.isOk()) {
140✔
200
                    auto next_sbr = next_result.unwrap();
140✔
201
                    auto next_str = to_string(next_sbr);
140✔
202
                    this->tss_line_indent_size
203
                        = compute_indent_size(next_str) + 1;
140✔
204
                    if (!next_str.empty()) {
140✔
205
                        break;
139✔
206
                    }
207
                }
279✔
208
            }
140✔
209
        }
210
        if (lf->has_line_metadata() && this->tas_display_time_offset) {
791✔
211
            auto relstr = this->get_time_offset_for_line(tc, vis_line_t(line));
1✔
212
            value_out
213
                = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
4✔
214
        }
1✔
215
    }
791✔
216

217
    return {};
791✔
218
}
791✔
219

220
void
221
textfile_sub_source::text_attrs_for_line(textview_curses& tc,
2,217✔
222
                                         int row,
223
                                         string_attrs_t& value_out)
224
{
225
    const auto curr_iter = this->current_file_state();
2,217✔
226
    if (curr_iter == this->tss_files.end()) {
2,217✔
227
        return;
×
228
    }
229
    const auto& lf = (*curr_iter)->fvs_file;
2,217✔
230

231
    auto lr = line_range{0, -1};
2,217✔
232
    if (this->tss_view_mode == view_mode::rendered
4,434✔
233
        && (*curr_iter)->fvs_text_source)
2,217✔
234
    {
235
        (*curr_iter)->fvs_text_source->text_attrs_for_line(tc, row, value_out);
1,424✔
236
    } else if (lf->get_text_format() == text_format_t::TF_BINARY) {
793✔
237
        value_out = this->tss_hex_line.get_attrs();
2✔
238
    } else {
239
        value_out = this->tss_plain_line_attrs;
791✔
240
        auto* lfo
241
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
791✔
242
        if (lfo != nullptr && row >= 0
791✔
243
            && row < (ssize_t) lfo->lfo_filter_state.tfs_index.size())
1,582✔
244
        {
245
            auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[row];
791✔
246

247
            value_out.emplace_back(lr, SA_LEVEL.value(ll->get_msg_level()));
791✔
248
            if (lf->has_line_metadata() && this->tas_display_time_offset) {
791✔
249
                auto time_offset_end = 13;
1✔
250
                lr.lr_start = 0;
1✔
251
                lr.lr_end = time_offset_end;
1✔
252

253
                shift_string_attrs(value_out, 0, time_offset_end);
1✔
254

255
                value_out.emplace_back(lr,
1✔
256
                                       VC_ROLE.value(role_t::VCR_OFFSET_TIME));
2✔
257
                value_out.emplace_back(line_range(12, 13),
1✔
258
                                       VC_GRAPHIC.value(NCACS_VLINE));
2✔
259

260
                auto bar_role = role_t::VCR_NONE;
1✔
261

262
                switch (this->get_line_accel_direction(vis_line_t(row))) {
1✔
263
                    case log_accel::direction_t::A_STEADY:
1✔
264
                        break;
1✔
265
                    case log_accel::direction_t::A_DECEL:
×
266
                        bar_role = role_t::VCR_DIFF_DELETE;
×
267
                        break;
×
268
                    case log_accel::direction_t::A_ACCEL:
×
269
                        bar_role = role_t::VCR_DIFF_ADD;
×
270
                        break;
×
271
                }
272
                if (bar_role != role_t::VCR_NONE) {
1✔
273
                    value_out.emplace_back(line_range(12, 13),
×
274
                                           VC_ROLE.value(bar_role));
×
275
                }
276
            }
277

278
            if ((*curr_iter)->fvs_metadata.m_sections_root) {
791✔
279
                auto ll_next_iter = ll + 1;
782✔
280
                auto end_offset = (ll_next_iter == lf->end())
782✔
281
                    ? lf->get_index_size() - 1
1,507✔
282
                    : ll_next_iter->get_offset() - 1;
725✔
283
                const auto& meta = (*curr_iter)->fvs_metadata;
782✔
284
                meta.m_section_types_tree.visit_overlapping(
1,564✔
285
                    lf->get_line_content_offset(ll),
782✔
286
                    end_offset,
287
                    [&value_out, &ll, &lf, end_offset](const auto& iv) {
103✔
288
                        auto ll_offset = lf->get_line_content_offset(ll);
103✔
289
                        auto lr = line_range{0, -1};
103✔
290
                        if (iv.start > ll_offset) {
103✔
291
                            lr.lr_start = iv.start - ll_offset;
1✔
292
                        }
293
                        if (iv.stop < end_offset) {
103✔
294
                            lr.lr_end = iv.stop - ll_offset;
×
295
                        } else {
296
                            lr.lr_end = end_offset - ll_offset;
103✔
297
                        }
298
                        auto role = role_t::VCR_NONE;
103✔
299
                        switch (iv.value) {
103✔
300
                            case lnav::document::section_types_t::comment:
29✔
301
                                role = role_t::VCR_COMMENT;
29✔
302
                                break;
29✔
303
                            case lnav::document::section_types_t::
74✔
304
                                multiline_string:
305
                                role = role_t::VCR_STRING;
74✔
306
                                break;
74✔
307
                        }
308
                        value_out.emplace_back(lr, VC_ROLE.value(role));
103✔
309
                    });
103✔
310
                for (const auto& indent : meta.m_indents) {
2,235✔
311
                    if (indent < this->tss_line_indent_size) {
1,453✔
312
                        auto guide_lr = line_range{
313
                            (int) indent,
173✔
314
                            (int) (indent + 1),
173✔
315
                            line_range::unit::codepoint,
316
                        };
173✔
317
                        if (this->tas_display_time_offset) {
173✔
318
                            guide_lr.shift(0, 13);
×
319
                        }
320
                        value_out.emplace_back(
173✔
321
                            guide_lr,
322
                            VC_BLOCK_ELEM.value(block_elem_t{
346✔
323
                                L'\u258f', role_t::VCR_INDENT_GUIDE}));
324
                    }
325
                }
326
            }
327
        }
328
    }
329

330
    if ((this->tss_context_before > 0 || this->tss_context_after > 0)
2,211✔
331
        && this->tss_apply_filters
6✔
332
        && lf->get_text_format() != text_format_t::TF_BINARY)
4,434✔
333
    {
334
        auto* ctx_lfo
335
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
6✔
336
        if (ctx_lfo != nullptr && row >= 0
6✔
337
            && row < (ssize_t) ctx_lfo->lfo_filter_state.tfs_index.size())
12✔
338
        {
339
            uint32_t ctx_filter_in_mask, ctx_filter_out_mask;
340
            this->get_filters().get_enabled_mask(ctx_filter_in_mask,
6✔
341
                                                 ctx_filter_out_mask);
342
            auto content_line = ctx_lfo->lfo_filter_state.tfs_index[row];
6✔
343
            if (ctx_lfo->excluded(
6✔
344
                    ctx_filter_in_mask, ctx_filter_out_mask, content_line))
345
            {
346
                value_out.emplace_back(line_range{0, -1},
4✔
347
                                       VC_ROLE.value(role_t::VCR_CONTEXT_LINE));
8✔
348
            }
349
        }
350
    }
351

352
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
2,217✔
353
}
354

355
size_t
356
textfile_sub_source::text_size_for_line(textview_curses& tc,
×
357
                                        int line,
358
                                        text_sub_source::line_flags_t flags)
359
{
360
    size_t retval = 0;
×
361

362
    if (!this->tss_files.empty()) {
×
363
        const auto curr_iter = this->current_file_state();
×
364
        const auto& lf = (*curr_iter)->fvs_file;
×
365
        if (this->tss_view_mode == view_mode::raw
×
366
            || !(*curr_iter)->fvs_text_source)
×
367
        {
368
            auto* lfo = dynamic_cast<line_filter_observer*>(
×
369
                lf->get_logline_observer());
×
370
            if (lfo == nullptr || line < 0
×
371
                || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
×
372
            {
373
            } else {
374
                auto read_res = lf->read_line(
375
                    lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
×
376
                if (read_res.isOk()) {
×
377
                    auto sbr = read_res.unwrap();
×
378
                    auto str = to_string(sbr);
×
379
                    scrub_ansi_string(str, nullptr);
×
380
                    retval = string_fragment::from_str(str).column_width();
×
381
                }
382
            }
383
        } else {
384
            retval = (*curr_iter)
×
385
                         ->fvs_text_source->text_size_for_line(tc, line, flags);
×
386
        }
387
    }
388

389
    return retval;
×
390
}
391

392
void
393
textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
×
394
{
395
    const auto iter = std::find_if(
396
        this->tss_files.begin(), this->tss_files.end(), [&lf](const auto& fvs) {
×
397
            return fvs->fvs_file == lf;
×
398
        });
×
399
    if (iter == this->tss_files.end()) {
×
400
        return;
×
401
    }
402
    this->tss_files.front()->save_from(*this->tss_view);
×
403
    std::swap(*this->tss_files.begin(), *iter);
×
404
    this->set_time_offset(false);
×
405
    this->tss_files.front()->load_into(*this->tss_view);
×
406
    this->tss_view->reload_data();
×
407
}
408

409
void
410
textfile_sub_source::rotate_left()
×
411
{
412
    if (this->tss_files.size() > 1) {
×
413
        this->tss_files.emplace_back(std::move(this->tss_files.front()));
×
414
        this->tss_files.pop_front();
×
415
        this->tss_files.back()->save_from(*this->tss_view);
×
416
        this->tss_files.front()->load_into(*this->tss_view);
×
417
        this->set_time_offset(false);
×
418
        this->tss_view->reload_data();
×
419
        this->tss_view->redo_search();
×
420
        this->tss_view->set_needs_update();
×
421
    }
422
}
423

424
void
425
textfile_sub_source::rotate_right()
×
426
{
427
    if (this->tss_files.size() > 1) {
×
428
        this->tss_files.front()->save_from(*this->tss_view);
×
429
        auto fvs = std::move(this->tss_files.back());
×
430
        this->tss_files.emplace_front(std::move(fvs));
×
431
        this->tss_files.pop_back();
×
432
        this->tss_files.front()->load_into(*this->tss_view);
×
433
        this->set_time_offset(false);
×
434
        this->tss_view->reload_data();
×
435
        this->tss_view->redo_search();
×
436
        this->tss_view->set_needs_update();
×
437
    }
438
}
439

440
void
441
textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
695✔
442
{
443
    auto iter = std::find_if(
444
        this->tss_files.begin(), this->tss_files.end(), [&lf](const auto& fvs) {
695✔
445
            return fvs->fvs_file == lf;
×
446
        });
1,390✔
447
    if (iter != this->tss_files.end()) {
695✔
448
        this->tss_files.erase(iter);
×
449
        this->detach_observer(lf);
×
450
    }
451
    this->set_time_offset(false);
695✔
452
    if (!this->tss_files.empty()) {
695✔
453
        this->tss_files.front()->load_into(*this->tss_view);
×
454
    }
455
    this->tss_view->reload_data();
695✔
456
}
695✔
457

458
void
459
textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
822✔
460
{
461
    auto* lfo = new line_filter_observer(this->get_filters(), lf);
822✔
462
    lf->set_logline_observer(lfo);
822✔
463
    this->tss_files.emplace_back(std::make_shared<file_view_state>(lf));
822✔
464
}
822✔
465

466
void
467
textfile_sub_source::text_mark(const bookmark_type_t* bm,
15✔
468
                               vis_line_t line,
469
                               bool added)
470
{
471
    if (this->tss_files.empty()) {
15✔
472
        return;
×
473
    }
474

475
    auto& front = this->tss_files.front();
15✔
476
    auto lf = front->fvs_file;
15✔
477
    if (lf == nullptr) {
15✔
478
        return;
×
479
    }
480

481
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
15✔
482
    if (lfo == nullptr
15✔
483
        || line < 0_vl
15✔
484
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
30✔
485
    {
486
        return;
×
487
    }
488

489
    auto cl = lfo->lfo_filter_state.tfs_index[static_cast<int>(line)];
15✔
490
    front->fvs_content_marks[bm].apply(cl, added);
15✔
491
}
15✔
492

493
void
494
textfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
24✔
495
{
496
    if (this->tss_files.empty()) {
24✔
497
        return;
22✔
498
    }
499

500
    auto& front = this->tss_files.front();
2✔
501
    front->fvs_content_marks[bm].clear();
2✔
502
}
503

504
void
505
textfile_sub_source::text_update_marks(vis_bookmarks& bm)
970✔
506
{
507
    if (this->tss_files.empty()) {
970✔
508
        return;
834✔
509
    }
510

511
    auto& front = this->tss_files.front();
136✔
512
    auto lf = front->fvs_file;
136✔
513
    if (lf == nullptr) {
136✔
514
        return;
×
515
    }
516

517
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
136✔
518
    if (lfo == nullptr) {
136✔
519
        return;
×
520
    }
521

522
    static const bookmark_type_t* MARK_TYPES[] = {
523
        &textview_curses::BM_USER,
524
        &textview_curses::BM_STICKY,
525
    };
526

527
    for (const auto* bmt : MARK_TYPES) {
408✔
528
        bm[bmt].clear();
272✔
529
        if (front->fvs_content_marks[bmt].empty()) {
272✔
530
            continue;
257✔
531
        }
532

533
        auto& tfs = lfo->lfo_filter_state.tfs_index;
15✔
534
        for (uint32_t vl = 0; vl < tfs.size(); ++vl) {
180✔
535
            auto cl = tfs[vl];
165✔
536
            if (front->fvs_content_marks[bmt].bv_tree.count(cl) > 0) {
165✔
537
                bm[bmt].insert_once(vis_line_t(vl));
15✔
538
            }
539
        }
540
    }
541

542
    if (lf->has_line_metadata()) {
136✔
543
        auto& bm_errors = bm[&textview_curses::BM_ERRORS];
22✔
544
        auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
22✔
545

546
        bm_errors.clear();
22✔
547
        bm_warnings.clear();
22✔
548

549
        auto& tfs = lfo->lfo_filter_state.tfs_index;
22✔
550
        for (uint32_t vl = 0; vl < tfs.size(); ++vl) {
85✔
551
            auto ll = lf->begin() + tfs[vl];
63✔
552
            switch (ll->get_msg_level()) {
63✔
553
                case log_level_t::LEVEL_FATAL:
9✔
554
                case log_level_t::LEVEL_CRITICAL:
555
                case log_level_t::LEVEL_ERROR:
556
                    bm_errors.insert_once(vis_line_t(vl));
9✔
557
                    break;
9✔
558
                case log_level_t::LEVEL_WARNING:
×
559
                    bm_warnings.insert_once(vis_line_t(vl));
×
560
                    break;
×
561
                default:
54✔
562
                    break;
54✔
563
            }
564
        }
565
    }
566
}
136✔
567

568
void
569
textfile_sub_source::text_filters_changed()
19✔
570
{
571
    auto lf = this->current_file();
19✔
572
    if (lf == nullptr || lf->get_text_format() == text_format_t::TF_BINARY) {
19✔
573
        return;
11✔
574
    }
575

576
    auto* lfo = (line_filter_observer*) lf->get_logline_observer();
8✔
577
    uint32_t filter_in_mask, filter_out_mask;
578

579
    lfo->clear_deleted_filter_state();
8✔
580
    lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
8✔
581

582
    this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
8✔
583
    lfo->lfo_filter_state.tfs_index.clear();
8✔
584

585
    auto context_before = this->tss_context_before;
8✔
586
    auto context_after = this->tss_context_after;
8✔
587
    std::vector<uint32_t> before_buf;
8✔
588
    before_buf.reserve(context_before);
8✔
589
    size_t after_remaining = 0;
8✔
590

591
    auto flush_before_buf = [&]() {
22✔
592
        for (auto ctx_lpc : before_buf) {
25✔
593
            lfo->lfo_filter_state.tfs_index.push_back(ctx_lpc);
3✔
594
        }
595
        before_buf.clear();
22✔
596
    };
22✔
597

598
    for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
68✔
599
        if (this->tss_apply_filters) {
60✔
600
            auto dominated = lfo->excluded(filter_in_mask, filter_out_mask, lpc);
60✔
601
            if (!dominated && lf->has_line_metadata()) {
60✔
602
                auto ll = lf->begin() + lpc;
×
603
                if (ll->get_timeval() < this->ttt_min_row_time) {
×
604
                    dominated = true;
×
605
                }
606
                if (this->ttt_max_row_time < ll->get_timeval()) {
×
607
                    dominated = true;
×
608
                }
609
            }
610
            if (dominated) {
60✔
611
                if (after_remaining > 0) {
38✔
612
                    lfo->lfo_filter_state.tfs_index.push_back(lpc);
1✔
613
                    after_remaining -= 1;
1✔
614
                } else if (context_before > 0) {
37✔
615
                    if (before_buf.size() >= context_before) {
15✔
616
                        before_buf.erase(before_buf.begin());
9✔
617
                    }
618
                    before_buf.push_back(lpc);
15✔
619
                }
620
                continue;
38✔
621
            }
622
        }
623
        flush_before_buf();
22✔
624
        lfo->lfo_filter_state.tfs_index.push_back(lpc);
22✔
625
        after_remaining = context_after;
22✔
626
    }
627

628
    this->tss_view->reload_data();
8✔
629
    this->tss_view->redo_search();
8✔
630

631
    auto iter = std::lower_bound(lfo->lfo_filter_state.tfs_index.begin(),
8✔
632
                                 lfo->lfo_filter_state.tfs_index.end(),
8✔
633
                                 this->tss_content_line);
8✔
634
    auto vl = vis_line_t(
635
        std::distance(lfo->lfo_filter_state.tfs_index.begin(), iter));
16✔
636
    this->tss_view->set_selection(vl);
8✔
637
}
19✔
638

639
void
640
textfile_sub_source::scroll_invoked(textview_curses* tc)
1,018✔
641
{
642
    const auto curr_iter = this->current_file_state();
1,018✔
643
    if (curr_iter == this->tss_files.end()
1,018✔
644
        || (*curr_iter)->fvs_file->get_text_format()
1,386✔
645
            == text_format_t::TF_BINARY)
1,386✔
646
    {
647
        return;
876✔
648
    }
649

650
    const auto& lf = (*curr_iter)->fvs_file;
182✔
651
    if (this->tss_view_mode == view_mode::rendered
364✔
652
        && (*curr_iter)->fvs_text_source)
182✔
653
    {
654
        return;
40✔
655
    }
656

657
    auto line = tc->get_selection();
142✔
658
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
142✔
659
    if (!line || lfo == nullptr || line < 0_vl
284✔
660
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
284✔
661
    {
662
        return;
×
663
    }
664

665
    this->tss_content_line = lfo->lfo_filter_state.tfs_index[line.value()];
142✔
666
}
667

668
int
669
textfile_sub_source::get_filtered_count() const
436✔
670
{
671
    const auto curr_iter = this->current_file_state();
436✔
672
    int retval = 0;
436✔
673

674
    if (curr_iter != this->tss_files.end()) {
436✔
675
        if (this->tss_view_mode == view_mode::raw
864✔
676
            || !(*curr_iter)->fvs_text_source)
432✔
677
        {
678
            const auto& lf = (*curr_iter)->fvs_file;
341✔
679
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
341✔
680
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
341✔
681
        }
682
    }
683
    return retval;
436✔
684
}
685

686
int
687
textfile_sub_source::get_filtered_count_for(size_t filter_index) const
×
688
{
689
    std::shared_ptr<logfile> lf = this->current_file();
×
690

691
    if (lf == nullptr) {
×
692
        return 0;
×
693
    }
694

695
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
696
    return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
×
697
}
698

699
std::optional<text_format_t>
700
textfile_sub_source::get_text_format() const
1,679✔
701
{
702
    if (this->tss_files.empty()) {
1,679✔
703
        return text_format_t::TF_PLAINTEXT;
×
704
    }
705

706
    return this->tss_files.front()->fvs_file->get_text_format();
1,679✔
707
}
708

709
static attr_line_t
710
to_display(const std::shared_ptr<logfile>& lf)
22✔
711
{
712
    const auto& loo = lf->get_open_options();
22✔
713
    attr_line_t retval;
22✔
714

715
    if (loo.loo_piper) {
22✔
716
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
717
            retval.append("\u21bb "_list_glyph);
×
718
        }
719
    } else if (loo.loo_child_poller && loo.loo_child_poller->is_alive()) {
18✔
720
        retval.append("\u21bb "_list_glyph);
×
721
    }
722
    retval.append(lf->get_unique_path());
22✔
723

724
    return retval;
22✔
725
}
×
726

727
void
728
textfile_sub_source::text_crumbs_for_line(
11✔
729
    int line, std::vector<breadcrumb::crumb>& crumbs)
730
{
731
    text_sub_source::text_crumbs_for_line(line, crumbs);
11✔
732

733
    if (this->empty()) {
11✔
734
        return;
×
735
    }
736

737
    const auto curr_iter = this->current_file_state();
11✔
738
    const auto& lf = (*curr_iter)->fvs_file;
11✔
739
    crumbs.emplace_back(
11✔
740
        lf->get_unique_path(),
11✔
741
        to_display(lf),
11✔
742
        [this]() {
×
743
            return this->tss_files | lnav::itertools::map([](const auto& lf) {
22✔
744
                       return breadcrumb::possibility{
745
                           lf->fvs_file->get_path_for_key(),
11✔
746
                           to_display(lf->fvs_file),
11✔
747
                       };
22✔
748
                   });
22✔
749
        },
750
        [this](const auto& key) {
11✔
751
            auto lf_opt = this->tss_files
×
752
                | lnav::itertools::map(
×
753
                              [](const auto& x) { return x->fvs_file; })
×
754
                | lnav::itertools::find_if([&key](const auto& elem) {
×
755
                              return key.template get<std::string>()
756
                                  == elem->get_path_for_key();
×
757
                          })
758
                | lnav::itertools::deref();
×
759

760
            if (!lf_opt) {
×
761
                return;
×
762
            }
763

764
            this->to_front(lf_opt.value());
×
765
            this->tss_view->reload_data();
×
766
        });
×
767
    if (lf->size() == 0) {
11✔
768
        return;
×
769
    }
770

771
    if (lf->has_line_metadata()) {
11✔
772
        auto* lfo
773
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
774
        if (line < 0
2✔
775
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
2✔
776
        {
777
            return;
×
778
        }
779
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
2✔
780
        char ts[64];
781

782
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
783

784
        crumbs.emplace_back(
2✔
785
            std::string(ts),
4✔
786
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
787
            [](const auto& key) {});
2✔
788
    }
789

790
    if (this->tss_view_mode == view_mode::rendered
22✔
791
        && (*curr_iter)->fvs_text_source)
11✔
792
    {
793
        (*curr_iter)->fvs_text_source->text_crumbs_for_line(line, crumbs);
2✔
794
    } else if ((*curr_iter)->fvs_metadata.m_sections_tree.empty()) {
9✔
795
    } else {
796
        auto* lfo
797
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
6✔
798
        if (line < 0
6✔
799
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
6✔
800
        {
801
            return;
×
802
        }
803
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
6✔
804
        auto ll_next_iter = ll_iter + 1;
6✔
805
        auto end_offset = (ll_next_iter == lf->end())
6✔
806
            ? lf->get_index_size() - 1
12✔
807
            : ll_next_iter->get_offset() - 1;
6✔
808
        const auto initial_size = crumbs.size();
6✔
809

810
        (*curr_iter)
6✔
811
            ->fvs_metadata.m_sections_tree.visit_overlapping(
6✔
812
                lf->get_line_content_offset(ll_iter),
6✔
813
                end_offset,
814
                [&crumbs,
6✔
815
                 initial_size,
816
                 meta = &(*curr_iter)->fvs_metadata,
6✔
817
                 this,
818
                 lf](const auto& iv) {
819
                    auto path = crumbs | lnav::itertools::skip(initial_size)
24✔
820
                        | lnav::itertools::map(&breadcrumb::crumb::c_key)
16✔
821
                        | lnav::itertools::append(iv.value);
8✔
822
                    auto curr_node = lnav::document::hier_node::lookup_path(
8✔
823
                        meta->m_sections_root.get(), path);
8✔
824
                    crumbs.emplace_back(
16✔
825
                        iv.value,
8✔
826
                        [meta, path]() {
8✔
827
                            return meta->possibility_provider(path);
8✔
828
                        },
829
                        [this, curr_node, path, lf](const auto& key) {
16✔
830
                            if (!curr_node) {
×
831
                                return;
×
832
                            }
833
                            auto* parent_node = curr_node.value()->hn_parent;
×
834
                            if (parent_node == nullptr) {
×
835
                                return;
×
836
                            }
837
                            key.match(
×
838
                                [this, parent_node](const std::string& str) {
×
839
                                    auto sib_iter
840
                                        = parent_node->hn_named_children.find(
×
841
                                            str);
842
                                    if (sib_iter
×
843
                                        == parent_node->hn_named_children
×
844
                                               .end()) {
×
845
                                        return;
×
846
                                    }
847
                                    this->set_top_from_off(
×
848
                                        sib_iter->second->hn_start);
×
849
                                },
850
                                [this, parent_node](size_t index) {
×
851
                                    if (index
×
852
                                        >= parent_node->hn_children.size()) {
×
853
                                        return;
×
854
                                    }
855
                                    auto sib
856
                                        = parent_node->hn_children[index].get();
×
857
                                    this->set_top_from_off(sib->hn_start);
×
858
                                });
859
                        });
860
                    if (curr_node
8✔
861
                        && curr_node.value()->hn_parent->hn_children.size()
16✔
862
                            != curr_node.value()
8✔
863
                                   ->hn_parent->hn_named_children.size())
8✔
864
                    {
865
                        auto node = lnav::document::hier_node::lookup_path(
1✔
866
                            meta->m_sections_root.get(), path);
1✔
867

868
                        crumbs.back().c_expected_input
1✔
869
                            = curr_node.value()
2✔
870
                                  ->hn_parent->hn_named_children.empty()
1✔
871
                            ? breadcrumb::crumb::expected_input_t::index
1✔
872
                            : breadcrumb::crumb::expected_input_t::
873
                                  index_or_exact;
874
                        crumbs.back().with_possible_range(
2✔
875
                            node | lnav::itertools::map([](const auto hn) {
1✔
876
                                return hn->hn_parent->hn_children.size();
1✔
877
                            })
878
                            | lnav::itertools::unwrap_or(size_t{0}));
2✔
879
                    }
880
                });
8✔
881

882
        auto path = crumbs | lnav::itertools::skip(initial_size)
12✔
883
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
12✔
884
        auto node = lnav::document::hier_node::lookup_path(
6✔
885
            (*curr_iter)->fvs_metadata.m_sections_root.get(), path);
6✔
886

887
        if (node && !node.value()->hn_children.empty()) {
6✔
888
            auto poss_provider = [curr_node = node.value()]() {
2✔
889
                std::vector<breadcrumb::possibility> retval;
2✔
890
                for (const auto& child : curr_node->hn_named_children) {
14✔
891
                    retval.emplace_back(child.first);
12✔
892
                }
893
                return retval;
2✔
894
            };
895
            auto path_performer = [this, curr_node = node.value()](
4✔
896
                                      const breadcrumb::crumb::key_t& value) {
897
                value.match(
×
898
                    [this, curr_node](const std::string& str) {
×
899
                        auto child_iter
900
                            = curr_node->hn_named_children.find(str);
×
901
                        if (child_iter != curr_node->hn_named_children.end()) {
×
902
                            this->set_top_from_off(
×
903
                                child_iter->second->hn_start);
×
904
                        }
905
                    },
×
906
                    [this, curr_node](size_t index) {
×
907
                        if (index >= curr_node->hn_children.size()) {
×
908
                            return;
×
909
                        }
910
                        auto* child = curr_node->hn_children[index].get();
×
911
                        this->set_top_from_off(child->hn_start);
×
912
                    });
913
            };
2✔
914
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
2✔
915
            crumbs.back().c_expected_input
2✔
916
                = node.value()->hn_named_children.empty()
4✔
917
                ? breadcrumb::crumb::expected_input_t::index
2✔
918
                : breadcrumb::crumb::expected_input_t::index_or_exact;
919
        }
920
    }
6✔
921
}
922

923
textfile_sub_source::rescan_result_t
924
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
5,680✔
925
                                  std::optional<ui_clock::time_point> deadline)
926
{
927
    static auto& lnav_db = injector::get<auto_sqlite3&>();
5,680✔
928

929
    file_iterator iter;
5,680✔
930
    rescan_result_t retval;
5,680✔
931
    size_t files_scanned = 0;
5,680✔
932

933
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
5,680✔
934
        return retval;
128✔
935
    }
936

937
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
5,552✔
938

939
    std::vector<std::shared_ptr<logfile>> closed_files;
5,552✔
940
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
7,078✔
941
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,526✔
942
        {
943
            log_info("rescan_files() deadline reached, breaking...");
×
944
            retval.rr_scan_completed = false;
×
945
            this->tss_last_scan_aborted = true;
×
946
            break;
×
947
        }
948

949
        std::shared_ptr<logfile> lf = (*iter)->fvs_file;
1,526✔
950

951
        if (lf->is_closed()) {
1,526✔
952
            iter = this->tss_files.erase(iter);
127✔
953
            this->detach_observer(lf);
127✔
954
            closed_files.emplace_back(lf);
127✔
955
            retval.rr_rescan_needed = true;
127✔
956
            continue;
127✔
957
        }
958

959
        if (last_aborted && lf->size() > 0) {
1,399✔
960
            retval.rr_scan_completed = false;
×
961
            ++iter;
×
962
            continue;
×
963
        }
964
        files_scanned += 1;
1,399✔
965

966
        try {
967
            const auto& st = lf->get_stat();
1,399✔
968
            uint32_t old_size = lf->size();
1,399✔
969
            auto new_text_data = lf->rebuild_index(deadline);
1,399✔
970

971
            if (lf->get_format() != nullptr) {
1,399✔
972
                iter = this->tss_files.erase(iter);
695✔
973
                this->detach_observer(lf);
695✔
974
                callback.promote_file(lf);
695✔
975
                continue;
800✔
976
            }
977

978
            bool new_data = false;
704✔
979
            switch (new_text_data) {
704✔
980
                case logfile::rebuild_result_t::NEW_LINES:
114✔
981
                case logfile::rebuild_result_t::NEW_ORDER:
982
                    new_data = true;
114✔
983
                    retval.rr_new_data += 1;
114✔
984
                    break;
114✔
985
                case logfile::rebuild_result_t::NO_NEW_LINES:
590✔
986
                    this->move_to_init_location(iter);
590✔
987
                    break;
590✔
988
                default:
×
989
                    break;
×
990
            }
991
            callback.scanned_file(lf);
704✔
992

993
            if (lf->is_indexing()
704✔
994
                && lf->get_text_format() != text_format_t::TF_BINARY)
704✔
995
            {
996
                if (!new_data) {
697✔
997
                    // Only invalidate the meta if the file is small, or we
998
                    // found some meta previously.
999
                    if ((st.st_mtime != (*iter)->fvs_mtime
585✔
1000
                         || st.st_size != (*iter)->fvs_file_size
572✔
1001
                         || lf->get_index_size()
572✔
1002
                             != (*iter)->fvs_file_indexed_size)
572✔
1003
                        && (st.st_size < 10 * 1024
1,157✔
1004
                            || (*iter)->fvs_file_size == 0
×
1005
                            || !(*iter)->fvs_metadata.m_sections_tree.empty()))
×
1006
                    {
1007
                        log_debug(
13✔
1008
                            "text file has changed, invalidating metadata.  "
1009
                            "old: {mtime: %ld size: %lld isize: %lld}, new: "
1010
                            "{mtime: %ld size: %lld isize: %lld}",
1011
                            (*iter)->fvs_mtime,
1012
                            (*iter)->fvs_file_size,
1013
                            (*iter)->fvs_file_indexed_size,
1014
                            st.st_mtime,
1015
                            st.st_size,
1016
                            lf->get_index_size());
1017
                        (*iter)->fvs_metadata = {};
13✔
1018
                        (*iter)->fvs_error.clear();
13✔
1019
                    }
1020
                }
1021

1022
                if (!(*iter)->fvs_metadata.m_sections_root
697✔
1023
                    && (*iter)->fvs_error.empty())
697✔
1024
                {
1025
                    auto read_res
1026
                        = lf->read_file(logfile::read_format_t::with_framing);
124✔
1027

1028
                    if (read_res.isOk()) {
124✔
1029
                        auto read_file_res = read_res.unwrap();
124✔
1030
                        auto tf_opt = lf->get_text_format();
124✔
1031

1032
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf
248✔
1033
                            || !tf_opt)
124✔
1034
                        {
1035
                            log_error(
18✔
1036
                                "%s: file has no text format, skipping meta "
1037
                                "discovery",
1038
                                lf->get_path_for_key().c_str());
1039
                            (*iter)->fvs_mtime = st.st_mtime;
18✔
1040
                            (*iter)->fvs_file_size = st.st_size;
18✔
1041
                            (*iter)->fvs_file_indexed_size
18✔
1042
                                = lf->get_index_size();
18✔
1043
                            (*iter)->fvs_error = "skipping meta discovery";
18✔
1044
                        } else {
1045
                            auto content
1046
                                = attr_line_t(read_file_res.rfr_content);
106✔
1047

1048
                            log_info("generating metadata for: %s (size=%zu)",
106✔
1049
                                     lf->get_path_for_key().c_str(),
1050
                                     content.length());
1051
                            scrub_ansi_string(content.get_string(),
106✔
1052
                                              &content.get_attrs());
106✔
1053

1054
                            auto text_meta = extract_text_meta(
1055
                                content.get_string(), tf_opt.value());
106✔
1056
                            if (text_meta) {
106✔
1057
                                lf->set_filename(text_meta->tfm_filename);
3✔
1058
                                lf->set_include_in_session(true);
3✔
1059
                                callback.renamed_file(lf);
3✔
1060
                            }
1061

1062
                            (*iter)->fvs_mtime = st.st_mtime;
106✔
1063
                            (*iter)->fvs_file_size = st.st_size;
106✔
1064
                            (*iter)->fvs_file_indexed_size
106✔
1065
                                = lf->get_index_size();
106✔
1066
                            (*iter)->fvs_metadata
106✔
1067
                                = lnav::document::discover(content)
106✔
1068
                                      .with_text_format(tf_opt.value())
106✔
1069
                                      .perform();
212✔
1070
                            log_info("  metadata indents size: %zu",
106✔
1071
                                     (*iter)->fvs_metadata.m_indents.size());
1072
                        }
106✔
1073
                    } else {
124✔
1074
                        auto errmsg = read_res.unwrapErr();
×
1075
                        log_error(
×
1076
                            "%s: unable to read file for meta discover -- %s",
1077
                            lf->get_path_for_key().c_str(),
1078
                            errmsg.c_str());
1079
                        (*iter)->fvs_mtime = st.st_mtime;
×
1080
                        (*iter)->fvs_file_size = st.st_size;
×
1081
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1082
                        (*iter)->fvs_error = errmsg;
×
1083
                    }
1084
                }
124✔
1085
            }
1086

1087
            uint32_t filter_in_mask, filter_out_mask;
1088

1089
            this->get_filters().get_enabled_mask(filter_in_mask,
704✔
1090
                                                 filter_out_mask);
1091
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
704✔
1092
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
5,115✔
1093
                if (this->tss_apply_filters
8,822✔
1094
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
4,411✔
1095
                {
1096
                    continue;
×
1097
                }
1098
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
4,411✔
1099
            }
1100

1101
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
704✔
1102
                if ((*iter)->fvs_text_source) {
115✔
1103
                    if ((*iter)->fvs_file_size == st.st_size
95✔
1104
                        && (*iter)->fvs_file_indexed_size
190✔
1105
                            == lf->get_index_size()
95✔
1106
                        && (*iter)->fvs_mtime == st.st_mtime)
190✔
1107
                    {
1108
                        ++iter;
94✔
1109
                        continue;
94✔
1110
                    }
1111
                    log_info("markdown file has been updated, re-rendering: %s",
1✔
1112
                             lf->get_path_for_key().c_str());
1113
                    (*iter)->fvs_text_source = nullptr;
1✔
1114
                }
1115

1116
                auto read_res = lf->read_file(logfile::read_format_t::plain);
21✔
1117
                if (read_res.isOk()) {
21✔
1118
                    auto read_file_res = read_res.unwrap();
21✔
1119
                    auto md_file = md4cpp::parse_file(
21✔
1120
                        lf->get_filename(), read_file_res.rfr_content);
1121
                    log_info("%s: rendering markdown content of size %zu",
21✔
1122
                             lf->get_basename().c_str(),
1123
                             read_file_res.rfr_content.size());
1124
                    md2attr_line mdal;
21✔
1125

1126
                    mdal.with_source_path(lf->get_actual_path());
21✔
1127
                    if (this->tss_view->tc_interactive) {
21✔
1128
                        mdal.add_lnav_script_icons();
×
1129
                    }
1130
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
21✔
1131

1132
                    (*iter)->fvs_mtime = st.st_mtime;
21✔
1133
                    (*iter)->fvs_file_indexed_size = lf->get_index_size();
21✔
1134
                    (*iter)->fvs_file_size = st.st_size;
21✔
1135
                    (*iter)->fvs_text_source
21✔
1136
                        = std::make_unique<plain_text_source>();
42✔
1137
                    (*iter)->fvs_text_source->set_text_format(
21✔
1138
                        lf->get_text_format());
1139
                    if (parse_res.isOk()) {
21✔
1140
                        auto& lf_meta = lf->get_embedded_metadata();
19✔
1141

1142
                        (*iter)->fvs_text_source->replace_with(
38✔
1143
                            parse_res.unwrap());
38✔
1144
                        if (!md_file.f_frontmatter.empty()) {
19✔
1145
                            lf_meta["net.daringfireball.markdown.frontmatter"]
10✔
1146
                                = {
1147
                                    md_file.f_frontmatter_format,
5✔
1148
                                    md_file.f_frontmatter.to_string(),
1149
                                };
10✔
1150
                        }
1151

1152
                        lnav::events::publish(
19✔
1153
                            lnav_db,
1154
                            lnav::events::file::format_detected{
57✔
1155
                                lf->get_filename(),
19✔
1156
                                fmt::to_string(lf->get_text_format().value_or(
38✔
1157
                                    text_format_t::TF_BINARY)),
38✔
1158
                            });
1159
                    } else {
1160
                        auto view_content
1161
                            = lnav::console::user_message::error(
4✔
1162
                                  "unable to parse markdown file")
1163
                                  .with_reason(parse_res.unwrapErr())
4✔
1164
                                  .to_attr_line();
2✔
1165
                        view_content.append("\n").append(
4✔
1166
                            attr_line_t::from_ansi_str(
4✔
1167
                                read_file_res.rfr_content.c_str()));
1168

1169
                        (*iter)->fvs_text_source->replace_with(view_content);
2✔
1170
                    }
2✔
1171
                    (*iter)->fvs_text_source->register_view(this->tss_view);
21✔
1172
                } else {
21✔
1173
                    log_error("unable to read markdown file: %s -- %s",
×
1174
                              lf->get_path_for_key().c_str(),
1175
                              read_res.unwrapErr().c_str());
1176
                }
1177
            } else if (file_needs_reformatting(lf) && !new_data) {
610✔
1178
                if ((*iter)->fvs_file_size == st.st_size
15✔
1179
                    && (*iter)->fvs_file_indexed_size == lf->get_index_size()
15✔
1180
                    && (*iter)->fvs_mtime == st.st_mtime
15✔
1181
                    && (!(*iter)->fvs_error.empty()
45✔
1182
                        || (*iter)->fvs_text_source != nullptr))
15✔
1183
                {
1184
                    ++iter;
11✔
1185
                    continue;
11✔
1186
                }
1187
                log_info("pretty file has been updated, re-rendering: %s",
4✔
1188
                         lf->get_path_for_key().c_str());
1189
                (*iter)->fvs_text_source = nullptr;
4✔
1190
                (*iter)->fvs_error.clear();
4✔
1191

1192
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
1193
                if (read_res.isOk()) {
4✔
1194
                    auto read_file_res = read_res.unwrap();
4✔
1195
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
1196
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
1197
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
1198
                        data_scanner ds(orig_al.al_string);
4✔
1199
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
1200
                        attr_line_t pretty_al;
4✔
1201

1202
                        pp.append_to(pretty_al);
4✔
1203
                        (*iter)->fvs_mtime = st.st_mtime;
4✔
1204
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
4✔
1205
                        (*iter)->fvs_file_size = st.st_size;
4✔
1206
                        (*iter)->fvs_text_source
4✔
1207
                            = std::make_unique<plain_text_source>();
8✔
1208
                        (*iter)->fvs_text_source->set_text_format(
4✔
1209
                            lf->get_text_format());
1210
                        (*iter)->fvs_text_source->register_view(this->tss_view);
4✔
1211
                        (*iter)->fvs_text_source->replace_with_mutable(
4✔
1212
                            pretty_al, lf->get_text_format());
1213
                    } else {
4✔
1214
                        log_error(
×
1215
                            "unable to read file to pretty-print: %s -- file "
1216
                            "is not valid UTF-8",
1217
                            lf->get_path_for_key().c_str());
1218
                        (*iter)->fvs_mtime = st.st_mtime;
×
1219
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1220
                        (*iter)->fvs_file_size = st.st_size;
×
1221
                        (*iter)->fvs_error = "file is not valid UTF-8";
×
1222
                    }
1223
                } else {
4✔
1224
                    auto errmsg = read_res.unwrapErr();
×
1225
                    log_error("unable to read file to pretty-print: %s -- %s",
×
1226
                              lf->get_path_for_key().c_str(),
1227
                              errmsg.c_str());
1228
                    (*iter)->fvs_mtime = st.st_mtime;
×
1229
                    (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1230
                    (*iter)->fvs_file_size = st.st_size;
×
1231
                    (*iter)->fvs_error = errmsg;
×
1232
                }
1233
            }
4✔
1234
        } catch (const line_buffer::error& e) {
×
1235
            iter = this->tss_files.erase(iter);
×
1236
            lf->close();
×
1237
            this->detach_observer(lf);
×
1238
            closed_files.emplace_back(lf);
×
1239
            continue;
×
1240
        }
×
1241

1242
        ++iter;
599✔
1243
    }
1,526✔
1244
    if (!closed_files.empty()) {
5,552✔
1245
        callback.closed_files(closed_files);
123✔
1246
        if (!this->tss_files.empty()) {
123✔
1247
            this->tss_files.front()->load_into(*this->tss_view);
×
1248
        }
1249
        this->tss_view->set_needs_update();
123✔
1250
    }
1251

1252
    if (retval.rr_new_data) {
5,552✔
1253
        this->tss_view->search_new_data();
110✔
1254
    }
1255

1256
    return retval;
5,552✔
1257
}
5,570✔
1258

1259
void
1260
textfile_sub_source::set_top_from_off(file_off_t off)
×
1261
{
1262
    auto lf = this->current_file();
×
1263

1264
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1265
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1266
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1267
            std::distance(lf->cbegin(), new_top_iter));
×
1268

1269
        if (new_top_opt) {
×
1270
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1271
            if (this->tss_view->is_selectable()) {
×
1272
                this->tss_view->set_top(
×
1273
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1274
            }
1275
        }
1276
    };
1277
}
1278

1279
void
1280
textfile_sub_source::quiesce()
×
1281
{
1282
    for (auto& lf : this->tss_files) {
×
1283
        lf->fvs_file->quiesce();
×
1284
    }
1285
}
1286

1287
size_t
1288
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
24,538✔
1289
{
1290
    size_t retval = 0;
24,538✔
1291

1292
    if (mode == view_mode::raw || !this->fvs_text_source) {
24,538✔
1293
        const auto& lf = this->fvs_file;
13,237✔
1294
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
13,237✔
1295
            const auto fsize = lf->get_content_size();
47✔
1296
            retval = fsize / 16;
47✔
1297
            if (fsize % 16) {
47✔
1298
                retval += 1;
47✔
1299
            }
1300
        } else {
1301
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
13,190✔
1302
            if (lfo != nullptr) {
13,190✔
1303
                retval = lfo->lfo_filter_state.tfs_index.size();
13,190✔
1304
            }
1305
        }
1306
    } else {
1307
        retval = this->fvs_text_source->text_line_count();
11,301✔
1308
    }
1309

1310
    return retval;
24,538✔
1311
}
1312

1313
size_t
1314
textfile_sub_source::file_view_state::text_line_width(view_mode mode,
662✔
1315
                                                      textview_curses& tc) const
1316
{
1317
    size_t retval = 0;
662✔
1318
    if (mode == view_mode::raw || !this->fvs_text_source) {
662✔
1319
        const auto& lf = this->fvs_file;
505✔
1320
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
505✔
1321
            retval = 88;
5✔
1322
        } else {
1323
            retval = lf->get_longest_line_length();
500✔
1324
        }
1325
    } else {
1326
        retval = this->fvs_text_source->text_line_width(tc);
157✔
1327
    }
1328
    return retval;
662✔
1329
}
1330

1331
std::optional<vis_line_t>
1332
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
13✔
1333
                                                     const std::string& id)
1334
{
1335
    if (mode == view_mode::rendered && this->fvs_text_source) {
13✔
1336
        return this->fvs_text_source->row_for_anchor(id);
11✔
1337
    }
1338

1339
    if (!this->fvs_metadata.m_sections_root) {
2✔
1340
        return std::nullopt;
×
1341
    }
1342

1343
    const auto& lf = this->fvs_file;
2✔
1344
    const auto& meta = this->fvs_metadata;
2✔
1345
    std::optional<vis_line_t> retval;
2✔
1346

1347
    auto is_ptr = startswith(id, "#/");
2✔
1348
    if (is_ptr) {
2✔
1349
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1350
        std::vector<lnav::document::section_key_t> path;
2✔
1351

1352
        while (!hier_sf.empty()) {
8✔
1353
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
6✔
1354
            auto scan_res
1355
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
6✔
1356
            if (scan_res && scan_res->range().empty()) {
6✔
1357
                path.emplace_back(scan_res->value());
2✔
1358
            } else {
1359
                stack_buf allocator;
4✔
1360
                path.emplace_back(
4✔
1361
                    json_ptr::decode(comp_pair.first, allocator).to_string());
8✔
1362
            }
4✔
1363
            hier_sf = comp_pair.second;
6✔
1364
        }
1365

1366
        auto lookup_res = lnav::document::hier_node::lookup_path(
2✔
1367
            meta.m_sections_root.get(), path);
2✔
1368
        if (lookup_res) {
2✔
1369
            auto ll_opt = lf->line_for_offset(lookup_res.value()->hn_start);
2✔
1370
            if (ll_opt != lf->end()) {
2✔
1371
                retval
1372
                    = vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1373
            }
1374
        }
1375

1376
        return retval;
2✔
1377
    }
2✔
1378

1379
    lnav::document::hier_node::depth_first(
×
1380
        meta.m_sections_root.get(),
1381
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1382
            for (const auto& child_pair : node->hn_named_children) {
×
1383
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1384

1385
                if (child_anchor != id) {
×
1386
                    continue;
×
1387
                }
1388

1389
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1390
                if (ll_opt != lf->end()) {
×
1391
                    retval = vis_line_t(
×
1392
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1393
                }
1394
                break;
×
1395
            }
1396
        });
×
1397

1398
    return retval;
×
1399
}
1400

1401
std::optional<vis_line_t>
1402
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1403
{
1404
    const auto curr_iter = this->current_file_state();
6✔
1405
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
1406
        return std::nullopt;
×
1407
    }
1408

1409
    return (*curr_iter)->row_for_anchor(this->tss_view_mode, id);
6✔
1410
}
1411

1412
static void
1413
anchor_generator(std::unordered_set<std::string>& retval,
×
1414
                 std::vector<std::string>& comps,
1415
                 size_t& max_depth,
1416
                 lnav::document::hier_node* hn)
1417
{
1418
    if (hn->hn_named_children.empty()) {
×
1419
        if (hn->hn_children.empty()) {
×
1420
            if (retval.size() >= 250 || comps.empty()) {
×
1421
            } else if (comps.size() == 1) {
×
1422
                retval.emplace(text_anchors::to_anchor_string(comps.front()));
×
1423
            } else {
1424
                retval.emplace(
×
1425
                    fmt::format(FMT_STRING("#/{}"),
×
1426
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1427
            }
1428
            max_depth = std::max(max_depth, comps.size());
×
1429
        } else {
1430
            int index = 0;
×
1431
            for (const auto& child : hn->hn_children) {
×
1432
                comps.emplace_back(fmt::to_string(index));
×
1433
                anchor_generator(retval, comps, max_depth, child.get());
×
1434
                comps.pop_back();
×
1435
            }
1436
        }
1437
    } else {
1438
        for (const auto& [child_name, child_node] : hn->hn_named_children) {
×
1439
            comps.emplace_back(child_name);
×
1440
            anchor_generator(retval, comps, max_depth, child_node);
×
1441
            comps.pop_back();
×
1442
        }
1443
        if (max_depth > 1) {
×
1444
            retval.emplace(
×
1445
                fmt::format(FMT_STRING("#/{}"),
×
1446
                            fmt::join(comps.begin(), comps.end(), "/")));
×
1447
        }
1448
    }
1449
}
1450

1451
std::unordered_set<std::string>
1452
textfile_sub_source::get_anchors()
×
1453
{
1454
    std::unordered_set<std::string> retval;
×
1455

1456
    const auto curr_iter = this->current_file_state();
×
1457
    if (curr_iter == this->tss_files.end()) {
×
1458
        return retval;
×
1459
    }
1460

1461
    if (this->tss_view_mode == view_mode::rendered
×
1462
        && (*curr_iter)->fvs_text_source)
×
1463
    {
1464
        return (*curr_iter)->fvs_text_source->get_anchors();
×
1465
    }
1466

1467
    const auto& meta = (*curr_iter)->fvs_metadata;
×
1468
    if (meta.m_sections_root == nullptr) {
×
1469
        return retval;
×
1470
    }
1471

1472
    std::vector<std::string> comps;
×
1473
    size_t max_depth = 0;
×
1474
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1475

1476
    return retval;
×
1477
}
1478

1479
struct tfs_time_cmp {
1480
    bool operator()(int32_t lhs, const timeval& rhs) const
×
1481
    {
1482
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
×
1483
        return ll->get_timeval() < rhs;
×
1484
    }
1485

1486
    logfile* ttc_logfile;
1487
    std::vector<uint32_t>& ttc_index;
1488
};
1489

1490
std::optional<vis_line_t>
1491
textfile_sub_source::row_for_time(timeval time_bucket)
×
1492
{
1493
    auto lf = this->current_file();
×
1494
    if (!lf || !lf->has_line_metadata()) {
×
1495
        return std::nullopt;
×
1496
    }
1497

1498
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
1499
    auto& tfs = lfo->lfo_filter_state.tfs_index;
×
1500
    auto lb = std::lower_bound(
×
1501
        tfs.begin(), tfs.end(), time_bucket, tfs_time_cmp{lf.get(), tfs});
×
1502
    if (lb != tfs.end()) {
×
1503
        return vis_line_t{(int) std::distance(tfs.begin(), lb)};
×
1504
    }
1505

1506
    return std::nullopt;
×
1507
}
1508

1509
std::optional<text_time_translator::row_info>
1510
textfile_sub_source::time_for_row(vis_line_t row)
5,650✔
1511
{
1512
    auto lf = this->current_file();
5,650✔
1513
    if (!lf || !lf->has_line_metadata()) {
5,650✔
1514
        return std::nullopt;
5,500✔
1515
    }
1516

1517
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
150✔
1518
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
150✔
1519
        return std::nullopt;
43✔
1520
    }
1521
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
107✔
1522
    auto ll_iter = lf->begin() + row_id;
107✔
1523
    return row_info{
214✔
1524
        ll_iter->get_timeval(),
1525
        row_id,
1526
    };
107✔
1527
}
5,650✔
1528

1529
static std::optional<vis_line_t>
1530
to_vis_line(const std::shared_ptr<logfile>& lf, file_off_t off)
3✔
1531
{
1532
    auto ll_opt = lf->line_for_offset(off);
3✔
1533
    if (ll_opt != lf->end()) {
3✔
1534
        return vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
6✔
1535
    }
1536

1537
    return std::nullopt;
×
1538
}
1539

1540
std::optional<vis_line_t>
1541
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
3✔
1542
{
1543
    const auto curr_iter = this->current_file_state();
3✔
1544
    if (curr_iter == this->tss_files.end()) {
3✔
1545
        return std::nullopt;
×
1546
    }
1547

1548
    const auto& lf = (*curr_iter)->fvs_file;
3✔
1549
    log_debug("adjacent_anchor: %s:L%d:%s",
3✔
1550
              lf->get_path_for_key().c_str(),
1551
              (int) vl,
1552
              dir == text_anchors::direction::prev ? "prev" : "next");
1553
    if (this->tss_view_mode == view_mode::rendered
6✔
1554
        && (*curr_iter)->fvs_text_source)
3✔
1555
    {
1556
        return (*curr_iter)->fvs_text_source->adjacent_anchor(vl, dir);
×
1557
    }
1558

1559
    if (!(*curr_iter)->fvs_metadata.m_sections_root) {
3✔
1560
        log_debug("  no metadata available");
×
1561
        return std::nullopt;
×
1562
    }
1563

1564
    auto& md = (*curr_iter)->fvs_metadata;
3✔
1565
    const auto* lfo
1566
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1567
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
3✔
1568
        || md.m_sections_root == nullptr)
3✔
1569
    {
1570
        return std::nullopt;
×
1571
    }
1572
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
3✔
1573
    const auto line_offsets = lf->get_file_range(ll_iter, false);
3✔
1574
    log_debug(
3✔
1575
        "  range %lld:%zu", line_offsets.fr_offset, line_offsets.next_offset());
1576
    auto path_for_line
1577
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
3✔
1578

1579
    if (path_for_line.empty()) {
3✔
1580
        log_debug("  no path found");
×
1581
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
1582
        if (!neighbors_res) {
×
1583
            return std::nullopt;
×
1584
        }
1585

1586
        switch (dir) {
×
1587
            case direction::prev: {
×
1588
                if (neighbors_res->cnr_previous) {
×
1589
                    return to_vis_line(
×
1590
                        lf, neighbors_res->cnr_previous.value()->hn_start);
×
1591
                }
1592
                break;
×
1593
            }
1594
            case direction::next: {
×
1595
                if (neighbors_res->cnr_next) {
×
1596
                    return to_vis_line(
×
1597
                        lf, neighbors_res->cnr_next.value()->hn_start);
×
1598
                }
1599
                if (!md.m_sections_root->hn_children.empty()) {
×
1600
                    return to_vis_line(
×
1601
                        lf, md.m_sections_root->hn_children[0]->hn_start);
×
1602
                }
1603
                break;
×
1604
            }
1605
        }
1606
        return std::nullopt;
×
1607
    }
1608

1609
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
3✔
1610
    const auto last_key = std::move(path_for_line.back());
3✔
1611
    path_for_line.pop_back();
3✔
1612

1613
    const auto parent_opt = lnav::document::hier_node::lookup_path(
3✔
1614
        md.m_sections_root.get(), path_for_line);
3✔
1615
    if (!parent_opt) {
3✔
1616
        log_debug("  no parent for path: %s",
×
1617
                  fmt::to_string(path_for_line).c_str());
1618
        return std::nullopt;
×
1619
    }
1620
    const auto parent = parent_opt.value();
3✔
1621

1622
    const auto child_hn = parent->lookup_child(last_key);
3✔
1623
    if (!child_hn) {
3✔
1624
        // XXX "should not happen"
1625
        log_debug("  child not found");
×
1626
        return std::nullopt;
×
1627
    }
1628

1629
    auto neighbors_res = parent->child_neighbors(
9✔
1630
        child_hn.value(), line_offsets.next_offset() + 1);
3✔
1631
    if (!neighbors_res) {
3✔
1632
        log_debug("  no neighbors found");
×
1633
        return std::nullopt;
×
1634
    }
1635

1636
    log_debug("  neighbors p:%d n:%d",
3✔
1637
              neighbors_res->cnr_previous.has_value(),
1638
              neighbors_res->cnr_next.has_value());
1639
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
3✔
1640
        auto neighbor_sub
1641
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
3✔
1642
        if (neighbor_sub) {
3✔
1643
            neighbors_res->cnr_previous = neighbor_sub;
×
1644
        }
1645
    }
1646

1647
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
3✔
1648
        auto neighbor_sub
1649
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
3✔
1650
        if (neighbor_sub) {
3✔
1651
            neighbors_res->cnr_next = neighbor_sub;
3✔
1652
        }
1653
    }
1654

1655
    switch (dir) {
3✔
1656
        case direction::prev: {
1✔
1657
            if (neighbors_res->cnr_previous) {
1✔
1658
                return to_vis_line(
1✔
1659
                    lf, neighbors_res->cnr_previous.value()->hn_start);
1✔
1660
            }
1661
            break;
×
1662
        }
1663
        case direction::next: {
2✔
1664
            if (neighbors_res->cnr_next) {
2✔
1665
                return to_vis_line(lf,
2✔
1666
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1667
            }
1668
            break;
×
1669
        }
1670
    }
1671

1672
    return std::nullopt;
×
1673
}
3✔
1674

1675
std::optional<std::string>
1676
textfile_sub_source::file_view_state::anchor_for_row(view_mode mode,
18✔
1677
                                                    vis_line_t vl)
1678
{
1679
    if (mode == view_mode::rendered && this->fvs_text_source) {
18✔
1680
        return this->fvs_text_source->anchor_for_row(vl);
6✔
1681
    }
1682

1683
    if (!this->fvs_metadata.m_sections_root) {
12✔
1684
        return std::nullopt;
×
1685
    }
1686

1687
    const auto& lf = this->fvs_file;
12✔
1688
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
12✔
1689
    if (lfo == nullptr
12✔
1690
        || vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
12✔
1691
    {
1692
        return std::nullopt;
×
1693
    }
1694
    auto& md = this->fvs_metadata;
12✔
1695
    auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
12✔
1696
    auto line_offsets = lf->get_file_range(ll_iter, false);
12✔
1697
    auto path_for_line
1698
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
12✔
1699

1700
    if (path_for_line.empty()) {
12✔
1701
        return std::nullopt;
8✔
1702
    }
1703

1704
    if ((path_for_line.size() == 1
4✔
1705
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1706
        && path_for_line.back().is<std::string>())
6✔
1707
    {
1708
        return text_anchors::to_anchor_string(
2✔
1709
            path_for_line.back().get<std::string>());
2✔
1710
    }
1711

1712
    auto comps
1713
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1714
              return elem.match(
1715
                  [](const std::string& str) {
×
1716
                      stack_buf allocator;
5✔
1717
                      return json_ptr::encode(str, allocator).to_string();
10✔
1718
                  },
5✔
1719
                  [](size_t index) { return fmt::to_string(index); });
13✔
1720
          });
2✔
1721

1722
    return fmt::format(FMT_STRING("#/{}"),
8✔
1723
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1724
}
12✔
1725

1726
std::optional<std::string>
1727
textfile_sub_source::anchor_for_row(vis_line_t vl)
12✔
1728
{
1729
    const auto curr_iter = this->current_file_state();
12✔
1730
    if (curr_iter == this->tss_files.end()) {
12✔
1731
        return std::nullopt;
×
1732
    }
1733
    return (*curr_iter)->anchor_for_row(this->tss_view_mode, vl);
12✔
1734
}
1735

1736
bool
1737
textfile_sub_source::to_front(const std::string& filename)
1✔
1738
{
1739
    auto lf_opt = this->tss_files
1✔
1740
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1741
                      return elem->fvs_file->get_filename() == filename;
1✔
1742
                  });
1✔
1743
    if (!lf_opt) {
1✔
1744
        return false;
1✔
1745
    }
1746

1747
    this->to_front((*lf_opt.value())->fvs_file);
×
1748

1749
    return true;
×
1750
}
1751

1752
logline*
1753
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1754
{
1755
    auto lf = this->current_file();
3✔
1756
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1757
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1758
}
3✔
1759

1760
void
1761
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1762
{
1763
    this->tss_view_mode = vm;
2✔
1764
    this->tss_view->set_needs_update();
2✔
1765
}
2✔
1766

1767
textfile_sub_source::view_mode
1768
textfile_sub_source::get_effective_view_mode() const
401✔
1769
{
1770
    auto retval = view_mode::raw;
401✔
1771

1772
    const auto curr_iter = this->current_file_state();
401✔
1773
    if (curr_iter != this->tss_files.end()) {
401✔
1774
        if (this->tss_view_mode == view_mode::rendered
802✔
1775
            && (*curr_iter)->fvs_text_source)
401✔
1776
        {
1777
            retval = view_mode::rendered;
11✔
1778
        }
1779
    }
1780

1781
    return retval;
401✔
1782
}
1783

1784
void
1785
textfile_sub_source::move_to_init_location(file_iterator& iter)
590✔
1786
{
1787
    if ((*iter)->fvs_consumed_init_location) {
590✔
1788
        return;
464✔
1789
    }
1790

1791
    auto& lf = (*iter)->fvs_file;
126✔
1792
    std::optional<vis_line_t> new_sel_opt;
126✔
1793
    require(lf->get_open_options().loo_init_location.valid());
126✔
1794
    lf->get_open_options().loo_init_location.match(
126✔
1795
        [this, &new_sel_opt, &lf](default_for_text_format def) {
×
1796
            if (!this->tss_apply_default_init_location) {
123✔
1797
                return;
123✔
1798
            }
1799
            auto tf = lf->get_text_format().value_or(text_format_t::TF_BINARY);
×
1800
            switch (tf) {
×
1801
                case text_format_t::TF_PLAINTEXT:
×
1802
                case text_format_t::TF_LOG: {
1803
                    log_info("file open request to tail");
×
1804
                    auto inner_height = lf->size();
×
1805
                    if (inner_height > 0) {
×
1806
                        new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1807
                    }
1808
                    break;
×
1809
                }
1810
                default:
×
1811
                    log_info("file open is %s, moving to top",
×
1812
                             fmt::to_string(tf).c_str());
1813
                    new_sel_opt = 0_vl;
×
1814
                    break;
×
1815
            }
1816
        },
1817
        [&new_sel_opt, &lf](file_location_tail tail) {
×
1818
            log_info("file open request to tail");
×
1819
            auto inner_height = lf->size();
×
1820
            if (inner_height > 0) {
×
1821
                new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1822
            }
1823
        },
×
1824
        [this, &new_sel_opt, &iter](int vl) {
×
1825
            log_info("file open request to jump to line: %d", vl);
2✔
1826
            auto height = (*iter)->text_line_count(this->tss_view_mode);
2✔
1827
            if (vl < 0) {
2✔
1828
                vl += height;
2✔
1829
                if (vl < 0) {
2✔
1830
                    vl = 0;
1✔
1831
                }
1832
            }
1833
            if (vl < height) {
2✔
1834
                new_sel_opt = vis_line_t(vl);
2✔
1835
            }
1836
        },
2✔
1837
        [this, &new_sel_opt, &iter](const std::string& loc) {
126✔
1838
            log_info("file open request to jump to anchor: %s", loc.c_str());
1✔
1839
            new_sel_opt = (*iter)->row_for_anchor(this->tss_view_mode, loc);
1✔
1840
        });
1✔
1841

1842
    if (new_sel_opt) {
126✔
1843
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1844
        log_info("  setting requested selection: %d",
3✔
1845
                 (int) new_sel_opt.value());
1846
        (*iter)->fvs_selection = new_sel_opt;
3✔
1847
        log_info("  actual top is now: %d", (int) (*iter)->fvs_top);
3✔
1848
        log_info("  actual selection is now: %d",
3✔
1849
                 (int) (*iter)->fvs_selection.value());
1850

1851
        if (this->current_file() == lf) {
3✔
1852
            this->tss_view->set_selection((*iter)->fvs_selection.value());
3✔
1853
        }
1854
    }
1855
    (*iter)->fvs_consumed_init_location = true;
126✔
1856
}
1857

1858
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
813✔
1859
                                                 text_sub_source* log_src)
813✔
1860
    : tho_src(src), tho_log_src(log_src)
813✔
1861
{
1862
}
813✔
1863

1864
bool
1865
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
9,677✔
1866
                                             media_t media,
1867
                                             int y,
1868
                                             int bottom,
1869
                                             attr_line_t& value_out)
1870
{
1871
    if (media == media_t::display) {
9,677✔
1872
        const std::vector<attr_line_t>* lines = nullptr;
9,606✔
1873
        auto curr_file = this->tho_src->current_file();
9,606✔
1874
        if (curr_file == nullptr) {
9,606✔
1875
            if (this->tho_log_src->text_line_count() == 0) {
×
1876
                lines = lnav::messages::view::no_files();
×
1877
            } else {
1878
                lines = lnav::messages::view::only_log_files();
×
1879
            }
1880
        } else if (!curr_file->get_notes().empty()) {
9,606✔
1881
            this->tho_static_lines = curr_file->get_notes()
×
1882
                                         .values()
×
1883
                                         .front()
×
1884
                                         .to_attr_line()
×
1885
                                         .split_lines();
×
1886
            lines = &this->tho_static_lines;
×
1887
        } else if (curr_file->size() == 0) {
9,606✔
1888
            lines = lnav::messages::view::empty_file();
×
1889
        } else if (this->tho_src->text_line_count() == 0) {
9,606✔
1890
            hasher h;
×
1891
            this->tho_src->update_filter_hash_state(h);
×
1892
            auto curr_state = h.to_array();
×
1893
            if (this->tho_static_lines.empty()
×
1894
                || curr_state != this->tho_filter_state)
×
1895
            {
1896
                auto msg = lnav::console::user_message::info(
1897
                    "All text lines are currently hidden");
×
1898
                auto min_time = this->tho_src->get_min_row_time();
×
1899
                if (min_time) {
×
1900
                    msg.with_note(attr_line_t("Lines before ")
×
1901
                                      .append_quoted(lnav::to_rfc3339_string(
×
1902
                                          min_time.value()))
×
1903
                                      .append(" are not being shown"));
×
1904
                }
1905
                auto max_time = this->tho_src->get_max_row_time();
×
1906
                if (max_time) {
×
1907
                    msg.with_note(attr_line_t("Lines after ")
×
1908
                                      .append_quoted(lnav::to_rfc3339_string(
×
1909
                                          max_time.value()))
×
1910
                                      .append(" are not being shown"));
×
1911
                }
1912
                auto& fs = this->tho_src->get_filters();
×
1913
                for (const auto& filt : fs) {
×
1914
                    auto hits = this->tho_src->get_filtered_count_for(
×
1915
                        filt->get_index());
1916
                    if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
1917
                        continue;
×
1918
                    }
1919
                    auto cmd = attr_line_t(":" + filt->to_command());
×
1920
                    readline_command_highlighter(cmd, std::nullopt);
×
1921
                    msg.with_note(
×
1922
                        attr_line_t("Filter ")
×
1923
                            .append_quoted(cmd)
×
1924
                            .append(" matched ")
×
1925
                            .append(lnav::roles::number(fmt::to_string(hits)))
×
1926
                            .append(" line(s) "));
×
1927
                }
1928
                this->tho_static_lines = msg.to_attr_line().split_lines();
×
1929
                this->tho_filter_state = curr_state;
×
1930
            }
1931

1932
            lines = &this->tho_static_lines;
×
1933
        }
1934

1935
        if (lines != nullptr && y < (ssize_t) lines->size()) {
9,606✔
1936
            value_out = lines->at(y);
×
1937
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
1938
            if (y == (ssize_t) lines->size() - 1) {
×
1939
                value_out.with_attr_for_all(
×
1940
                    VC_STYLE.value(text_attrs::with_underline()));
×
1941
            }
1942
            return true;
×
1943
        }
1944
    }
9,606✔
1945

1946
    if (this->tho_src->text_line_count() > 0) {
9,677✔
1947
        auto& tc
1948
            = dynamic_cast<textview_curses&>(const_cast<listview_curses&>(lv));
9,677✔
1949
        const auto& sticky_bv = tc.get_bookmarks()[&textview_curses::BM_STICKY];
9,677✔
1950
        if (!sticky_bv.empty()) {
9,677✔
1951
            auto top = lv.get_top();
159✔
1952
            int sticky_index = 0;
159✔
1953
            for (auto iter = sticky_bv.bv_tree.begin();
159✔
1954
                 iter != sticky_bv.bv_tree.end();
235✔
1955
                 ++iter)
76✔
1956
            {
1957
                if (*iter >= top) {
180✔
1958
                    break;
89✔
1959
                }
1960
                if (y == sticky_index) {
91✔
1961
                    tc.textview_value_for_row(*iter, value_out);
15✔
1962
                    value_out.with_attr_for_all(
15✔
1963
                        VC_ROLE.value(role_t::VCR_STATUS));
30✔
1964
                    auto next_iter = std::next(iter);
15✔
1965
                    if (next_iter == sticky_bv.bv_tree.end()
15✔
1966
                        || *next_iter >= top)
15✔
1967
                    {
1968
                        value_out.with_attr_for_all(
12✔
1969
                            VC_STYLE.value(text_attrs::with_underline()));
24✔
1970
                    }
1971
                    return true;
15✔
1972
                }
1973
                sticky_index++;
76✔
1974
            }
1975
        }
1976
    }
1977

1978
    if (y != 0) {
9,662✔
1979
        return false;
9,098✔
1980
    }
1981

1982
    const auto lf = this->tho_src->current_file();
564✔
1983
    if (lf == nullptr) {
564✔
1984
        return false;
×
1985
    }
1986

1987
    if (media == media_t::display
564✔
1988
        && lf->get_text_format() != text_format_t::TF_MARKDOWN
502✔
1989
        && this->tho_src->get_effective_view_mode()
1,066✔
1990
            == textfile_sub_source::view_mode::rendered)
1991
    {
1992
        auto ta = text_attrs::with_underline();
11✔
1993
        value_out.append("\u24d8"_info)
11✔
1994
            .append(" The following is a rendered view of the content.  Use ")
11✔
1995
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
11✔
1996
            .append(" to view the raw version of this text")
11✔
1997
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
22✔
1998
            .with_attr_for_all(VC_STYLE.value(ta));
11✔
1999
        return true;
11✔
2000
    }
2001

2002
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
553✔
2003
        return false;
546✔
2004
    }
2005

2006
    {
2007
        attr_line_builder alb(value_out);
7✔
2008
        {
2009
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
7✔
2010
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
21✔
2011
        }
7✔
2012
        size_t byte_off = 0;
7✔
2013
        for (size_t lpc = 0; lpc < 16; lpc++) {
119✔
2014
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
112✔
2015
            if (byte_off == 8) {
112✔
2016
                alb.append(" ");
7✔
2017
            }
2018
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
336✔
2019
            byte_off += 1;
112✔
2020
        }
112✔
2021
        {
2022
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
7✔
2023
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
21✔
2024
        }
7✔
2025
    }
2026
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
7✔
2027
    return true;
7✔
2028
}
564✔
2029

2030
std::optional<attr_line_t>
2031
textfile_header_overlay::list_header_for_overlay(const listview_curses& lv,
4✔
2032
                                                 media_t media,
2033
                                                 vis_line_t line)
2034
{
2035
    return this->tho_hex_line_header;
4✔
2036
}
2037

2038
void
2039
textfile_header_overlay::list_value_for_overlay(
11,420✔
2040
    const listview_curses& lv,
2041
    vis_line_t line,
2042
    std::vector<attr_line_t>& value_out)
2043
{
2044
    if (line != lv.get_selection()) {
11,420✔
2045
        return;
11,414✔
2046
    }
2047

2048
    if (this->tho_src->empty() || line < 0) {
309✔
2049
        value_out.clear();
×
2050
        return;
×
2051
    }
2052

2053
    const auto curr_iter = this->tho_src->current_file_state();
309✔
2054
    if (this->tho_src->tss_view_mode == textfile_sub_source::view_mode::rendered
618✔
2055
        && (*curr_iter)->fvs_text_source)
309✔
2056
    {
2057
        return;
53✔
2058
    }
2059
    const auto& lf = (*curr_iter)->fvs_file;
256✔
2060
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
256✔
2061
        return;
7✔
2062
    }
2063

2064
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
249✔
2065
    if (lfo == nullptr
249✔
2066
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
249✔
2067
    {
2068
        value_out.clear();
×
2069
        return;
×
2070
    }
2071

2072
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
249✔
2073
    if (ll->is_valid_utf()) {
249✔
2074
        return;
243✔
2075
    }
2076

2077
    auto read_opts = subline_options{};
6✔
2078
    read_opts.scrub_invalid_utf8 = false;
6✔
2079
    auto read_result = lf->read_line(ll, read_opts);
6✔
2080
    if (read_result.isErr()) {
6✔
2081
        return;
×
2082
    }
2083

2084
    auto sbr = read_result.unwrap();
6✔
2085
    attr_line_t al;
6✔
2086
    attr_line_builder alb(al);
6✔
2087
    alb.append_as_hexdump(sbr.to_string_fragment());
6✔
2088
    this->tho_hex_line_header
2089
        = attr_line_t(" Line ")
6✔
2090
              .append(lnav::roles::number(fmt::to_string(line + 1)))
12✔
2091
              .append(" at file offset ")
6✔
2092
              .append(lnav::roles::number(fmt::to_string(ll->get_offset())))
12✔
2093
              .append(
6✔
2094
                  " contains invalid UTF-8 content, the following is a hex "
2095
                  "dump of the line");
6✔
2096
    al.split_lines(value_out);
6✔
2097
}
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