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

tstack / lnav / 20245728190-2749

15 Dec 2025 07:59PM UTC coverage: 68.864% (-0.07%) from 68.929%
20245728190-2749

push

github

tstack
[text_format] add plaintext type

Related to #1296

85 of 132 new or added lines in 24 files covered. (64.39%)

73 existing lines in 10 files now uncovered.

51605 of 74938 relevant lines covered (68.86%)

434003.35 hits per line

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

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

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

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

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

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

98
    return retval;
20,442✔
99
}
100

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

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

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

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

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

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

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

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

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

175
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
722✔
176
    auto read_result = lf->read_line(ll);
722✔
177
    this->tss_line_indent_size = 0;
722✔
178
    this->tss_plain_line_attrs.clear();
722✔
179
    if (read_result.isOk()) {
722✔
180
        auto sbr = read_result.unwrap();
722✔
181
        value_out = to_string(sbr);
722✔
182
        if (sbr.get_metadata().m_has_ansi) {
722✔
183
            scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
35✔
184
        }
185
        for (const auto& ch : value_out) {
2,018✔
186
            if (ch == ' ') {
1,884✔
187
                this->tss_line_indent_size += 1;
1,294✔
188
            } else if (ch == '\t') {
590✔
189
                do {
190
                    this->tss_line_indent_size += 1;
15✔
191
                } while (this->tss_line_indent_size % 8);
15✔
192
            } else {
193
                break;
588✔
194
            }
195
        }
196
        if (lf->has_line_metadata() && this->tas_display_time_offset) {
722✔
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
    }
722✔
202

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

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

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

233
            value_out.emplace_back(lr, SA_LEVEL.value(ll->get_msg_level()));
722✔
234
            if (lf->has_line_metadata() && this->tas_display_time_offset) {
722✔
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:
×
255
                        bar_role = role_t::VCR_DIFF_ADD;
×
256
                        break;
×
257
                }
258
                if (bar_role != role_t::VCR_NONE) {
1✔
259
                    value_out.emplace_back(line_range(12, 13),
×
260
                                           VC_ROLE.value(bar_role));
×
261
                }
262
            }
263

264
            if (curr_iter->fvs_metadata.m_sections_root) {
722✔
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✔
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✔
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()));
2,091✔
317
}
318

319
size_t
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;
×
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());
×
334
            if (lfo == nullptr || line < 0
×
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);
×
343
                    scrub_ansi_string(str, nullptr);
×
344
                    retval = string_fragment::from_str(str).column_width();
×
345
                }
346
            }
347
        } else {
348
            retval = curr_iter->fvs_text_source->text_size_for_line(
×
349
                tc, line, flags);
350
        }
351
    }
352

353
    return retval;
×
354
}
355

356
void
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);
×
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);
×
369
    fvs.load_into(*this->tss_view);
×
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();
×
383
        this->tss_view->redo_search();
×
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();
×
399
        this->tss_view->redo_search();
×
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✔
409
        this->tss_files.erase(iter);
×
410
        this->detach_observer(lf);
×
411
    }
412
    this->set_time_offset(false);
