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

tstack / lnav / 20114321350-2741

10 Dec 2025 09:41PM UTC coverage: 68.836% (-0.07%) from 68.908%
20114321350-2741

push

github

tstack
[filters] add level filter

41 of 135 new or added lines in 10 files covered. (30.37%)

447 existing lines in 8 files now uncovered.

51534 of 74865 relevant lines covered (68.84%)

434761.7 hits per line

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

66.36
/src/textfile_sub_source.cc
1
/**
2
 * Copyright (c) 2020, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#include <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)
558✔
65
{
66
    static const auto& cfg = injector::get<const lnav::textfile::config&>();
558✔
67

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

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

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

93
    if (!this->tss_files.empty()) {
23,135✔
94
        retval
95
            = this->current_file_state()->text_line_count(this->tss_view_mode);
15,632✔
96
    }
97

98
    return retval;
23,135✔
99
}
100

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

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

112
line_info
113
textfile_sub_source::text_value_for_line(textview_curses& tc,
3,440✔
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) {
3,440✔
UNCOV
119
        value_out.clear();
×
UNCOV
120
        return {};
×
121
    }
122

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

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

139
        auto read_res = lf->read_range(fr);
1,430✔
140
        if (read_res.isErr()) {
1,430✔
UNCOV
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());
UNCOV
146
            return {};
×
147
        }
148

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

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

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

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

203
    return {};
716✔
204
}
716✔
205

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

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

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

239
                shift_string_attrs(value_out, 0, time_offset_end);
1✔
240

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

246
                auto bar_role = role_t::VCR_NONE;
1✔
247

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

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

316
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
3,440✔
317
}
318

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

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

UNCOV
353
    return retval;
×
354
}
355

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

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

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

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

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

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

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

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

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

461
    this->tss_view->redo_search();
3✔
462

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

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

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

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

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

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

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

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

UNCOV
522
    if (lf == nullptr) {
×
UNCOV
523
        return 0;
×
524
    }
525

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

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

537
    return this->tss_files.front().fvs_file->get_text_format();
2,941✔
538
}
539

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

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

552
    return retval;
22✔
UNCOV
553
}
×
554

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

561
    if (this->empty()) {
11✔
UNCOV
562
        return;
×
563
    }
564

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

UNCOV
587
            if (!lf_opt) {
×
UNCOV
588
                return;
×
589
            }
590

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

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

609
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
610

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

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

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

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

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

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

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

746
    file_iterator iter;
4,443✔
747
    rescan_result_t retval;
4,443✔
748
    size_t files_scanned = 0;
4,443✔
749

750
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,443✔
751
        return retval;
124✔
752
    }
753

754
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,319✔
755

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

766
        std::shared_ptr<logfile> lf = iter->fvs_file;
1,266✔
767

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

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

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

788
            if (lf->get_format() != nullptr) {
1,165✔
789
                iter = this->tss_files.erase(iter);
519✔
790
                this->detach_observer(lf);
519✔
791
                callback.promote_file(lf);
519✔
792
                continue;
613✔
793
            }
794

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

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

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

843
                    if (read_res.isOk()) {
95✔
844
                        auto read_file_res = read_res.unwrap();
95✔
845

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

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

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

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

893
            uint32_t filter_in_mask, filter_out_mask;
894

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

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

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

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

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

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

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

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

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

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

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

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

1059
    return retval;
4,319✔
1060
}
4,337✔
1061

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

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

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

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

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

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

1113
    return retval;
15,634✔
1114
}
1115

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

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

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

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

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

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

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

1179
        return retval;
2✔
1180
    }
2✔
1181

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

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

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

UNCOV
1201
    return retval;
×
1202
}
1203

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

1212
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
6✔
1213
}
1214

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

1254
std::unordered_set<std::string>
UNCOV
1255
textfile_sub_source::get_anchors()
×
1256
{
UNCOV
1257
    std::unordered_set<std::string> retval;
×
1258

1259
    const auto curr_iter = this->current_file_state();
×
1260
    if (curr_iter == this->tss_files.end()) {
×
UNCOV
1261
        return retval;
×
1262
    }
1263

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

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

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

UNCOV
1279
    return retval;
×
1280
}
1281

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

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

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

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

UNCOV
1309
    return std::nullopt;
×
1310
}
1✔
1311

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

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

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

UNCOV
1340
    return std::nullopt;
×
1341
}
1342

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

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

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

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

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

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

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

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

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

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

1439
    log_debug("  neighbors p:%d n:%d",
3✔
1440
              neighbors_res->cnr_previous.has_value(),
1441
              neighbors_res->cnr_next.has_value());
1442
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
3✔
1443
        auto neighbor_sub
1444
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
3✔
1445
        if (neighbor_sub) {
3✔
UNCOV
1446
            neighbors_res->cnr_previous = neighbor_sub;
×
1447
        }
1448
    }
1449

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

1458
    switch (dir) {
3✔
1459
        case direction::prev: {
1✔
1460
            if (neighbors_res->cnr_previous) {
1✔
1461
                return to_vis_line(
1✔
1462
                    lf, neighbors_res->cnr_previous.value()->hn_start);
1✔
1463
            }
UNCOV
1464
            break;
×
1465
        }
1466
        case direction::next: {
2✔
1467
            if (neighbors_res->cnr_next) {
2✔
1468
                return to_vis_line(lf,
2✔
1469
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1470
            }
UNCOV
1471
            break;
×
1472
        }
1473
    }
1474

UNCOV
1475
    return std::nullopt;
×
1476
}
3✔
1477

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

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

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

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

1507
    if (path_for_line.empty()) {
9✔
1508
        return std::nullopt;
5✔
1509
    }
1510

1511
    if ((path_for_line.size() == 1
4✔
1512
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1513
        && path_for_line.back().is<std::string>())
6✔
1514
    {
1515
        return text_anchors::to_anchor_string(
2✔
1516
            path_for_line.back().get<std::string>());
2✔
1517
    }
1518

1519
    auto comps
1520
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1521
              return elem.match(
UNCOV
1522
                  [](const std::string& str) {
×
1523
                      stack_buf allocator;
5✔
1524
                      return json_ptr::encode(str, allocator).to_string();
10✔
1525
                  },
5✔
1526
                  [](size_t index) { return fmt::to_string(index); });
13✔
1527
          });
2✔
1528

1529
    return fmt::format(FMT_STRING("#/{}"),
8✔
1530
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1531
}
9✔
1532

1533
bool
1534
textfile_sub_source::to_front(const std::string& filename)
1✔
1535
{
1536
    auto lf_opt = this->tss_files
1✔
1537
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1538
                      return elem.fvs_file->get_filename() == filename;
1✔
1539
                  });
1✔
1540
    if (!lf_opt) {
1✔
1541
        return false;
1✔
1542
    }
1543

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

UNCOV
1546
    return true;
×
1547
}
1548

1549
logline*
1550
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1551
{
1552
    auto lf = this->current_file();
3✔
1553
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1554
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1555
}
3✔
1556

1557
void
1558
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1559
{
1560
    this->tss_view_mode = vm;
2✔
1561
    this->tss_view->set_needs_update();
2✔
1562
}
2✔
1563

1564
textfile_sub_source::view_mode
1565
textfile_sub_source::get_effective_view_mode() const
379✔
1566
{
1567
    auto retval = view_mode::raw;
379✔
1568

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

1578
    return retval;
379✔
1579
}
1580

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

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

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

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

1654
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
634✔
1655
                                                 text_sub_source* log_src)
634✔
1656
    : tho_src(src), tho_log_src(log_src)
634✔
1657
{
1658
}
634✔
1659

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

UNCOV
1728
            lines = &this->tho_static_lines;
×
1729
        }
1730

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

1742
    if (y != 0) {
9,866✔
1743
        return false;
9,359✔
1744
    }
1745

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

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

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

1770
    {
1771
        attr_line_builder alb(value_out);
27✔
1772
        {
1773
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1774
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
81✔
1775
        }
27✔
1776
        size_t byte_off = 0;
27✔
1777
        for (size_t lpc = 0; lpc < 16; lpc++) {
459✔
1778
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
432✔
1779
            if (byte_off == 8) {
432✔
1780
                alb.append(" ");
27✔
1781
            }
1782
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
1,296✔
1783
            byte_off += 1;
432✔
1784
        }
432✔
1785
        {
1786
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
27✔
1787
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
81✔
1788
        }
27✔
1789
    }
1790
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
27✔
1791
    return true;
27✔
1792
}
507✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc