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

tstack / lnav / 25176890854-3015

30 Apr 2026 04:20PM UTC coverage: 69.248% (+0.005%) from 69.243%
25176890854-3015

push

github

tstack
[views] move zoom stuff to text_time_translator

This adds support for zooming in the DB view.

Related to #1675

116 of 168 new or added lines in 14 files covered. (69.05%)

6 existing lines in 4 files now uncovered.

54401 of 78560 relevant lines covered (69.25%)

565148.14 hits per line

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

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

30
#include <algorithm>
31
#include <chrono>
32
#include <iterator>
33
#include <memory>
34
#include <unordered_set>
35

36
#include "textfile_sub_source.hh"
37

38
#include <date/date.h>
39

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

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

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

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

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

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

95
    if (!this->tss_files.empty()) {
31,003✔
96
        retval = (*this->current_file_state())
22,478✔
97
                     ->text_line_count(this->tss_view_mode);
22,478✔
98
    }
99

100
    return retval;
31,003✔
101
}
102

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

330
    value_out.emplace_back(lr, L_FILE.value(this->current_file()));
2,214✔
331
}
332

333
size_t
334
textfile_sub_source::text_size_for_line(textview_curses& tc,
×
335
                                        int line,
336
                                        text_sub_source::line_flags_t flags)
337
{
338
    size_t retval = 0;
×
339

340
    if (!this->tss_files.empty()) {
×
341
        const auto curr_iter = this->current_file_state();
×
342
        const auto& lf = (*curr_iter)->fvs_file;
×
343
        if (this->tss_view_mode == view_mode::raw
×
344
            || !(*curr_iter)->fvs_text_source)
×
345
        {
346
            auto* lfo = dynamic_cast<line_filter_observer*>(
×
347
                lf->get_logline_observer());
×
348
            if (lfo == nullptr || line < 0
×
349
                || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
×
350
            {
351
            } else {
352
                auto read_res = lf->read_line(
353
                    lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
×
354
                if (read_res.isOk()) {
×
355
                    auto sbr = read_res.unwrap();
×
356
                    auto str = to_string(sbr);
×
357
                    scrub_ansi_string(str, nullptr);
×
358
                    retval = string_fragment::from_str(str).column_width();
×
359
                }
360
            }
361
        } else {
362
            retval = (*curr_iter)
×
363
                         ->fvs_text_source->text_size_for_line(tc, line, flags);
×
364
        }
365
    }
366

367
    return retval;
×
368
}
369

370
void
371
textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
×
372
{
373
    const auto iter = std::find_if(
374
        this->tss_files.begin(), this->tss_files.end(), [&lf](const auto& fvs) {
×
375
            return fvs->fvs_file == lf;
×
376
        });
×
377
    if (iter == this->tss_files.end()) {
×
378
        return;
×
379
    }
380
    this->tss_files.front()->save_from(*this->tss_view);
×
381
    std::swap(*this->tss_files.begin(), *iter);
×
382
    this->set_time_offset(false);
×
383
    this->tss_files.front()->load_into(*this->tss_view);
×
384
    this->tss_view->reload_data();
×
385
}
386

387
void
388
textfile_sub_source::rotate_left()
×
389
{
390
    if (this->tss_files.size() > 1) {
×
391
        this->tss_files.emplace_back(std::move(this->tss_files.front()));
×
392
        this->tss_files.pop_front();
×
393
        this->tss_files.back()->save_from(*this->tss_view);
×
394
        this->tss_files.front()->load_into(*this->tss_view);
×
395
        this->set_time_offset(false);
×
396
        this->tss_view->reload_data();
×
397
        this->tss_view->redo_search();
×
398
        this->tss_view->set_needs_update();
×
399
    }
400
}
401

402
void
403
textfile_sub_source::rotate_right()
×
404
{
405
    if (this->tss_files.size() > 1) {
×
406
        this->tss_files.front()->save_from(*this->tss_view);
×
407
        auto fvs = std::move(this->tss_files.back());
×
408
        this->tss_files.emplace_front(std::move(fvs));
×
409
        this->tss_files.pop_back();
×
410
        this->tss_files.front()->load_into(*this->tss_view);
×
411
        this->set_time_offset(false);
×
412
        this->tss_view->reload_data();
×
413
        this->tss_view->redo_search();
×
414
        this->tss_view->set_needs_update();
×
415
    }
416
}
417

418
void
419
textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
583✔
420
{
421
    auto iter = std::find_if(
422
        this->tss_files.begin(), this->tss_files.end(), [&lf](const auto& fvs) {
583✔
423
            return fvs->fvs_file == lf;
×
424
        });
1,166✔
425
    if (iter != this->tss_files.end()) {
583✔
426
        this->tss_files.erase(iter);
×
427
        this->detach_observer(lf);
×
428
    }
429
    this->set_time_offset(false);
583✔
430
    if (!this->tss_files.empty()) {
583✔
431
        this->tss_files.front()->load_into(*this->tss_view);
×
432
    }
433
    this->tss_view->reload_data();
583✔
434
}
583✔
435

436
void
437
textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
695✔
438
{
439
    auto* lfo = new line_filter_observer(this->get_filters(), lf);
695✔
440
    lf->set_logline_observer(lfo);
695✔
441
    this->tss_files.emplace_back(std::make_shared<file_view_state>(lf));
695✔
442
}
695✔
443

444
void
445
textfile_sub_source::text_mark(const bookmark_type_t* bm,
10✔
446
                               vis_line_t line,
447
                               bool added)
448
{
449
    if (this->tss_files.empty()) {
10✔
450
        return;
×
451
    }
452

453
    auto& front = this->tss_files.front();
10✔
454
    auto lf = front->fvs_file;
10✔
455
    if (lf == nullptr) {
10✔
456
        return;
×
457
    }
458

459
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
10✔
460
    if (lfo == nullptr
10✔
461
        || line < 0_vl
10✔
462
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
20✔
463
    {
464
        return;
×
465
    }
466

467
    auto cl = lfo->lfo_filter_state.tfs_index[static_cast<int>(line)];
10✔
468
    if (added) {
10✔
469
        front->fvs_content_marks[bm].insert_once(cl);
8✔
470
    } else {
471
        front->fvs_content_marks[bm].erase(cl);
2✔
472
    }
473
}
10✔
474

475
void
476
textfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
16✔
477
{
478
    if (this->tss_files.empty()) {
16✔
479
        return;
14✔
480
    }
481

482
    auto& front = this->tss_files.front();
2✔
483
    front->fvs_content_marks[bm].clear();
2✔
484
}
485

486
void
487
textfile_sub_source::text_update_marks(vis_bookmarks& bm)
833✔
488
{
489
    if (this->tss_files.empty()) {
833✔
490
        return;
728✔
491
    }
492

493
    auto& front = this->tss_files.front();
105✔
494
    auto lf = front->fvs_file;
105✔
495
    if (lf == nullptr) {
105✔
496
        return;
×
497
    }
498

499
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
105✔
500
    if (lfo == nullptr) {
105✔
501
        return;
×
502
    }
503

504
    static const bookmark_type_t* MARK_TYPES[] = {
505
        &textview_curses::BM_USER,
506
        &textview_curses::BM_STICKY,
507
    };
508

509
    for (const auto* bmt : MARK_TYPES) {
315✔
510
        bm[bmt].clear();
210✔
511
        if (front->fvs_content_marks[bmt].empty()) {
210✔
512
            continue;
205✔
513
        }
514

515
        auto& tfs = lfo->lfo_filter_state.tfs_index;
5✔
516
        for (uint32_t vl = 0; vl < tfs.size(); ++vl) {
49✔
517
            auto cl = tfs[vl];
44✔
518
            if (front->fvs_content_marks[bmt].bv_tree.count(cl) > 0) {
44✔
519
                bm[bmt].insert_once(vis_line_t(vl));
5✔
520
            }
521
        }
522
    }
523

524
    if (lf->has_line_metadata()) {
105✔
525
        auto& bm_errors = bm[&textview_curses::BM_ERRORS];
23✔
526
        auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
23✔
527

528
        bm_errors.clear();
23✔
529
        bm_warnings.clear();
23✔
530

531
        auto& tfs = lfo->lfo_filter_state.tfs_index;
23✔
532
        for (uint32_t vl = 0; vl < tfs.size(); ++vl) {
89✔
533
            auto ll = lf->begin() + tfs[vl];
66✔
534
            switch (ll->get_msg_level()) {
66✔
535
                case log_level_t::LEVEL_FATAL:
9✔
536
                case log_level_t::LEVEL_CRITICAL:
537
                case log_level_t::LEVEL_ERROR:
538
                    bm_errors.insert_once(vis_line_t(vl));
9✔
539
                    break;
9✔
540
                case log_level_t::LEVEL_WARNING:
×
541
                    bm_warnings.insert_once(vis_line_t(vl));
×
542
                    break;
×
543
                default:
57✔
544
                    break;
57✔
545
            }
546
        }
547
    }
548
}
105✔
549

550
void
551
textfile_sub_source::text_filters_changed()
11✔
552
{
553
    auto lf = this->current_file();
11✔
554
    if (lf == nullptr || lf->get_text_format() == text_format_t::TF_BINARY) {
11✔
555
        return;
7✔
556
    }
557

558
    auto* lfo = (line_filter_observer*) lf->get_logline_observer();
4✔
559
    uint32_t filter_in_mask, filter_out_mask;
560

561
    lfo->clear_deleted_filter_state();
4✔
562
    lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
4✔
563

564
    this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
4✔
565
    lfo->lfo_filter_state.tfs_index.clear();
4✔
566
    for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
28✔
567
        if (this->tss_apply_filters) {
24✔
568
            if (lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
24✔
569
                continue;
6✔
570
            }
571
            if (lf->has_line_metadata()) {
18✔
572
                auto ll = lf->begin() + lpc;
×
573
                if (ll->get_timeval() < this->ttt_min_row_time) {
×
574
                    continue;
×
575
                }
576
                if (this->ttt_max_row_time < ll->get_timeval()) {
×
577
                    continue;
×
578
                }
579
            }
580
        }
581
        lfo->lfo_filter_state.tfs_index.push_back(lpc);
18✔
582
    }
583

584
    this->tss_view->reload_data();
4✔
585
    this->tss_view->redo_search();
4✔
586

587
    auto iter = std::lower_bound(lfo->lfo_filter_state.tfs_index.begin(),
4✔
588
                                 lfo->lfo_filter_state.tfs_index.end(),
589
                                 this->tss_content_line);
4✔
590
    auto vl = vis_line_t(
591
        std::distance(lfo->lfo_filter_state.tfs_index.begin(), iter));
8✔
592
    this->tss_view->set_selection(vl);
4✔
593
}
11✔
594

595
void
596
textfile_sub_source::scroll_invoked(textview_curses* tc)
871✔
597
{
598
    const auto curr_iter = this->current_file_state();
871✔
599
    if (curr_iter == this->tss_files.end()
871✔
600
        || (*curr_iter)->fvs_file->get_text_format()
1,157✔
601
            == text_format_t::TF_BINARY)
1,157✔
602
    {
603
        return;
752✔
604
    }
605

606
    const auto& lf = (*curr_iter)->fvs_file;
141✔
607
    if (this->tss_view_mode == view_mode::rendered
282✔
608
        && (*curr_iter)->fvs_text_source)
141✔
609
    {
610
        return;
22✔
611
    }
612

613
    auto line = tc->get_selection();
119✔
614
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
119✔
615
    if (!line || lfo == nullptr || line < 0_vl
238✔
616
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
238✔
617
    {
618
        return;
×
619
    }
620

621
    this->tss_content_line = lfo->lfo_filter_state.tfs_index[line.value()];
119✔
622
}
623

624
int
625
textfile_sub_source::get_filtered_count() const
346✔
626
{
627
    const auto curr_iter = this->current_file_state();
346✔
628
    int retval = 0;
346✔
629

630
    if (curr_iter != this->tss_files.end()) {
346✔
631
        if (this->tss_view_mode == view_mode::raw
684✔
632
            || !(*curr_iter)->fvs_text_source)
342✔
633
        {
634
            const auto& lf = (*curr_iter)->fvs_file;
287✔
635
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
287✔
636
            retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
287✔
637
        }
638
    }
639
    return retval;
346✔
640
}
641

642
int
643
textfile_sub_source::get_filtered_count_for(size_t filter_index) const
×
644
{
645
    std::shared_ptr<logfile> lf = this->current_file();
×
646

647
    if (lf == nullptr) {
×
648
        return 0;
×
649
    }
650

651
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
652
    return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
×
653
}
654

655
std::optional<text_format_t>
656
textfile_sub_source::get_text_format() const
1,676✔
657
{
658
    if (this->tss_files.empty()) {
1,676✔
659
        return text_format_t::TF_PLAINTEXT;
×
660
    }
661

662
    return this->tss_files.front()->fvs_file->get_text_format();
1,676✔
663
}
664

665
static attr_line_t
666
to_display(const std::shared_ptr<logfile>& lf)
22✔
667
{
668
    const auto& loo = lf->get_open_options();
22✔
669
    attr_line_t retval;
22✔
670

671
    if (loo.loo_piper) {
22✔
672
        if (!lf->get_open_options().loo_piper->is_finished()) {
4✔
673
            retval.append("\u21bb "_list_glyph);
×
674
        }
675
    } else if (loo.loo_child_poller && loo.loo_child_poller->is_alive()) {
18✔
676
        retval.append("\u21bb "_list_glyph);
×
677
    }
678
    retval.append(lf->get_unique_path());
22✔
679

680
    return retval;
22✔
681
}
×
682

683
void
684
textfile_sub_source::text_crumbs_for_line(
11✔
685
    int line, std::vector<breadcrumb::crumb>& crumbs)
686
{
687
    text_sub_source::text_crumbs_for_line(line, crumbs);
11✔
688

689
    if (this->empty()) {
11✔
690
        return;
×
691
    }
692

693
    const auto curr_iter = this->current_file_state();
11✔
694
    const auto& lf = (*curr_iter)->fvs_file;
11✔
695
    crumbs.emplace_back(
11✔
696
        lf->get_unique_path(),
11✔
697
        to_display(lf),
11✔
698
        [this]() {
×
699
            return this->tss_files | lnav::itertools::map([](const auto& lf) {
22✔
700
                       return breadcrumb::possibility{
701
                           lf->fvs_file->get_path_for_key(),
11✔
702
                           to_display(lf->fvs_file),
11✔
703
                       };
22✔
704
                   });
22✔
705
        },
706
        [this](const auto& key) {
11✔
707
            auto lf_opt = this->tss_files
×
708
                | lnav::itertools::map(
×
709
                              [](const auto& x) { return x->fvs_file; })
×
710
                | lnav::itertools::find_if([&key](const auto& elem) {
×
711
                              return key.template get<std::string>()
712
                                  == elem->get_path_for_key();
×
713
                          })
714
                | lnav::itertools::deref();
×
715

716
            if (!lf_opt) {
×
717
                return;
×
718
            }
719

720
            this->to_front(lf_opt.value());
×
721
            this->tss_view->reload_data();
×
722
        });
×
723
    if (lf->size() == 0) {
11✔
724
        return;
×
725
    }
726

727
    if (lf->has_line_metadata()) {
11✔
728
        auto* lfo
729
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
2✔
730
        if (line < 0
2✔
731
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
2✔
732
        {
733
            return;
×
734
        }
735
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
2✔
736
        char ts[64];
737

738
        sql_strftime(ts, sizeof(ts), ll_iter->get_timeval(), 'T');
2✔
739

740
        crumbs.emplace_back(
2✔
741
            std::string(ts),
4✔
742
            []() -> std::vector<breadcrumb::possibility> { return {}; },
2✔
743
            [](const auto& key) {});
2✔
744
    }
745

746
    if (this->tss_view_mode == view_mode::rendered
22✔
747
        && (*curr_iter)->fvs_text_source)
11✔
748
    {
749
        (*curr_iter)->fvs_text_source->text_crumbs_for_line(line, crumbs);
2✔
750
    } else if ((*curr_iter)->fvs_metadata.m_sections_tree.empty()) {
9✔
751
    } else {
752
        auto* lfo
753
            = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
6✔
754
        if (line < 0
6✔
755
            || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
6✔
756
        {
757
            return;
×
758
        }
759
        auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
6✔
760
        auto ll_next_iter = ll_iter + 1;
6✔
761
        auto end_offset = (ll_next_iter == lf->end())
6✔
762
            ? lf->get_index_size() - 1
12✔
763
            : ll_next_iter->get_offset() - 1;
6✔
764
        const auto initial_size = crumbs.size();
6✔
765

766
        (*curr_iter)
6✔
767
            ->fvs_metadata.m_sections_tree.visit_overlapping(
6✔
768
                lf->get_line_content_offset(ll_iter),
6✔
769
                end_offset,
770
                [&crumbs,
6✔
771
                 initial_size,
772
                 meta = &(*curr_iter)->fvs_metadata,
6✔
773
                 this,
774
                 lf](const auto& iv) {
775
                    auto path = crumbs | lnav::itertools::skip(initial_size)
24✔
776
                        | lnav::itertools::map(&breadcrumb::crumb::c_key)
16✔
777
                        | lnav::itertools::append(iv.value);
8✔
778
                    auto curr_node = lnav::document::hier_node::lookup_path(
8✔
779
                        meta->m_sections_root.get(), path);
8✔
780
                    crumbs.emplace_back(
16✔
781
                        iv.value,
8✔
782
                        [meta, path]() {
8✔
783
                            return meta->possibility_provider(path);
8✔
784
                        },
785
                        [this, curr_node, path, lf](const auto& key) {
16✔
786
                            if (!curr_node) {
×
787
                                return;
×
788
                            }
789
                            auto* parent_node = curr_node.value()->hn_parent;
×
790
                            if (parent_node == nullptr) {
×
791
                                return;
×
792
                            }
793
                            key.match(
×
794
                                [this, parent_node](const std::string& str) {
×
795
                                    auto sib_iter
796
                                        = parent_node->hn_named_children.find(
×
797
                                            str);
798
                                    if (sib_iter
×
799
                                        == parent_node->hn_named_children
×
800
                                               .end()) {
×
801
                                        return;
×
802
                                    }
803
                                    this->set_top_from_off(
×
804
                                        sib_iter->second->hn_start);
×
805
                                },
806
                                [this, parent_node](size_t index) {
×
807
                                    if (index
×
808
                                        >= parent_node->hn_children.size()) {
×
809
                                        return;
×
810
                                    }
811
                                    auto sib
812
                                        = parent_node->hn_children[index].get();
×
813
                                    this->set_top_from_off(sib->hn_start);
×
814
                                });
815
                        });
816
                    if (curr_node
8✔
817
                        && curr_node.value()->hn_parent->hn_children.size()
16✔
818
                            != curr_node.value()
8✔
819
                                   ->hn_parent->hn_named_children.size())
8✔
820
                    {
821
                        auto node = lnav::document::hier_node::lookup_path(
1✔
822
                            meta->m_sections_root.get(), path);
1✔
823

824
                        crumbs.back().c_expected_input
1✔
825
                            = curr_node.value()
2✔
826
                                  ->hn_parent->hn_named_children.empty()
1✔
827
                            ? breadcrumb::crumb::expected_input_t::index
1✔
828
                            : breadcrumb::crumb::expected_input_t::
829
                                  index_or_exact;
830
                        crumbs.back().with_possible_range(
2✔
831
                            node | lnav::itertools::map([](const auto hn) {
1✔
832
                                return hn->hn_parent->hn_children.size();
1✔
833
                            })
834
                            | lnav::itertools::unwrap_or(size_t{0}));
2✔
835
                    }
836
                });
8✔
837

838
        auto path = crumbs | lnav::itertools::skip(initial_size)
12✔
839
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
12✔
840
        auto node = lnav::document::hier_node::lookup_path(
6✔
841
            (*curr_iter)->fvs_metadata.m_sections_root.get(), path);
6✔
842

843
        if (node && !node.value()->hn_children.empty()) {
6✔
844
            auto poss_provider = [curr_node = node.value()]() {
2✔
845
                std::vector<breadcrumb::possibility> retval;
2✔
846
                for (const auto& child : curr_node->hn_named_children) {
14✔
847
                    retval.emplace_back(child.first);
12✔
848
                }
849
                return retval;
2✔
850
            };
851
            auto path_performer = [this, curr_node = node.value()](
4✔
852
                                      const breadcrumb::crumb::key_t& value) {
853
                value.match(
×
854
                    [this, curr_node](const std::string& str) {
×
855
                        auto child_iter
856
                            = curr_node->hn_named_children.find(str);
×
857
                        if (child_iter != curr_node->hn_named_children.end()) {
×
858
                            this->set_top_from_off(
×
859
                                child_iter->second->hn_start);
×
860
                        }
861
                    },
×
862
                    [this, curr_node](size_t index) {
×
863
                        if (index >= curr_node->hn_children.size()) {
×
864
                            return;
×
865
                        }
866
                        auto* child = curr_node->hn_children[index].get();
×
867
                        this->set_top_from_off(child->hn_start);
×
868
                    });
869
            };
2✔
870
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
2✔
871
            crumbs.back().c_expected_input
2✔
872
                = node.value()->hn_named_children.empty()
4✔
873
                ? breadcrumb::crumb::expected_input_t::index
2✔
874
                : breadcrumb::crumb::expected_input_t::index_or_exact;
875
        }
876
    }
6✔
877
}
878

879
textfile_sub_source::rescan_result_t
880
textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
4,933✔
881
                                  std::optional<ui_clock::time_point> deadline)
882
{
883
    static auto& lnav_db = injector::get<auto_sqlite3&>();
4,933✔
884

885
    file_iterator iter;
4,933✔
886
    rescan_result_t retval;
4,933✔
887
    size_t files_scanned = 0;
4,933✔
888

889
    if (this->tss_view == nullptr || this->tss_view->is_paused()) {
4,933✔
890
        return retval;
124✔
891
    }
892

893
    auto last_aborted = std::exchange(this->tss_last_scan_aborted, false);
4,809✔
894

895
    std::vector<std::shared_ptr<logfile>> closed_files;
4,809✔
896
    for (iter = this->tss_files.begin(); iter != this->tss_files.end();) {
6,096✔
897
        if (deadline && files_scanned > 0 && ui_clock::now() > deadline.value())
1,287✔
898
        {
899
            log_info("rescan_files() deadline reached, breaking...");
×
900
            retval.rr_scan_completed = false;
×
901
            this->tss_last_scan_aborted = true;
×
902
            break;
×
903
        }
904

905
        std::shared_ptr<logfile> lf = (*iter)->fvs_file;
1,287✔
906

907
        if (lf->is_closed()) {
1,287✔
908
            iter = this->tss_files.erase(iter);
112✔
909
            this->detach_observer(lf);
112✔
910
            closed_files.emplace_back(lf);
112✔
911
            retval.rr_rescan_needed = true;
112✔
912
            continue;
112✔
913
        }
914

915
        if (last_aborted && lf->size() > 0) {
1,175✔
916
            retval.rr_scan_completed = false;
×
917
            ++iter;
×
918
            continue;
×
919
        }
920
        files_scanned += 1;
1,175✔
921

922
        try {
923
            const auto& st = lf->get_stat();
1,175✔
924
            uint32_t old_size = lf->size();
1,175✔
925
            auto new_text_data = lf->rebuild_index(deadline);
1,175✔
926

927
            if (lf->get_format() != nullptr) {
1,175✔
928
                iter = this->tss_files.erase(iter);
583✔
929
                this->detach_observer(lf);
583✔
930
                callback.promote_file(lf);
583✔
931
                continue;
646✔
932
            }
933

934
            bool new_data = false;
592✔
935
            switch (new_text_data) {
592✔
936
                case logfile::rebuild_result_t::NEW_LINES:
99✔
937
                case logfile::rebuild_result_t::NEW_ORDER:
938
                    new_data = true;
99✔
939
                    retval.rr_new_data += 1;
99✔
940
                    break;
99✔
941
                case logfile::rebuild_result_t::NO_NEW_LINES:
493✔
942
                    this->move_to_init_location(iter);
493✔
943
                    break;
493✔
944
                default:
×
945
                    break;
×
946
            }
947
            callback.scanned_file(lf);
592✔
948

949
            if (lf->is_indexing()
592✔
950
                && lf->get_text_format() != text_format_t::TF_BINARY)
592✔
951
            {
952
                if (!new_data) {
585✔
953
                    // Only invalidate the meta if the file is small, or we
954
                    // found some meta previously.
955
                    if ((st.st_mtime != (*iter)->fvs_mtime
488✔
956
                         || st.st_size != (*iter)->fvs_file_size
475✔
957
                         || lf->get_index_size()
475✔
958
                             != (*iter)->fvs_file_indexed_size)
475✔
959
                        && (st.st_size < 10 * 1024
963✔
960
                            || (*iter)->fvs_file_size == 0
×
961
                            || !(*iter)->fvs_metadata.m_sections_tree.empty()))
×
962
                    {
963
                        log_debug(
13✔
964
                            "text file has changed, invalidating metadata.  "
965
                            "old: {mtime: %ld size: %lld isize: %lld}, new: "
966
                            "{mtime: %ld size: %lld isize: %lld}",
967
                            (*iter)->fvs_mtime,
968
                            (*iter)->fvs_file_size,
969
                            (*iter)->fvs_file_indexed_size,
970
                            st.st_mtime,
971
                            st.st_size,
972
                            lf->get_index_size());
973
                        (*iter)->fvs_metadata = {};
13✔
974
                        (*iter)->fvs_error.clear();
13✔
975
                    }
976
                }
977

978
                if (!(*iter)->fvs_metadata.m_sections_root
585✔
979
                    && (*iter)->fvs_error.empty())
585✔
980
                {
981
                    auto read_res
982
                        = lf->read_file(logfile::read_format_t::with_framing);
109✔
983

984
                    if (read_res.isOk()) {
109✔
985
                        auto read_file_res = read_res.unwrap();
109✔
986
                        auto tf_opt = lf->get_text_format();
109✔
987

988
                        if (!read_file_res.rfr_range.fr_metadata.m_valid_utf
218✔
989
                            || !tf_opt)
109✔
990
                        {
991
                            log_error(
18✔
992
                                "%s: file has no text format, skipping meta "
993
                                "discovery",
994
                                lf->get_path_for_key().c_str());
995
                            (*iter)->fvs_mtime = st.st_mtime;
18✔
996
                            (*iter)->fvs_file_size = st.st_size;
18✔
997
                            (*iter)->fvs_file_indexed_size
18✔
998
                                = lf->get_index_size();
18✔
999
                            (*iter)->fvs_error = "skipping meta discovery";
18✔
1000
                        } else {
1001
                            auto content
1002
                                = attr_line_t(read_file_res.rfr_content);
91✔
1003

1004
                            log_info("generating metadata for: %s (size=%zu)",
91✔
1005
                                     lf->get_path_for_key().c_str(),
1006
                                     content.length());
1007
                            scrub_ansi_string(content.get_string(),
91✔
1008
                                              &content.get_attrs());
91✔
1009

1010
                            auto text_meta = extract_text_meta(
1011
                                content.get_string(), tf_opt.value());
91✔
1012
                            if (text_meta) {
91✔
1013
                                lf->set_filename(text_meta->tfm_filename);
3✔
1014
                                lf->set_include_in_session(true);
3✔
1015
                                callback.renamed_file(lf);
3✔
1016
                            }
1017

1018
                            (*iter)->fvs_mtime = st.st_mtime;
91✔
1019
                            (*iter)->fvs_file_size = st.st_size;
91✔
1020
                            (*iter)->fvs_file_indexed_size
91✔
1021
                                = lf->get_index_size();
91✔
1022
                            (*iter)->fvs_metadata
91✔
1023
                                = lnav::document::discover(content)
91✔
1024
                                      .with_text_format(tf_opt.value())
91✔
1025
                                      .perform();
182✔
1026
                            log_info("  metadata indents size: %zu",
91✔
1027
                                     (*iter)->fvs_metadata.m_indents.size());
1028
                        }
91✔
1029
                    } else {
109✔
1030
                        auto errmsg = read_res.unwrapErr();
×
1031
                        log_error(
×
1032
                            "%s: unable to read file for meta discover -- %s",
1033
                            lf->get_path_for_key().c_str(),
1034
                            errmsg.c_str());
1035
                        (*iter)->fvs_mtime = st.st_mtime;
×
1036
                        (*iter)->fvs_file_size = st.st_size;
×
1037
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1038
                        (*iter)->fvs_error = errmsg;
×
1039
                    }
1040
                }
109✔
1041
            }
1042

1043
            uint32_t filter_in_mask, filter_out_mask;
1044

1045
            this->get_filters().get_enabled_mask(filter_in_mask,
592✔
1046
                                                 filter_out_mask);
1047
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
592✔
1048
            for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
4,857✔
1049
                if (this->tss_apply_filters
8,530✔
1050
                    && lfo->excluded(filter_in_mask, filter_out_mask, lpc))
4,265✔
1051
                {
1052
                    continue;
×
1053
                }
1054
                lfo->lfo_filter_state.tfs_index.push_back(lpc);
4,265✔
1055
            }
1056

1057
            if (lf->get_text_format() == text_format_t::TF_MARKDOWN) {
592✔
1058
                if ((*iter)->fvs_text_source) {
67✔
1059
                    if ((*iter)->fvs_file_size == st.st_size
53✔
1060
                        && (*iter)->fvs_file_indexed_size
106✔
1061
                            == lf->get_index_size()
53✔
1062
                        && (*iter)->fvs_mtime == st.st_mtime)
106✔
1063
                    {
1064
                        ++iter;
52✔
1065
                        continue;
52✔
1066
                    }
1067
                    log_info("markdown file has been updated, re-rendering: %s",
1✔
1068
                             lf->get_path_for_key().c_str());
1069
                    (*iter)->fvs_text_source = nullptr;
1✔
1070
                }
1071

1072
                auto read_res = lf->read_file(logfile::read_format_t::plain);
15✔
1073
                if (read_res.isOk()) {
15✔
1074
                    auto read_file_res = read_res.unwrap();
15✔
1075
                    auto md_file = md4cpp::parse_file(
15✔
1076
                        lf->get_filename(), read_file_res.rfr_content);
1077
                    log_info("%s: rendering markdown content of size %zu",
15✔
1078
                             lf->get_basename().c_str(),
1079
                             read_file_res.rfr_content.size());
1080
                    md2attr_line mdal;
15✔
1081

1082
                    mdal.with_source_path(lf->get_actual_path());
15✔
1083
                    if (this->tss_view->tc_interactive) {
15✔
1084
                        mdal.add_lnav_script_icons();
×
1085
                    }
1086
                    auto parse_res = md4cpp::parse(md_file.f_body, mdal);
15✔
1087

1088
                    (*iter)->fvs_mtime = st.st_mtime;
15✔
1089
                    (*iter)->fvs_file_indexed_size = lf->get_index_size();
15✔
1090
                    (*iter)->fvs_file_size = st.st_size;
15✔
1091
                    (*iter)->fvs_text_source
15✔
1092
                        = std::make_unique<plain_text_source>();
30✔
1093
                    (*iter)->fvs_text_source->set_text_format(
15✔
1094
                        lf->get_text_format());
1095
                    if (parse_res.isOk()) {
15✔
1096
                        auto& lf_meta = lf->get_embedded_metadata();
13✔
1097

1098
                        (*iter)->fvs_text_source->replace_with(
26✔
1099
                            parse_res.unwrap());
26✔
1100
                        if (!md_file.f_frontmatter.empty()) {
13✔
1101
                            lf_meta["net.daringfireball.markdown.frontmatter"]
10✔
1102
                                = {
1103
                                    md_file.f_frontmatter_format,
5✔
1104
                                    md_file.f_frontmatter.to_string(),
1105
                                };
10✔
1106
                        }
1107

1108
                        lnav::events::publish(
13✔
1109
                            lnav_db,
1110
                            lnav::events::file::format_detected{
39✔
1111
                                lf->get_filename(),
13✔
1112
                                fmt::to_string(lf->get_text_format().value_or(
26✔
1113
                                    text_format_t::TF_BINARY)),
26✔
1114
                            });
1115
                    } else {
1116
                        auto view_content
1117
                            = lnav::console::user_message::error(
4✔
1118
                                  "unable to parse markdown file")
1119
                                  .with_reason(parse_res.unwrapErr())
4✔
1120
                                  .to_attr_line();
2✔
1121
                        view_content.append("\n").append(
4✔
1122
                            attr_line_t::from_ansi_str(
4✔
1123
                                read_file_res.rfr_content.c_str()));
1124

1125
                        (*iter)->fvs_text_source->replace_with(view_content);
2✔
1126
                    }
2✔
1127
                    (*iter)->fvs_text_source->register_view(this->tss_view);
15✔
1128
                } else {
15✔
1129
                    log_error("unable to read markdown file: %s -- %s",
×
1130
                              lf->get_path_for_key().c_str(),
1131
                              read_res.unwrapErr().c_str());
1132
                }
1133
            } else if (file_needs_reformatting(lf) && !new_data) {
540✔
1134
                if ((*iter)->fvs_file_size == st.st_size
15✔
1135
                    && (*iter)->fvs_file_indexed_size == lf->get_index_size()
15✔
1136
                    && (*iter)->fvs_mtime == st.st_mtime
15✔
1137
                    && (!(*iter)->fvs_error.empty()
45✔
1138
                        || (*iter)->fvs_text_source != nullptr))
15✔
1139
                {
1140
                    ++iter;
11✔
1141
                    continue;
11✔
1142
                }
1143
                log_info("pretty file has been updated, re-rendering: %s",
4✔
1144
                         lf->get_path_for_key().c_str());
1145
                (*iter)->fvs_text_source = nullptr;
4✔
1146
                (*iter)->fvs_error.clear();
4✔
1147

1148
                auto read_res = lf->read_file(logfile::read_format_t::plain);
4✔
1149
                if (read_res.isOk()) {
4✔
1150
                    auto read_file_res = read_res.unwrap();
4✔
1151
                    if (read_file_res.rfr_range.fr_metadata.m_valid_utf) {
4✔
1152
                        auto orig_al = attr_line_t(read_file_res.rfr_content);
4✔
1153
                        scrub_ansi_string(orig_al.al_string, &orig_al.al_attrs);
4✔
1154
                        data_scanner ds(orig_al.al_string);
4✔
1155
                        pretty_printer pp(&ds, orig_al.al_attrs);
4✔
1156
                        attr_line_t pretty_al;
4✔
1157

1158
                        pp.append_to(pretty_al);
4✔
1159
                        (*iter)->fvs_mtime = st.st_mtime;
4✔
1160
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
4✔
1161
                        (*iter)->fvs_file_size = st.st_size;
4✔
1162
                        (*iter)->fvs_text_source
4✔
1163
                            = std::make_unique<plain_text_source>();
8✔
1164
                        (*iter)->fvs_text_source->set_text_format(
4✔
1165
                            lf->get_text_format());
1166
                        (*iter)->fvs_text_source->register_view(this->tss_view);
4✔
1167
                        (*iter)->fvs_text_source->replace_with_mutable(
4✔
1168
                            pretty_al, lf->get_text_format());
1169
                    } else {
4✔
1170
                        log_error(
×
1171
                            "unable to read file to pretty-print: %s -- file "
1172
                            "is not valid UTF-8",
1173
                            lf->get_path_for_key().c_str());
1174
                        (*iter)->fvs_mtime = st.st_mtime;
×
1175
                        (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1176
                        (*iter)->fvs_file_size = st.st_size;
×
1177
                        (*iter)->fvs_error = "file is not valid UTF-8";
×
1178
                    }
1179
                } else {
4✔
1180
                    auto errmsg = read_res.unwrapErr();
×
1181
                    log_error("unable to read file to pretty-print: %s -- %s",
×
1182
                              lf->get_path_for_key().c_str(),
1183
                              errmsg.c_str());
1184
                    (*iter)->fvs_mtime = st.st_mtime;
×
1185
                    (*iter)->fvs_file_indexed_size = lf->get_index_size();
×
1186
                    (*iter)->fvs_file_size = st.st_size;
×
1187
                    (*iter)->fvs_error = errmsg;
×
1188
                }
1189
            }
4✔
1190
        } catch (const line_buffer::error& e) {
×
1191
            iter = this->tss_files.erase(iter);
×
1192
            lf->close();
×
1193
            this->detach_observer(lf);
×
1194
            closed_files.emplace_back(lf);
×
1195
            continue;
×
1196
        }
×
1197

1198
        ++iter;
529✔
1199
    }
1,287✔
1200
    if (!closed_files.empty()) {
4,809✔
1201
        callback.closed_files(closed_files);
108✔
1202
        if (!this->tss_files.empty()) {
108✔
1203
            this->tss_files.front()->load_into(*this->tss_view);
×
1204
        }
1205
        this->tss_view->set_needs_update();
108✔
1206
    }
1207

1208
    if (retval.rr_new_data) {
4,809✔
1209
        this->tss_view->search_new_data();
95✔
1210
    }
1211

1212
    return retval;
4,809✔
1213
}
4,827✔
1214

1215
void
1216
textfile_sub_source::set_top_from_off(file_off_t off)
×
1217
{
1218
    auto lf = this->current_file();
×
1219

1220
    lf->line_for_offset(off) | [this, lf](auto new_top_iter) {
×
1221
        auto* lfo = (line_filter_observer*) lf->get_logline_observer();
×
1222
        auto new_top_opt = lfo->lfo_filter_state.content_line_to_vis_line(
×
1223
            std::distance(lf->cbegin(), new_top_iter));
×
1224

1225
        if (new_top_opt) {
×
1226
            this->tss_view->set_selection(vis_line_t(new_top_opt.value()));
×
1227
            if (this->tss_view->is_selectable()) {
×
1228
                this->tss_view->set_top(
×
1229
                    this->tss_view->get_selection().value() - 2_vl, false);
×
1230
            }
1231
        }
1232
    };
1233
}
1234

1235
void
1236
textfile_sub_source::quiesce()
×
1237
{
1238
    for (auto& lf : this->tss_files) {
×
1239
        lf->fvs_file->quiesce();
×
1240
    }
1241
}
1242

1243
size_t
1244
textfile_sub_source::file_view_state::text_line_count(view_mode mode) const
22,480✔
1245
{
1246
    size_t retval = 0;
22,480✔
1247

1248
    if (mode == view_mode::raw || !this->fvs_text_source) {
22,480✔
1249
        const auto& lf = this->fvs_file;
12,413✔
1250
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
12,413✔
1251
            const auto fsize = lf->get_content_size();
47✔
1252
            retval = fsize / 16;
47✔
1253
            if (fsize % 16) {
47✔
1254
                retval += 1;
47✔
1255
            }
1256
        } else {
1257
            auto* lfo = (line_filter_observer*) lf->get_logline_observer();
12,366✔
1258
            if (lfo != nullptr) {
12,366✔
1259
                retval = lfo->lfo_filter_state.tfs_index.size();
12,366✔
1260
            }
1261
        }
1262
    } else {
1263
        retval = this->fvs_text_source->text_line_count();
10,067✔
1264
    }
1265

1266
    return retval;
22,480✔
1267
}
1268

1269
size_t
1270
textfile_sub_source::file_view_state::text_line_width(view_mode mode,
535✔
1271
                                                      textview_curses& tc) const
1272
{
1273
    size_t retval = 0;
535✔
1274
    if (mode == view_mode::raw || !this->fvs_text_source) {
535✔
1275
        const auto& lf = this->fvs_file;
438✔
1276
        if (lf->get_text_format() == text_format_t::TF_BINARY) {
438✔
1277
            retval = 88;
5✔
1278
        } else {
1279
            retval = lf->get_longest_line_length();
433✔
1280
        }
1281
    } else {
1282
        retval = this->fvs_text_source->text_line_width(tc);
97✔
1283
    }
1284
    return retval;
535✔
1285
}
1286

1287
std::optional<vis_line_t>
1288
textfile_sub_source::file_view_state::row_for_anchor(view_mode mode,
7✔
1289
                                                     const std::string& id)
1290
{
1291
    if (mode == view_mode::rendered && this->fvs_text_source) {
7✔
1292
        return this->fvs_text_source->row_for_anchor(id);
5✔
1293
    }
1294

1295
    if (!this->fvs_metadata.m_sections_root) {
2✔
1296
        return std::nullopt;
×
1297
    }
1298

1299
    const auto& lf = this->fvs_file;
2✔
1300
    const auto& meta = this->fvs_metadata;
2✔
1301
    std::optional<vis_line_t> retval;
2✔
1302

1303
    auto is_ptr = startswith(id, "#/");
2✔
1304
    if (is_ptr) {
2✔
1305
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
2✔
1306
        std::vector<lnav::document::section_key_t> path;
2✔
1307

1308
        while (!hier_sf.empty()) {
8✔
1309
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
6✔
1310
            auto scan_res
1311
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
6✔
1312
            if (scan_res && scan_res->range().empty()) {
6✔
1313
                path.emplace_back(scan_res->value());
2✔
1314
            } else {
1315
                stack_buf allocator;
4✔
1316
                path.emplace_back(
4✔
1317
                    json_ptr::decode(comp_pair.first, allocator).to_string());
8✔
1318
            }
4✔
1319
            hier_sf = comp_pair.second;
6✔
1320
        }
1321

1322
        auto lookup_res = lnav::document::hier_node::lookup_path(
2✔
1323
            meta.m_sections_root.get(), path);
2✔
1324
        if (lookup_res) {
2✔
1325
            auto ll_opt = lf->line_for_offset(lookup_res.value()->hn_start);
2✔
1326
            if (ll_opt != lf->end()) {
2✔
1327
                retval
1328
                    = vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
4✔
1329
            }
1330
        }
1331

1332
        return retval;
2✔
1333
    }
2✔
1334

1335
    lnav::document::hier_node::depth_first(
×
1336
        meta.m_sections_root.get(),
1337
        [lf, &id, &retval](const lnav::document::hier_node* node) {
×
1338
            for (const auto& child_pair : node->hn_named_children) {
×
1339
                const auto& child_anchor = to_anchor_string(child_pair.first);
×
1340

1341
                if (child_anchor != id) {
×
1342
                    continue;
×
1343
                }
1344

1345
                auto ll_opt = lf->line_for_offset(child_pair.second->hn_start);
×
1346
                if (ll_opt != lf->end()) {
×
1347
                    retval = vis_line_t(
×
1348
                        std::distance(lf->cbegin(), ll_opt.value()));
×
1349
                }
1350
                break;
×
1351
            }
1352
        });
×
1353

1354
    return retval;
×
1355
}
1356

1357
std::optional<vis_line_t>
1358
textfile_sub_source::row_for_anchor(const std::string& id)
6✔
1359
{
1360
    const auto curr_iter = this->current_file_state();
6✔
1361
    if (curr_iter == this->tss_files.end() || id.empty()) {
6✔
1362
        return std::nullopt;
×
1363
    }
1364

1365
    return (*curr_iter)->row_for_anchor(this->tss_view_mode, id);
6✔
1366
}
1367

1368
static void
1369
anchor_generator(std::unordered_set<std::string>& retval,
×
1370
                 std::vector<std::string>& comps,
1371
                 size_t& max_depth,
1372
                 lnav::document::hier_node* hn)
1373
{
1374
    if (hn->hn_named_children.empty()) {
×
1375
        if (hn->hn_children.empty()) {
×
1376
            if (retval.size() >= 250 || comps.empty()) {
×
1377
            } else if (comps.size() == 1) {
×
1378
                retval.emplace(text_anchors::to_anchor_string(comps.front()));
×
1379
            } else {
1380
                retval.emplace(
×
1381
                    fmt::format(FMT_STRING("#/{}"),
×
1382
                                fmt::join(comps.begin(), comps.end(), "/")));
×
1383
            }
1384
            max_depth = std::max(max_depth, comps.size());
×
1385
        } else {
1386
            int index = 0;
×
1387
            for (const auto& child : hn->hn_children) {
×
1388
                comps.emplace_back(fmt::to_string(index));
×
1389
                anchor_generator(retval, comps, max_depth, child.get());
×
1390
                comps.pop_back();
×
1391
            }
1392
        }
1393
    } else {
1394
        for (const auto& [child_name, child_node] : hn->hn_named_children) {
×
1395
            comps.emplace_back(child_name);
×
1396
            anchor_generator(retval, comps, max_depth, child_node);
×
1397
            comps.pop_back();
×
1398
        }
1399
        if (max_depth > 1) {
×
1400
            retval.emplace(
×
1401
                fmt::format(FMT_STRING("#/{}"),
×
1402
                            fmt::join(comps.begin(), comps.end(), "/")));
×
1403
        }
1404
    }
1405
}
1406

1407
std::unordered_set<std::string>
1408
textfile_sub_source::get_anchors()
×
1409
{
1410
    std::unordered_set<std::string> retval;
×
1411

1412
    const auto curr_iter = this->current_file_state();
×
1413
    if (curr_iter == this->tss_files.end()) {
×
1414
        return retval;
×
1415
    }
1416

1417
    if (this->tss_view_mode == view_mode::rendered
×
1418
        && (*curr_iter)->fvs_text_source)
×
1419
    {
1420
        return (*curr_iter)->fvs_text_source->get_anchors();
×
1421
    }
1422

1423
    const auto& meta = (*curr_iter)->fvs_metadata;
×
1424
    if (meta.m_sections_root == nullptr) {
×
1425
        return retval;
×
1426
    }
1427

1428
    std::vector<std::string> comps;
×
1429
    size_t max_depth = 0;
×
1430
    anchor_generator(retval, comps, max_depth, meta.m_sections_root.get());
×
1431

1432
    return retval;
×
1433
}
1434

1435
struct tfs_time_cmp {
1436
    bool operator()(int32_t lhs, const timeval& rhs) const
×
1437
    {
1438
        auto ll = this->ttc_logfile->begin() + this->ttc_index[lhs];
×
1439
        return ll->get_timeval() < rhs;
×
1440
    }
1441

1442
    logfile* ttc_logfile;
1443
    std::vector<uint32_t>& ttc_index;
1444
};
1445

1446
std::optional<vis_line_t>
1447
textfile_sub_source::row_for_time(timeval time_bucket)
×
1448
{
1449
    auto lf = this->current_file();
×
1450
    if (!lf || !lf->has_line_metadata()) {
×
1451
        return std::nullopt;
×
1452
    }
1453

1454
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
×
1455
    auto& tfs = lfo->lfo_filter_state.tfs_index;
×
1456
    auto lb = std::lower_bound(
×
1457
        tfs.begin(), tfs.end(), time_bucket, tfs_time_cmp{lf.get(), tfs});
×
1458
    if (lb != tfs.end()) {
×
1459
        return vis_line_t{(int) std::distance(tfs.begin(), lb)};
×
1460
    }
1461

1462
    return std::nullopt;
×
1463
}
1464

1465
std::optional<text_time_translator::row_info>
1466
textfile_sub_source::time_for_row(vis_line_t row)
4,881✔
1467
{
1468
    auto lf = this->current_file();
4,881✔
1469
    if (!lf || !lf->has_line_metadata()) {
4,881✔
1470
        return std::nullopt;
4,727✔
1471
    }
1472

1473
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
154✔
1474
    if (row < 0_vl || row >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
154✔
1475
        return std::nullopt;
43✔
1476
    }
1477
    auto row_id = lfo->lfo_filter_state.tfs_index[row];
111✔
1478
    auto ll_iter = lf->begin() + row_id;
111✔
1479
    return row_info{
222✔
1480
        ll_iter->get_timeval(),
1481
        row_id,
1482
    };
111✔
1483
}
4,881✔
1484

1485
static std::optional<vis_line_t>
1486
to_vis_line(const std::shared_ptr<logfile>& lf, file_off_t off)
3✔
1487
{
1488
    auto ll_opt = lf->line_for_offset(off);
3✔
1489
    if (ll_opt != lf->end()) {
3✔
1490
        return vis_line_t(std::distance(lf->cbegin(), ll_opt.value()));
6✔
1491
    }
1492

1493
    return std::nullopt;
×
1494
}
1495

1496
std::optional<vis_line_t>
1497
textfile_sub_source::adjacent_anchor(vis_line_t vl, direction dir)
3✔
1498
{
1499
    const auto curr_iter = this->current_file_state();
3✔
1500
    if (curr_iter == this->tss_files.end()) {
3✔
1501
        return std::nullopt;
×
1502
    }
1503

1504
    const auto& lf = (*curr_iter)->fvs_file;
3✔
1505
    log_debug("adjacent_anchor: %s:L%d:%s",
3✔
1506
              lf->get_path_for_key().c_str(),
1507
              (int) vl,
1508
              dir == text_anchors::direction::prev ? "prev" : "next");
1509
    if (this->tss_view_mode == view_mode::rendered
6✔
1510
        && (*curr_iter)->fvs_text_source)
3✔
1511
    {
1512
        return (*curr_iter)->fvs_text_source->adjacent_anchor(vl, dir);
×
1513
    }
1514

1515
    if (!(*curr_iter)->fvs_metadata.m_sections_root) {
3✔
1516
        log_debug("  no metadata available");
×
1517
        return std::nullopt;
×
1518
    }
1519

1520
    auto& md = (*curr_iter)->fvs_metadata;
3✔
1521
    const auto* lfo
1522
        = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1523
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()
3✔
1524
        || md.m_sections_root == nullptr)
3✔
1525
    {
1526
        return std::nullopt;
×
1527
    }
1528
    const auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
3✔
1529
    const auto line_offsets = lf->get_file_range(ll_iter, false);
3✔
1530
    log_debug(
3✔
1531
        "  range %lld:%zu", line_offsets.fr_offset, line_offsets.next_offset());
1532
    auto path_for_line
1533
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
3✔
1534

1535
    if (path_for_line.empty()) {
3✔
1536
        log_debug("  no path found");
×
1537
        const auto neighbors_res = md.m_sections_root->line_neighbors(vl);
×
1538
        if (!neighbors_res) {
×
1539
            return std::nullopt;
×
1540
        }
1541

1542
        switch (dir) {
×
1543
            case direction::prev: {
×
1544
                if (neighbors_res->cnr_previous) {
×
1545
                    return to_vis_line(
×
1546
                        lf, neighbors_res->cnr_previous.value()->hn_start);
×
1547
                }
1548
                break;
×
1549
            }
1550
            case direction::next: {
×
1551
                if (neighbors_res->cnr_next) {
×
1552
                    return to_vis_line(
×
1553
                        lf, neighbors_res->cnr_next.value()->hn_start);
×
1554
                }
1555
                if (!md.m_sections_root->hn_children.empty()) {
×
1556
                    return to_vis_line(
×
1557
                        lf, md.m_sections_root->hn_children[0]->hn_start);
×
1558
                }
1559
                break;
×
1560
            }
1561
        }
1562
        return std::nullopt;
×
1563
    }
1564

1565
    log_debug("  path for line: %s", fmt::to_string(path_for_line).c_str());
3✔
1566
    const auto last_key = std::move(path_for_line.back());
3✔
1567
    path_for_line.pop_back();
3✔
1568

1569
    const auto parent_opt = lnav::document::hier_node::lookup_path(
3✔
1570
        md.m_sections_root.get(), path_for_line);
3✔
1571
    if (!parent_opt) {
3✔
1572
        log_debug("  no parent for path: %s",
×
1573
                  fmt::to_string(path_for_line).c_str());
1574
        return std::nullopt;
×
1575
    }
1576
    const auto parent = parent_opt.value();
3✔
1577

1578
    const auto child_hn = parent->lookup_child(last_key);
3✔
1579
    if (!child_hn) {
3✔
1580
        // XXX "should not happen"
1581
        log_debug("  child not found");
×
1582
        return std::nullopt;
×
1583
    }
1584

1585
    auto neighbors_res = parent->child_neighbors(
9✔
1586
        child_hn.value(), line_offsets.next_offset() + 1);
3✔
1587
    if (!neighbors_res) {
3✔
1588
        log_debug("  no neighbors found");
×
1589
        return std::nullopt;
×
1590
    }
1591

1592
    log_debug("  neighbors p:%d n:%d",
3✔
1593
              neighbors_res->cnr_previous.has_value(),
1594
              neighbors_res->cnr_next.has_value());
1595
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
3✔
1596
        auto neighbor_sub
1597
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
3✔
1598
        if (neighbor_sub) {
3✔
1599
            neighbors_res->cnr_previous = neighbor_sub;
×
1600
        }
1601
    }
1602

1603
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
3✔
1604
        auto neighbor_sub
1605
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
3✔
1606
        if (neighbor_sub) {
3✔
1607
            neighbors_res->cnr_next = neighbor_sub;
3✔
1608
        }
1609
    }
1610

1611
    switch (dir) {
3✔
1612
        case direction::prev: {
1✔
1613
            if (neighbors_res->cnr_previous) {
1✔
1614
                return to_vis_line(
1✔
1615
                    lf, neighbors_res->cnr_previous.value()->hn_start);
1✔
1616
            }
1617
            break;
×
1618
        }
1619
        case direction::next: {
2✔
1620
            if (neighbors_res->cnr_next) {
2✔
1621
                return to_vis_line(lf,
2✔
1622
                                   neighbors_res->cnr_next.value()->hn_start);
2✔
1623
            }
1624
            break;
×
1625
        }
1626
    }
1627

1628
    return std::nullopt;
×
1629
}
3✔
1630

1631
std::optional<std::string>
1632
textfile_sub_source::anchor_for_row(vis_line_t vl)
12✔
1633
{
1634
    const auto curr_iter = this->current_file_state();
12✔
1635
    if (curr_iter == this->tss_files.end()) {
12✔
1636
        return std::nullopt;
×
1637
    }
1638

1639
    if (this->tss_view_mode == view_mode::rendered
24✔
1640
        && (*curr_iter)->fvs_text_source)
12✔
1641
    {
1642
        return (*curr_iter)->fvs_text_source->anchor_for_row(vl);
3✔
1643
    }
1644

1645
    if (!(*curr_iter)->fvs_metadata.m_sections_root) {
9✔
1646
        return std::nullopt;
×
1647
    }
1648

1649
    const auto& lf = (*curr_iter)->fvs_file;
9✔
1650
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
9✔
1651
    if (vl >= (ssize_t) lfo->lfo_filter_state.tfs_index.size()) {
9✔
1652
        return std::nullopt;
×
1653
    }
1654
    auto& md = (*curr_iter)->fvs_metadata;
9✔
1655
    auto ll_iter = lf->begin() + lfo->lfo_filter_state.tfs_index[vl];
9✔
1656
    auto line_offsets = lf->get_file_range(ll_iter, false);
9✔
1657
    auto path_for_line
1658
        = md.path_for_range(line_offsets.fr_offset, line_offsets.next_offset());
9✔
1659

1660
    if (path_for_line.empty()) {
9✔
1661
        return std::nullopt;
5✔
1662
    }
1663

1664
    if ((path_for_line.size() == 1
4✔
1665
         || md.m_text_format == text_format_t::TF_MARKDOWN)
2✔
1666
        && path_for_line.back().is<std::string>())
6✔
1667
    {
1668
        return text_anchors::to_anchor_string(
2✔
1669
            path_for_line.back().get<std::string>());
2✔
1670
    }
1671

1672
    auto comps
1673
        = path_for_line | lnav::itertools::map([](const auto& elem) {
4✔
1674
              return elem.match(
1675
                  [](const std::string& str) {
×
1676
                      stack_buf allocator;
5✔
1677
                      return json_ptr::encode(str, allocator).to_string();
10✔
1678
                  },
5✔
1679
                  [](size_t index) { return fmt::to_string(index); });
13✔
1680
          });
2✔
1681

1682
    return fmt::format(FMT_STRING("#/{}"),
8✔
1683
                       fmt::join(comps.begin(), comps.end(), "/"));
4✔
1684
}
9✔
1685