519✔
413
    if (!this->tss_files.empty()) {
519✔
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;
×
450
                if (ll->get_timeval() < this->ttt_min_row_time) {
×
451
                    continue;
×
452
                }
453
                if (this->ttt_max_row_time < ll->get_timeval()) {
×
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)
759✔
473
{
474
    const auto curr_iter = this->current_file_state();
759✔
475
    if (curr_iter == this->tss_files.end()
759✔
476
        || curr_iter->fvs_file->get_text_format() == text_format_t::TF_BINARY)
759✔
477
    {
478
        return;
672✔
479
    }
480

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

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

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

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

505
    if (curr_iter != this->tss_files.end()) {
276✔
506
        if (this->tss_view_mode == view_mode::raw
544✔
507
            || !curr_iter->fvs_text_source)
272✔
508
        {
509
            const auto& lf = curr_iter->fvs_file;
214✔
510
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
214✔
511
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
214✔
512
        }
513
    }
514
    return retval;
276✔
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

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

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

530
std::optional<text_format_t>
531
textfile_sub_source::get_text_format() const
1,582✔
532
{
533
    if (this->tss_files.empty()) {
1,582✔
NEW
534
        return text_format_t::TF_PLAINTEXT;
×
535
    }
536

537
    return this->tss_files.front().fvs_file->get_text_format();
1,582✔
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✔
547
            retval.append("\u21bb "_list_glyph);
×
548
        }
549
    }
550
    retval.append(lf->get_unique_path());
22✔
551

552
    return retval;
22✔
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✔
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✔
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
×
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

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

591
            this->to_front(lf_opt.value());
×
592
            this->tss_view->reload_data();
×
593
        });
×
594
    if (lf->size() == 0) {
11✔
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
        {
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
        {
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✔
651
                        if (!curr_node) {
×
652
                            return;
×
653
                        }
654
                        auto* parent_node = curr_node.value()->hn_parent;
×
655
                        if (parent_node == nullptr) {
×
656
                            return;
×
657
                        }
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
×
663
                                    == parent_node->hn_named_children.end()) {
×
664
                                    return;
×
665
                                }
666
                                this->set_top_from_off(
×
667
                                    sib_iter->second->hn_start);
×
668
                            },
669
                            [this, parent_node](size_t index) {
×
670
                                if (index >= parent_node->hn_children.size()) {
×
671
                                    return;
×
672
                                }
673
                                auto sib
674
                                    = parent_node->hn_children[index].get();
×
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) {
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()) {
×
719
                            this->set_top_from_off(
×
720
                                child_iter->second->hn_start);
×
721
                        }
722
                    },
×
723
                    [this, curr_node](size_t index) {
×
724
                        if (index >= curr_node->hn_children.size()) {
×
725
                            return;
×
726
                        }
727
                        auto* child = curr_node->hn_children[index].get();
×
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,439✔
742
                                  std::optional<ui_clock::time_point> deadline)