1686
bool
1687
textfile_sub_source::to_front(const std::string& filename)
1✔
1688
{
1689
    auto lf_opt = this->tss_files
1✔
1690
        | lnav::itertools::find_if([&filename](const auto& elem) {
1✔
1691
                      return elem->fvs_file->get_filename() == filename;
1✔
1692
                  });
1✔
1693
    if (!lf_opt) {
1✔
1694
        return false;
1✔
1695
    }
1696

1697
    this->to_front((*lf_opt.value())->fvs_file);
×
1698

1699
    return true;
×
1700
}
1701

1702
logline*
1703
textfile_sub_source::text_accel_get_line(vis_line_t vl)
3✔
1704
{
1705
    auto lf = this->current_file();
3✔
1706
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
3✔
1707
    return (lf->begin() + lfo->lfo_filter_state.tfs_index[vl]).base();
3✔
1708
}
3✔
1709

1710
void
1711
textfile_sub_source::set_view_mode(view_mode vm)
2✔
1712
{
1713
    this->tss_view_mode = vm;
2✔
1714
    this->tss_view->set_needs_update();
2✔
1715
}
2✔
1716

1717
textfile_sub_source::view_mode
1718
textfile_sub_source::get_effective_view_mode() const
342✔
1719
{
1720
    auto retval = view_mode::raw;
342✔
1721

1722
    const auto curr_iter = this->current_file_state();
342✔
1723
    if (curr_iter != this->tss_files.end()) {
342✔
1724
        if (this->tss_view_mode == view_mode::rendered
684✔
1725
            && (*curr_iter)->fvs_text_source)
342✔
1726
        {
1727
            retval = view_mode::rendered;
11✔
1728
        }
1729
    }
1730

1731
    return retval;
342✔
1732
}
1733

1734
void
1735
textfile_sub_source::move_to_init_location(file_iterator& iter)
493✔
1736
{
1737
    if ((*iter)->fvs_consumed_init_location) {
493✔
1738
        return;
382✔
1739
    }
1740

1741
    auto& lf = (*iter)->fvs_file;
111✔
1742
    std::optional<vis_line_t> new_sel_opt;
111✔
1743
    require(lf->get_open_options().loo_init_location.valid());
111✔
1744
    lf->get_open_options().loo_init_location.match(
111✔
1745
        [this, &new_sel_opt, &lf](default_for_text_format def) {
×
1746
            if (!this->tss_apply_default_init_location) {
108✔
1747
                return;
108✔
1748
            }
1749
            auto tf = lf->get_text_format().value_or(text_format_t::TF_BINARY);
×
1750
            switch (tf) {
×
1751
                case text_format_t::TF_PLAINTEXT:
×
1752
                case text_format_t::TF_LOG: {
1753
                    log_info("file open request to tail");
×
1754
                    auto inner_height = lf->size();
×
1755
                    if (inner_height > 0) {
×
1756
                        new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1757
                    }
1758
                    break;
×
1759
                }
1760
                default:
×
1761
                    log_info("file open is %s, moving to top",
×
1762
                             fmt::to_string(tf).c_str());
1763
                    new_sel_opt = 0_vl;
×
1764
                    break;
×
1765
            }
1766
        },
1767
        [&new_sel_opt, &lf](file_location_tail tail) {
×
1768
            log_info("file open request to tail");
×
1769
            auto inner_height = lf->size();
×
1770
            if (inner_height > 0) {
×
1771
                new_sel_opt = vis_line_t(inner_height) - 1_vl;
×
1772
            }
1773
        },
×
1774
        [this, &new_sel_opt, &iter](int vl) {
×
1775
            log_info("file open request to jump to line: %d", vl);
2✔
1776
            auto height = (*iter)->text_line_count(this->tss_view_mode);
2✔
1777
            if (vl < 0) {
2✔
1778
                vl += height;
2✔
1779
                if (vl < 0) {
2✔
1780
                    vl = 0;
1✔
1781
                }
1782
            }
1783
            if (vl < height) {
2✔
1784
                new_sel_opt = vis_line_t(vl);
2✔
1785
            }
1786
        },
2✔
1787
        [this, &new_sel_opt, &iter](const std::string& loc) {
111✔
1788
            log_info("file open request to jump to anchor: %s", loc.c_str());
1✔
1789
            new_sel_opt = (*iter)->row_for_anchor(this->tss_view_mode, loc);
1✔
1790
        });
1✔
1791

1792
    if (new_sel_opt) {
111✔
1793
        log_info("%s", fmt::to_string(lf->get_filename()).c_str());
3✔
1794
        log_info("  setting requested selection: %d",
3✔
1795
                 (int) new_sel_opt.value());
1796
        (*iter)->fvs_selection = new_sel_opt;
3✔
1797
        log_info("  actual top is now: %d", (int) (*iter)->fvs_top);
3✔
1798
        log_info("  actual selection is now: %d",
3✔
1799
                 (int) (*iter)->fvs_selection.value());
1800

1801
        if (this->current_file() == lf) {
3✔
1802
            this->tss_view->set_selection((*iter)->fvs_selection.value());
3✔
1803
        }
1804
    }
1805
    (*iter)->fvs_consumed_init_location = true;
111✔
1806
}
1807