743
{
744
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,439✔
745

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

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

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

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

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

768
        if (lf->is_closed()) {
1,267✔
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,166✔
777
            retval.rr_scan_completed = false;
×
778
            ++iter;
×
779
            continue;
×
780
        }
781
        files_scanned += 1;
1,166✔
782

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

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

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

810
            if (lf->is_indexing()
647✔
811
                && lf->get_text_format() != text_format_t::TF_BINARY)
647✔
812
            {
813
                if (!new_data) {
641✔
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
554✔
817
                         || st.st_size != iter->fvs_file_size
541✔
818
                         || lf->get_index_size() != iter->fvs_file_indexed_size)
541✔
819
                        && (st.st_size < 10 * 1024 || iter->fvs_file_size == 0
1,095✔
820
                            || !iter->fvs_metadata.m_sections_tree.empty()))
×
821
                    {
822
                        log_debug(
98✔
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 = {};
98✔
833
                        iter->fvs_error.clear();
98✔
834
                    }
835
                }
836

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

843
                    if (read_res.isOk()) {
189✔
844
                        auto read_file_res = read_res.unwrap();
189✔
845
                        auto tf_opt = lf->get_text_format();
189✔
846

847
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf
378✔
848
                            || !tf_opt)
189✔
849
                        {
850
                            log_error(
107✔
851
                                "%s: file has no text format, skipping meta "
852
                                "discovery",
853
                                lf->get_path_for_key().c_str());
854
                            iter->fvs_mtime = st.st_mtime;
107✔
855
                            iter->fvs_file_size = lf->get_index_size();
107✔
856
                        } else {
857
                            auto content
858
                                = attr_line_t(read_file_res.rfr_content);
82✔
859

860
                            log_info("generating metadata for: %s (size=%zu)",
82✔
861
                                     lf->get_path_for_key().c_str(),
862
                                     content.length());
863
                            scrub_ansi_string(content.get_string(),
82✔
864
                                              &content.get_attrs());
82✔
865

866
                            auto text_meta = extract_text_meta(
867
                                content.get_string(), tf_opt.value());
82✔
868
                            if (text_meta) {
82✔
869
                                lf->set_filename(text_meta->tfm_filename);
3✔
870
                                lf->set_include_in_session(true);
3✔
871
                                callback.renamed_file(lf);
3✔
872
                            }
873

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

898
            uint32_t filter_in_mask, filter_out_mask;
899

900
            this->get_filters().get_enabled_mask(filter_in_mask,
647✔
901
                                                 filter_out_mask);
902
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
647✔
903
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,737✔
904
                if (this->tss_apply_filters
8,180✔
905
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
4,090✔
906
                {
907
                    continue;
×
908
                }
909
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
4,090✔
910
            }
911

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

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

936
                    mdal.with_source_path(lf->get_actual_path());
15✔
937
                    if (this->tss_view->tc_interactive) {
15✔
938
                        mdal.add_lnav_script_icons();
×
939
                    }
940
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
15✔
941

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

952
                        iter->fvs_text_source->replace_with(parse_res.unwrap());
13✔
953
                        if (!md_file.f_frontmatter.empty()) {
13✔
954
                            lf_meta["net.daringfireball.markdown.frontmatter"]
10✔
955
                                = {
956
                                    md_file.f_frontmatter_format,
5✔
957
                                    md_file.f_frontmatter.to_string(),
958
                                };
10✔
959
                        }
960

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

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

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

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

1051
        ++iter;
549✔
1052
    }
1,267✔
1053
    if (!closed_files.empty()) {
4,315✔
1054
        callback.closed_files(closed_files);
97✔
1055
        if (!this->tss_files.empty()) {
97✔
1056
            this->tss_files.front().load_into(*this->tss_view);
×
1057
        }
1058
        this->tss_view->set_needs_update();
97✔
1059
    }
1060

1061
    if (retval.rr_new_data) {
4,315✔
1062
        this->tss_view->search_new_data();
84✔
1063
    }
1064

1065
    return retval;
4,315✔
1066
}
4,418✔
1067

1068
void
1069
textfile_sub_source::set_top_from_off(file_off_t off)
×
1070
{
1071
    auto lf = this->current_file();
×
1072

1073
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1074
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1075
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1076
            std::distance(lf->cbegin(), new_top_iter));
×
1077

1078
        if (new_top_opt) {
×
1079
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1080
            if (this->tss_view->is_selectable()) {
×
1081
                this->tss_view->set_top(
×
1082
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1083
            }
1084
        }
1085
    };
1086
}
1087

1088
void
1089
textfile_sub_source::quiesce()
×
1090
{
1091
    for (auto& lf : this->tss_files) {
×
1092
        lf.fvs_file->quiesce();
×
1093
    }
1094
}
1095

1096
size_t
1097
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
12,939✔
1098
{
1099
    size_t retval = 0;
12,939✔
1100

1101
    if (mode == view_mode::raw || !this->fvs_text_source) {
12,939✔
1102
        const auto& lf = this->fvs_file;
7,131✔
1103
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
7,131✔
1104
            const auto fsize = lf->get_content_size();
28✔
1105
            retval = fsize / 16;
28✔
1106
            if (fsize % 16) {
28✔
1107
                retval += 1;
28✔
1108
            }
1109
        } else {
1110
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
7,103✔
1111
            if (lfo != nullptr) {
7,103✔
1112
                retval = lfo->lfo_filter_state.tfs_index.size();
7,103✔
1113
            }
1114
        }
1115
    } else {
1116
        retval = this->fvs_text_source->text_line_count();
5,808✔
1117
    }
1118

1119
    return retval;
12,939✔
1120
}
1121

1122
size_t
1123
textfile_sub_source::file_view_state::text_line_width(view_mode mode,
576✔
1124
                                                      textview_curses& tc) const
1125
{
1126
    size_t retval = 0;
576✔
1127
    if (mode == view_mode::raw || !this->fvs_text_source) {
576✔
1128
        const auto& lf = this->fvs_file;
446✔
1129
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
446✔
1130
            retval = 88;
5✔
1131
        } else {
1132
            retval = lf->get_longest_line_length();
441✔
1133
        }
1134
    } else {
1135
        retval = this->fvs_text_source->text_line_width(tc);
130✔
1136
    }
1137
    return retval;
576✔
1138
}
1139

1140
std::optional<vis_line_t>
1141
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
7✔
1142
                                                     const std::string& id)
1143
{
1144
    if (mode == view_mode::rendered && this->fvs_text_source) {
7✔
1145
        return this->fvs_text_source->row_for_anchor(id);
5✔
1146
    }
1147

1148
    if (!this->fvs_metadata.m_sections_root) {
2✔
1149
        return std::nullopt;
×
1150
    }
1151

1152
    const auto& lf = this->fvs_file;
2✔
1153
    const auto& meta = this->fvs_metadata;
2✔
1154
    std::optional<vis_line_t> retval;
2✔
1155

1156
    auto is_ptr = startswith(id, "#/");
2✔
1157
    if (is_ptr) {
2✔
1158
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1159
        std::vector<lnav::document::section_key_t> path;
2✔
1160

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

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

1185
        return retval;
2✔
1186
    }
2✔
1187

1188
    lnav::document::hier_node::depth_first(
×
1189
        meta.m_sections_root.get(),
1190
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1191
            for (const auto& child_pair : node->hn_named_children) {
×
1192
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1193

1194
                if (child_anchor != id) {
×
1195
                    continue;
×
1196
                }
1197

1198
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1199
                if (ll_opt != lf->end()) {
×
1200
                    retval = vis_line_t(
×
1201
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1202
                }
1203
                break;
×
1204
            }
1205
        });
×
1206

1207
    return retval;
×
1208
}
1209

1210
std::optional<vis_line_t>
1211
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1212
{
1213
    const auto curr_iter = this->current_file_state();
6✔
1214
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
1215
        return std::nullopt;
×
1216
    }
1217

1218
    return curr_iter->row_for_anchor(this->tss_view_mode, id);
6✔
1219
}
1220

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

1260
std::unordered_set<std::string>
1261
textfile_sub_source::get_anchors()
×
1262
{
1263
    std::unordered_set<std::string> retval;
×
1264

1265
    const auto curr_iter = this->current_file_state();
×
1266
    if (curr_iter == this->tss_files.end()) {
×
1267
        return retval;
×
1268
    }
1269

1270
    if (this->tss_view_mode == view_mode::rendered
×
1271
        && curr_iter->fvs_text_source)
×
1272
    {
1273
        return curr_iter->fvs_text_source->get_anchors();
×
1274
    }
1275

1276
    const auto& meta = curr_iter->fvs_metadata;
×
1277
    if (meta.m_sections_root == nullptr) {
×
1278
        return retval;
×
1279
    }
1280

1281
    std::vector<std::string> comps;
×
1282
    size_t max_depth = 0;
×
1283
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1284

1285
    return retval;
×
1286
}
1287

1288
struct tfs_time_cmp {
1289
    bool operator()(int32_t lhs, const timeval& rhs) const
1✔
1290
    {
1291
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
1✔
1292
        return ll->get_timeval() < rhs;
1✔
1293
    }
1294

1295
    logfile* ttc_logfile;
1296
    std::vector<uint32_t>& ttc_index;
1297
};
1298

1299
std::optional<vis_line_t>
1300
textfile_sub_source::row_for_time(timeval time_bucket)
1✔
1301
{
1302
    auto lf = this->current_file();
1✔
1303
    if (!lf || !lf->has_line_metadata()) {
1✔
1304
        return std::nullopt;
×
1305
    }
1306

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

1315
    return std::nullopt;
×
1316
}
1✔
1317

1318
std::optional<text_time_translator::row_info>
1319
textfile_sub_source::time_for_row(vis_line_t row)
4,420✔
1320
{
1321
    auto lf = this->current_file();
4,420✔
1322
    if (!lf || !lf->has_line_metadata()) {
4,420✔
1323
        return std::nullopt;
4,271✔
1324
    }
1325

1326
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
149✔
1327
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
149✔
1328
        return std::nullopt;
41✔
1329
    }
1330
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
108✔
1331
    auto ll_iter = lf->begin() + row_id;
108✔
1332
    return row_info{
216✔
1333
        ll_iter->get_timeval(),
1334
        row_id,
1335
    };
108✔
1336
}
4,420✔
1337

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

1346
    return std::nullopt;
×
1347
}
1348

1349
std::optional<vis_line_t>
1350
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
3✔
1351
{
1352
    const auto curr_iter = this->current_file_state();
3✔
1353
    if (curr_iter == this->tss_files.end()) {
3✔
1354
        return std::nullopt;
×
1355
    }
1356

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

1368
    if (!curr_iter->fvs_metadata.m_sections_root) {
3✔
1369
        log_debug("  no metadata available");
×
1370
        return std::nullopt;
×
1371
    }
1372

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

1388
    if (path_for_line.empty()) {
3✔
1389
        log_debug("  no path found");
×
1390
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
1391
        if (!neighbors_res) {
×
1392
            return std::nullopt;
×
1393
        }
1394

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

1418
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
3✔
1419
    const auto last_key = std::move(path_for_line.back());
3✔
1420
    path_for_line.pop_back();
3✔
1421

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

1431
    const auto child_hn = parent->lookup_child(last_key);
3✔
1432
    if (!child_hn) {
3✔
1433
        // XXX "should not happen"
1434
        log_debug("  child not found");
×
1435
        return std::nullopt;
×
1436
    }
1437

1438
    auto neighbors_res = parent->child_neighbors(
9✔
1439
        child_hn.value(), line_offsets.next_offset() + 1);
3✔
1440
    if (!neighbors_res) {
3✔
1441
        log_debug("  no neighbors found");
×
1442
        return std::nullopt;
×
1443
    }
1444

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

1456
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
3✔
1457
        auto neighbor_sub
1458
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
3✔
1459
        if (neighbor_sub) {
3✔
1460
            neighbors_res->cnr_next = neighbor_sub;
3✔
1461
        }
1462
    }
1463

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

1481
    return std::nullopt;
×
1482
}
3✔
1483

1484
std::optional<std::string>
1485
textfile_sub_source::anchor_for_row(vis_line_t vl)
12✔
1486
{
1487
    const auto curr_iter = this->current_file_state();
12✔
1488
    if (curr_iter == this->tss_files.end()) {
12✔
1489
        return std::nullopt;
×
1490
    }
1491

1492
    if (this->tss_view_mode == view_mode::rendered
24✔
1493
        && curr_iter->fvs_text_source)
12✔
1494
    {
1495
        return curr_iter->fvs_text_source->anchor_for_row(vl);
3✔
1496
    }
1497

1498
    if (!curr_iter->fvs_metadata.m_sections_root) {
9✔
1499
        return std::nullopt;
×
1500
    }
1501

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

1513
    if (path_for_line.empty()) {
9✔
1514
        return std::nullopt;
5✔
1515
    }
1516

1517
    if ((path_for_line.size() == 1
4✔
1518
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1519
        && path_for_line.back().is<std::string>())
6✔
1520
    {
1521
        return text_anchors::to_anchor_string(
2✔
1522
            path_for_line.back().get<std::string>());
2✔
1523
    }
1524

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

1535
    return fmt::format(FMT_STRING("#/{}"),
8✔
1536
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1537
}
9✔
1538

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

1550
    this->to_front(lf_opt.value()->fvs_file);
×
1551

1552
    return true;
×
1553
}
1554

1555
logline*
1556
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1557
{
1558
    auto lf = this->current_file();
3✔
1559
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1560
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1561
}
3✔
1562

1563
void
1564
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1565
{
1566
    this->tss_view_mode = vm;
2✔
1567
    this->tss_view->set_needs_update();
2✔
1568
}
2✔
1569

1570
textfile_sub_source::view_mode
1571
textfile_sub_source::get_effective_view_mode() const
375✔
1572
{
1573
    auto retval = view_mode::raw;
375✔
1574

1575
    const auto curr_iter = this->current_file_state();
375✔
1576
    if (curr_iter != this->tss_files.end()) {
375✔
1577
        if (this->tss_view_mode == view_mode::rendered
750✔
1578
            && curr_iter->fvs_text_source)
375✔
1579
        {
1580
            retval = view_mode::rendered;
19✔
1581
        }
1582
    }
1583

1584
    return retval;
375✔
1585
}
1586

1587
void
1588
textfile_sub_source::move_to_init_location(file_iterator& iter)
559✔
1589
{
1590
    if (iter->fvs_consumed_init_location) {
559✔
1591
        return;
459✔
1592
    }
1593

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

1645
    if (new_sel_opt) {
100✔
1646
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1647
        log_info("  setting requested selection: %d",
3✔
1648
                 (int) new_sel_opt.value());
1649
        iter->fvs_selection = new_sel_opt;
3✔
1650
        log_info("  actual top is now: %d", (int) iter->fvs_top);
3✔
1651
        log_info("  actual selection is now: %d",
3✔
1652
                 (int) iter->fvs_selection.value());
1653

1654
        if (this->current_file() == lf) {
3✔
1655
            this->tss_view->set_selection(iter->fvs_selection.value());
3✔
1656
        }
1657
    }
1658
    iter->fvs_consumed_init_location = true;
100✔
1659
}
1660

1661
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
634✔
1662
                                                 text_sub_source* log_src)
634✔
1663
    : tho_src(src), tho_log_src(log_src)
634✔
1664
{
1665
}
634✔
1666

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

1735
            lines = &this->tho_static_lines;
×
1736
        }
1737

1738
        if (lines != nullptr && y < (ssize_t) lines->size()) {
8,466✔
1739
            value_out = lines->at(y);
×
1740
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
1741
            if (y == (ssize_t) lines->size() - 1) {
×
1742
                value_out.with_attr_for_all(
×
1743
                    VC_STYLE.value(text_attrs::with_underline()));
×
1744
            }
1745
            return true;
×
1746
        }
1747
    }
8,466✔
1748

1749
    if (y != 0) {
8,520✔
1750
        return false;
8,012✔
1751
    }
1752

1753
    const auto lf = this->tho_src->current_file();
508✔
1754
    if (lf == nullptr) {
508✔
1755
        return false;
×
1756
    }
1757

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

1773
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
489✔
1774
        return false;
483✔
1775
    }
1776

1777
    {
1778
        attr_line_builder alb(value_out);
6✔
1779
        {
1780
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
6✔
1781
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
18✔
1782
        }
6✔
1783
        size_t byte_off = 0;
6✔
1784
        for (size_t lpc = 0; lpc < 16; lpc++) {
102✔
1785
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
96✔
1786
            if (byte_off == 8) {
96✔
1787
                alb.append(" ");
6✔
1788
            }
1789
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
288✔
1790
            byte_off += 1;
96✔
1791
        }
96✔
1792
        {
1793
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
6✔
1794
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
18✔
1795
        }
6✔
1796
    }
1797
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
6✔
1798
    return true;
6✔
1799
}
508✔
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