1808
textfile_header_overlay::textfile_header_overlay(textfile_sub_source* src,
712✔
1809
                                                 text_sub_source* log_src)
712✔
1810
    : tho_src(src), tho_log_src(log_src)
712✔
1811
{
1812
}
712✔
1813

1814
bool
1815
textfile_header_overlay::list_static_overlay(const listview_curses& lv,
8,898✔
1816
                                             media_t media,
1817
                                             int y,
1818
                                             int bottom,
1819
                                             attr_line_t& value_out)
1820
{
1821
    if (media == media_t::display) {
8,898✔
1822
        const std::vector<attr_line_t>* lines = nullptr;
8,828✔
1823
        auto curr_file = this->tho_src->current_file();
8,828✔
1824
        if (curr_file == nullptr) {
8,828✔
1825
            if (this->tho_log_src->text_line_count() == 0) {
×
1826
                lines = lnav::messages::view::no_files();
×
1827
            } else {
1828
                lines = lnav::messages::view::only_log_files();
×
1829
            }
1830
        } else if (!curr_file->get_notes().empty()) {
8,828✔
1831
            this->tho_static_lines = curr_file->get_notes()
×
1832
                                         .values()
×
1833
                                         .front()
×
1834
                                         .to_attr_line()
×
1835
                                         .split_lines();
×
1836
            lines = &this->tho_static_lines;
×
1837
        } else if (curr_file->size() == 0) {
8,828✔
1838
            lines = lnav::messages::view::empty_file();
×
1839
        } else if (this->tho_src->text_line_count() == 0) {
8,828✔
1840
            hasher h;
×
1841
            this->tho_src->update_filter_hash_state(h);
×
1842
            auto curr_state = h.to_array();
×
1843
            if (this->tho_static_lines.empty()
×
1844
                || curr_state != this->tho_filter_state)
×
1845
            {
1846
                auto msg = lnav::console::user_message::info(
1847
                    "All text lines are currently hidden");
×
1848
                auto min_time = this->tho_src->get_min_row_time();
×
1849
                if (min_time) {
×
1850
                    msg.with_note(attr_line_t("Lines before ")
×
1851
                                      .append_quoted(lnav::to_rfc3339_string(
×
1852
                                          min_time.value()))
×
1853
                                      .append(" are not being shown"));
×
1854
                }
1855
                auto max_time = this->tho_src->get_max_row_time();
×
1856
                if (max_time) {
×
1857
                    msg.with_note(attr_line_t("Lines after ")
×
1858
                                      .append_quoted(lnav::to_rfc3339_string(
×
1859
                                          max_time.value()))
×
1860
                                      .append(" are not being shown"));
×
1861
                }
1862
                auto& fs = this->tho_src->get_filters();
×
1863
                for (const auto& filt : fs) {
×
1864
                    auto hits = this->tho_src->get_filtered_count_for(
×
1865
                        filt->get_index());
1866
                    if (filt->get_type() == text_filter::EXCLUDE && hits == 0) {
×
1867
                        continue;
×
1868
                    }
1869
                    auto cmd = attr_line_t(":" + filt->to_command());
×
1870
                    readline_command_highlighter(cmd, std::nullopt);
×
1871
                    msg.with_note(
×
1872
                        attr_line_t("Filter ")
×
1873
                            .append_quoted(cmd)
×
1874
                            .append(" matched ")
×
1875
                            .append(lnav::roles::number(fmt::to_string(hits)))
×
1876
                            .append(" line(s) "));
×
1877
                }
1878
                this->tho_static_lines = msg.to_attr_line().split_lines();
×
1879
                this->tho_filter_state = curr_state;
×
1880
            }
1881

1882
            lines = &this->tho_static_lines;
×
1883
        }
1884

1885
        if (lines != nullptr && y < (ssize_t) lines->size()) {
8,828✔
1886
            value_out = lines->at(y);
×
1887
            value_out.with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS));
×
1888
            if (y == (ssize_t) lines->size() - 1) {
×
1889
                value_out.with_attr_for_all(
×
1890
                    VC_STYLE.value(text_attrs::with_underline()));
×
1891
            }
1892
            return true;
×
1893
        }
1894
    }
8,828✔
1895

1896
    if (this->tho_src->text_line_count() > 0) {
8,898✔
1897
        auto& tc
1898
            = dynamic_cast<textview_curses&>(const_cast<listview_curses&>(lv));
8,898✔
1899
        const auto& sticky_bv = tc.get_bookmarks()[&textview_curses::BM_STICKY];
8,898✔
1900
        if (!sticky_bv.empty()) {
8,898✔
1901
            auto top = lv.get_top();
159✔
1902
            int sticky_index = 0;
159✔
1903
            for (auto iter = sticky_bv.bv_tree.begin();
159✔
1904
                 iter != sticky_bv.bv_tree.end();
235✔
1905
                 ++iter)
76✔
1906
            {
1907
                if (*iter >= top) {
180✔
1908
                    break;
89✔
1909
                }
1910
                if (y == sticky_index) {
91✔
1911
                    tc.textview_value_for_row(*iter, value_out);
15✔
1912
                    value_out.with_attr_for_all(
15✔
1913
                        VC_ROLE.value(role_t::VCR_STATUS));
30✔
1914
                    auto next_iter = std::next(iter);
15✔
1915
                    if (next_iter == sticky_bv.bv_tree.end()
15✔
1916
                        || *next_iter >= top)
15✔
1917
                    {
1918
                        value_out.with_attr_for_all(
12✔
1919
                            VC_STYLE.value(text_attrs::with_underline()));
24✔
1920
                    }
1921
                    return true;
15✔
1922
                }
1923
                sticky_index++;
76✔
1924
            }
1925
        }
1926
    }
1927

1928
    if (y != 0) {
8,883✔
1929
        return false;
8,427✔
1930
    }
1931

1932
    const auto lf = this->tho_src->current_file();
456✔
1933
    if (lf == nullptr) {
456✔
1934
        return false;
×
1935
    }
1936

1937
    if (media == media_t::display
456✔
1938
        && lf->get_text_format() != text_format_t::TF_MARKDOWN
395✔
1939
        && this->tho_src->get_effective_view_mode()
851✔
1940
            == textfile_sub_source::view_mode::rendered)
1941
    {
1942
        auto ta = text_attrs::with_underline();
11✔
1943
        value_out.append("\u24d8"_info)
11✔
1944
            .append(" The following is a rendered view of the content.  Use ")
11✔
1945
            .append(lnav::roles::quoted_code(":set-text-view-mode raw"))
11✔
1946
            .append(" to view the raw version of this text")
11✔
1947
            .with_attr_for_all(VC_ROLE.value(role_t::VCR_STATUS_INFO))
22✔
1948
            .with_attr_for_all(VC_STYLE.value(ta));
11✔
1949
        return true;
11✔
1950
    }
1951

1952
    if (lf->get_text_format() != text_format_t::TF_BINARY) {
445✔
1953
        return false;
438✔
1954
    }
1955

1956
    {
1957
        attr_line_builder alb(value_out);
7✔
1958
        {
1959
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
7✔
1960
            alb.appendf(FMT_STRING("{:>16} "), "File Offset");
21✔
1961
        }
7✔
1962
        size_t byte_off = 0;
7✔
1963
        for (size_t lpc = 0; lpc < 16; lpc++) {
119✔
1964
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_FILE_OFFSET));
112✔
1965
            if (byte_off == 8) {
112✔
1966
                alb.append(" ");
7✔
1967
            }
1968
            alb.appendf(FMT_STRING(" {:0>2x}"), lpc);
336✔
1969
            byte_off += 1;
112✔
1970
        }
112✔
1971
        {
1972
            auto ag = alb.with_attr(VC_ROLE.value(role_t::VCR_TABLE_HEADER));
7✔
1973
            alb.appendf(FMT_STRING("  {:^17}"), "ASCII");
21✔
1974
        }
7✔
1975
    }
1976
    value_out.with_attr_for_all(VC_STYLE.value(text_attrs::with_underline()));
7✔
1977
    return true;
7✔
1978
}
456✔
1979

1980
std::optional<attr_line_t>
1981
textfile_header_overlay::list_header_for_overlay(const listview_curses& lv,
4✔
1982
                                                 media_t media,
1983
                                                 vis_line_t line)
1984
{
1985
    return this->tho_hex_line_header;
4✔
1986
}
1987

1988
void
1989
textfile_header_overlay::list_value_for_overlay(
10,749✔
1990
    const listview_curses& lv,
1991
    vis_line_t line,
1992
    std::vector<attr_line_t>& value_out)
1993
{
1994
    if (line != lv.get_selection()) {
10,749✔
1995
        return;
10,743✔
1996
    }
1997

1998
    if (this->tho_src->empty() || line < 0) {
256✔
1999
        value_out.clear();
×
2000
        return;
×
2001
    }
2002

2003
    const auto curr_iter = this->tho_src->current_file_state();
256✔
2004
    if (this->tho_src->tss_view_mode == textfile_sub_source::view_mode::rendered
512✔
2005
        && (*curr_iter)->fvs_text_source)
256✔
2006
    {
2007
        return;
26✔
2008
    }
2009
    const auto& lf = (*curr_iter)->fvs_file;
230✔
2010
    if (lf->get_text_format() == text_format_t::TF_BINARY) {
230✔
2011
        return;
7✔
2012
    }
2013

2014
    auto* lfo = dynamic_cast<line_filter_observer*>(lf->get_logline_observer());
223✔
2015
    if (lfo == nullptr
223✔
2016
        || line >= (ssize_t) lfo->lfo_filter_state.tfs_index.size())
223✔
2017
    {
2018
        value_out.clear();
×
2019
        return;
×
2020
    }
2021

2022
    const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
223✔
2023
    if (ll->is_valid_utf()) {
223✔
2024
        return;
217✔
2025
    }
2026

2027
    auto read_opts = subline_options{};
6✔
2028
    read_opts.scrub_invalid_utf8 = false;
6✔
2029
    auto read_result = lf->read_line(ll, read_opts);
6✔
2030
    if (read_result.isErr()) {
6✔
2031
        return;
×
2032
    }
2033

2034
    auto sbr = read_result.unwrap();
6✔
2035
    attr_line_t al;
6✔
2036
    attr_line_builder alb(al);
6✔
2037
    alb.append_as_hexdump(sbr.to_string_fragment());
6✔
2038
    this->tho_hex_line_header
2039
        = attr_line_t(" Line ")
6✔
2040
              .append(lnav::roles::number(fmt::to_string(line + 1)))
12✔
2041
              .append(" at file offset ")
6✔
2042
              .append(lnav::roles::number(fmt::to_string(ll->get_offset())))
12✔
2043
              .append(
6✔
2044
                  " contains invalid UTF-8 content, the following is a hex "
2045
                  "dump of the line");
6✔
2046
    al.split_lines(value_out);
6✔
2047
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc