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

tstack / lnav / 25381834772-3027

05 May 2026 02:15PM UTC coverage: 69.987% (-0.007%) from 69.994%
25381834772-3027

push

github

tstack
[build] make distclean stuff

57060 of 81530 relevant lines covered (69.99%)

626613.16 hits per line

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

79.13
/src/filter_status_source.cc
1
/**
2
 * Copyright (c) 2018, 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 "filter_status_source.hh"
31

32
#include "base/ansi_scrubber.hh"
33
#include "base/opt_util.hh"
34
#include "config.h"
35
#include "files_sub_source.hh"
36
#include "filter_sub_source.hh"
37
#include "lnav.hh"
38

39
using namespace lnav::roles::literals;
40

41
static constexpr auto TOGGLE_MSG = "Press " ANSI_HOTKEY("TAB") " to edit "_frag;
42
static constexpr auto EXIT_MSG = "Press " ANSI_HOTKEY("ESC") " to exit "_frag;
43

44
static constexpr auto CREATE_HELP
45
    = "Create: " ANSI_HOTKEY("i") "n/" ANSI_HOTKEY("o") "ut";
46
static constexpr auto CREATE_EXPR_HELP = "  SQL " ANSI_HOTKEY("e") "xpr";
47
static constexpr auto CREATE_LEVEL_HELP = "  " ANSI_HOTKEY("l") "evel";
48
static constexpr auto MIN_MAX_TIME_HELP
49
    = "  " ANSI_HOTKEY("m") "in/" ANSI_HOTKEY("M") "ax time";
50
static constexpr auto ENABLE_HELP = ANSI_HOTKEY("SPC") ": ";
51
static constexpr auto EDIT_HELP = ANSI_HOTKEY("ENTER") ": Edit";
52
static constexpr auto TOGGLE_HELP = ANSI_HOTKEY("t") ": To ";
53
static constexpr auto DELETE_HELP = ANSI_HOTKEY("D") ": Delete";
54
static constexpr auto FILTERING_HELP = ANSI_HOTKEY("f") ": ";
55
static constexpr auto JUMP_HELP = ANSI_HOTKEY("ENTER") ": Jump To";
56
static constexpr auto CLOSE_HELP = ANSI_HOTKEY("X") ": Close";
57
static constexpr auto FOCUS_DETAILS_HELP
58
    = ANSI_BOLD("CTRL-]") ": Focus on details view";
59

60
filter_status_source::filter_status_source()
10,704✔
61
{
62
    this->tss_fields[TSF_TITLE].set_width(14);
1,338✔
63
    this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_TITLE);
1,338✔
64
    this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
1,338✔
65
                                          role_t::VCR_STATUS_TITLE_HOTKEY);
66
    this->tss_fields[TSF_TITLE].on_click
67
        = [](status_field&) { set_view_mode(ln_mode_t::FILTER); };
1,338✔
68

69
    this->tss_fields[TSF_STITCH_TITLE].set_width(2);
1,338✔
70
    this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(
1,338✔
71
        role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
72
        role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
73

74
    this->tss_fields[TSF_COUNT].set_min_width(16);
1,338✔
75
    this->tss_fields[TSF_COUNT].set_share(1);
1,338✔
76
    this->tss_fields[TSF_COUNT].set_role(role_t::VCR_STATUS);
1,338✔
77

78
    this->tss_fields[TSF_FILTERED].set_min_width(20);
1,338✔
79
    this->tss_fields[TSF_FILTERED].set_share(1);
1,338✔
80
    this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
1,338✔
81

82
    this->tss_fields[TSF_FILES_TITLE].set_width(7);
1,338✔
83
    this->tss_fields[TSF_FILES_TITLE].set_role(
1,338✔
84
        role_t::VCR_STATUS_DISABLED_TITLE);
85
    this->tss_fields[TSF_FILES_TITLE].set_value(" " ANSI_ROLE("F") "iles ",
1,338✔
86
                                                role_t::VCR_STATUS_HOTKEY);
87
    this->tss_fields[TSF_FILES_TITLE].on_click
88
        = [](status_field&) { set_view_mode(ln_mode_t::FILES); };
1,338✔
89

90
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_width(2);
1,338✔
91
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
1,338✔
92
        role_t::VCR_STATUS, role_t::VCR_STATUS);
93

94
    this->tss_fields[TSF_HELP].right_justify(true);
1,338✔
95
    this->tss_fields[TSF_HELP].set_width(20);
1,338✔
96
    this->tss_fields[TSF_HELP].set_value(TOGGLE_MSG);
1,338✔
97
    this->tss_fields[TSF_HELP].set_left_pad(1);
1,338✔
98

99
    this->tss_error.set_min_width(20);
1,338✔
100
    this->tss_error.set_share(1);
1,338✔
101
    this->tss_error.set_role(role_t::VCR_ALERT_STATUS);
1,338✔
102
}
1,338✔
103

104
size_t
105
filter_status_source::statusview_fields()
18✔
106
{
107
    switch (lnav_data.ld_mode) {
18✔
108
        case ln_mode_t::SEARCH_FILTERS:
×
109
        case ln_mode_t::SEARCH_FILES:
110
            this->tss_fields[TSF_HELP].set_value("");
×
111
            break;
×
112
        case ln_mode_t::FILTER:
8✔
113
        case ln_mode_t::FILES:
114
        case ln_mode_t::FILE_DETAILS:
115
            this->tss_fields[TSF_HELP].set_value(EXIT_MSG);
8✔
116
            break;
8✔
117
        default:
10✔
118
            this->tss_fields[TSF_HELP].set_value(TOGGLE_MSG);
10✔
119
            break;
10✔
120
    }
121

122
    size_t error_count = 0;
18✔
123

124
    auto& fc = lnav_data.ld_active_files;
18✔
125
    {
126
        auto stub_map = fc.fc_name_to_stubs->readAccess();
18✔
127

128
        for (const auto& stub : *stub_map) {
18✔
129
            switch (stub.second.fsi_description.um_level) {
×
130
                case lnav::console::user_message::level::raw:
×
131
                case lnav::console::user_message::level::ok:
132
                case lnav::console::user_message::level::info:
133
                case lnav::console::user_message::level::warning:
134
                    break;
×
135
                case lnav::console::user_message::level::error:
×
136
                    error_count += 1;
×
137
                    break;
×
138
            }
139
        }
140
    }
18✔
141

142
    if (error_count == 0) {
18✔
143
        this->tss_error.clear();
18✔
144
    } else if (error_count == 1) {
×
145
        this->tss_error.set_value(" error: a file cannot be opened "_frag);
×
146
    } else if (error_count > 1) {
×
147
        this->tss_error.set_value(" error: %zu files cannot be opened ",
×
148
                                  error_count);
149
    }
150

151
    if (lnav_data.ld_mode == ln_mode_t::FILES
18✔
152
        || lnav_data.ld_mode == ln_mode_t::FILE_DETAILS
14✔
153
        || lnav_data.ld_mode == ln_mode_t::SEARCH_FILES)
14✔
154
    {
155
        this->tss_fields[TSF_FILES_TITLE].set_value(
4✔
156
            " " ANSI_ROLE("F") "iles ", role_t::VCR_STATUS_TITLE_HOTKEY);
157
        if (error_count > 0) {
4✔
158
            this->tss_fields[TSF_FILES_TITLE].set_role(
×
159
                role_t::VCR_ALERT_STATUS_TITLE);
160
            this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
×
161
                role_t::VCR_STATUS_STITCH_ALERT_TITLE_TO_NORMAL,
162
                role_t::VCR_STATUS_STITCH_NORMAL_TO_ALERT_TITLE);
163
        } else {
164
            this->tss_fields[TSF_FILES_TITLE].set_role(
4✔
165
                role_t::VCR_STATUS_TITLE);
166
            this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
4✔
167
                role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
168
                role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
169
        }
170
        this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
4✔
171
                                              role_t::VCR_STATUS_HOTKEY);
172
        this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_DISABLED_TITLE);
4✔
173
        this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(role_t::VCR_STATUS,
4✔
174
                                                            role_t::VCR_STATUS);
175
    } else {
176
        this->tss_fields[TSF_FILES_TITLE].set_value(" " ANSI_ROLE("F") "iles ",
14✔
177
                                                    role_t::VCR_STATUS_HOTKEY);
178
        if (error_count > 0) {
14✔
179
            this->tss_fields[TSF_FILES_TITLE].set_role(
×
180
                role_t::VCR_ALERT_STATUS);
181
        } else {
182
            this->tss_fields[TSF_FILES_TITLE].set_role(
14✔
183
                role_t::VCR_STATUS_DISABLED_TITLE);
184
        }
185
        this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
14✔
186
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE,
187
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL);
188
        this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
14✔
189
                                              role_t::VCR_STATUS_TITLE_HOTKEY);
190
        this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_TITLE);
14✔
191
        this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(
14✔
192
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
193
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
194
    }
195

196
    lnav_data.ld_view_stack.top() | [this](auto tc) {
18✔
197
        text_sub_source* tss = tc->get_sub_source();
18✔
198
        if (tss == nullptr) {
18✔
199
            return;
×
200
        }
201

202
        filter_stack& fs = tss->get_filters();
18✔
203
        auto enabled_count = 0, filter_count = 0;
18✔
204

205
        for (const auto& tf : fs) {
29✔
206
            if (tf->is_enabled()) {
11✔
207
                enabled_count += 1;
2✔
208
            }
209
            filter_count += 1;
11✔
210
        }
211
        if (filter_count == 0) {
18✔
212
            this->tss_fields[TSF_COUNT].set_value(""_frag);
7✔
213
        } else {
214
            this->tss_fields[TSF_COUNT].set_value(
11✔
215
                " " ANSI_BOLD("%d") " of " ANSI_BOLD("%d") " enabled ",
216
                enabled_count,
217
                filter_count);
218
        }
219
    };
220

221
    return TSF__MAX;
18✔
222
}
223

224
status_field&
225
filter_status_source::statusview_value_for_field(int field)
126✔
226
{
227
    if (field == TSF_FILTERED
126✔
228
        && !lnav_data.ld_active_files.fc_name_to_stubs->readAccess()->empty())
126✔
229
    {
230
        return this->tss_error;
×
231
    }
232

233
    return this->tss_fields[field];
126✔
234
}
235

236
bool
237
filter_status_source::update_filtered(text_sub_source* tss)
4,038✔
238
{
239
    if (tss == nullptr) {
4,038✔
240
        return false;
773✔
241
    }
242

243
    auto& sf = this->tss_fields[TSF_FILTERED];
3,265✔
244
    auto retval = false;
3,265✔
245

246
    auto curr_filtered_count = tss->get_filtered_count();
3,265✔
247
    if (curr_filtered_count == 0) {
3,265✔
248
        if (tss->tss_apply_filters) {
3,042✔
249
            if (!sf.empty()) {
3,039✔
250
                sf.clear();
28✔
251
                retval = true;
28✔
252
            }
253
        } else {
254
            auto al = attr_line_t(" ")
3✔
255
                          .append("  ", VC_ICON.value(ui_icon_t::warning))
6✔
256
                          .append(" Filtering disabled, re-enable with ")
3✔
257
                          .append(":toggle-filtering"_symbol);
3✔
258
            retval = sf.set_value(al);
3✔
259
        }
3✔
260
        this->bss_last_filtered_count = curr_filtered_count;
3,042✔
261
    } else {
262
        auto& timer = ui_periodic_timer::singleton();
223✔
263
        auto& al = sf.get_value();
223✔
264

265
        if (curr_filtered_count == this->bss_last_filtered_count) {
223✔
266
            if (timer.fade_diff(this->bss_filter_counter) == 0
127✔
267
                && this->tss_fields[TSF_FILTERED].get_role()
127✔
268
                    != role_t::VCR_STATUS)
269
            {
270
                this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
×
271
                al.with_attr(
×
272
                    string_attr(line_range{0, -1},
×
273
                                VC_STYLE.value(text_attrs::with_bold())));
×
274
                retval = true;
×
275
            }
276
        } else {
277
            this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_ALERT_STATUS);
96✔
278
            this->bss_last_filtered_count = tss->get_filtered_count();
96✔
279
            timer.start_fade(this->bss_filter_counter, 3);
96✔
280
            retval = sf.set_value("%'9d Lines not shown ",
96✔
281
                                  tss->get_filtered_count());
96✔
282
        }
283
    }
284

285
    return retval;
3,265✔
286
}
287

288
filter_help_status_source::filter_help_status_source()
1,338✔
289
{
290
    this->fss_help.set_min_width(10);
1,338✔
291
    this->fss_help.set_share(1);
1,338✔
292
    this->fss_prompt.set_left_pad(1);
1,338✔
293
    this->fss_prompt.set_min_width(35);
1,338✔
294
    this->fss_prompt.set_share(1);
1,338✔
295
    this->fss_error.set_left_pad(25);
1,338✔
296
    this->fss_error.set_min_width(35);
1,338✔
297
    this->fss_error.set_share(1);
1,338✔
298
}
1,338✔
299

300
size_t
301
filter_help_status_source::statusview_fields()
10✔
302
{
303
    lnav_data.ld_view_stack.top() | [this](auto tc) {
10✔
304
        auto* tss = tc->get_sub_source();
10✔
305
        if (tss == nullptr) {
10✔
306
            return;
×
307
        }
308

309
        auto* lss = dynamic_cast<logfile_sub_source*>(tss);
10✔
310
        auto* ttt = dynamic_cast<text_time_translator*>(tss);
10✔
311

312
        if (lnav_data.ld_mode == ln_mode_t::FILTER) {
10✔
313
            static auto* editor = injector::get<filter_sub_source*>();
4✔
314
            auto& lv = lnav_data.ld_filter_view;
4✔
315
            auto sel = lv.get_selection();
4✔
316
            auto* fss = dynamic_cast<filter_sub_source*>(lv.get_sub_source());
4✔
317
            auto rows = fss->rows_for(tc);
4✔
318
            if (rows.empty()) {
4✔
319
                this->fss_help.set_value(
2✔
320
                    "  %s%s%s%s",
321
                    CREATE_HELP,
322
                    lss != nullptr ? CREATE_EXPR_HELP : "",
323
                    lss != nullptr ? CREATE_LEVEL_HELP : "",
324
                    ttt != nullptr ? MIN_MAX_TIME_HELP : "");
325
            } else {
326
                auto& row = rows[sel.value()];
2✔
327
                auto* tfr = dynamic_cast<filter_sub_source::text_filter_row*>(
2✔
328
                    row.get());
2✔
329
                auto* tir = dynamic_cast<filter_sub_source::time_filter_row*>(
2✔
330
                    row.get());
2✔
331
                if (editor->fss_editing) {
2✔
332
                    if (tfr != nullptr) {
2✔
333
                        auto& tf = tfr->tfr_filter;
2✔
334
                        auto lang = tf->get_lang() == filter_lang_t::SQL
2✔
335
                            ? "an SQL"
2✔
336
                            : "a regular";
337

338
                        if (tf->get_type() == text_filter::type_t::INCLUDE) {
2✔
339
                            this->fss_help.set_value(
×
340
                                "                           "
341
                                "Enter %s expression to match lines to filter "
342
                                "in:",
343
                                lang);
344
                        } else {
345
                            this->fss_help.set_value(
2✔
346
                                "                           "
347
                                "Enter %s expression to match lines to filter "
348
                                "out:",
349
                                lang);
350
                        }
351
                    } else if (tir != nullptr) {
×
352
                        this->fss_help.set_value(
×
353
                            "                           "
354
                            "Enter timestamp:");
355
                    } else {
356
                        this->fss_help.set_value(
×
357
                            "                           "
358
                            "Enter level:");
359
                    }
360
                } else if (tfr != nullptr) {
×
361
                    auto& tf = tfr->tfr_filter;
×
362
                    this->fss_help.set_value(
×
363
                        "  %s%s%s%s  %s%s  %s  %s%s  %s  %s%s",
364
                        CREATE_HELP,
365
                        lss != nullptr ? CREATE_EXPR_HELP : "",
366
                        lss != nullptr ? CREATE_LEVEL_HELP : "",
367
                        ttt != nullptr ? MIN_MAX_TIME_HELP : "",
368
                        ENABLE_HELP,
369
                        tf->is_enabled() ? "Disable" : "Enable ",
×
370
                        EDIT_HELP,
371
                        TOGGLE_HELP,
372
                        tf->get_type() == text_filter::type_t::INCLUDE ? "OUT"
×
373
                                                                       : "IN ",
374
                        DELETE_HELP,
375
                        FILTERING_HELP,
376
                        tss->tss_apply_filters ? "Disable Filtering"
×
377
                                               : "Enable Filtering");
378
                } else {
379
                    this->fss_help.set_value(
×
380
                        "  %s%s%s%s  %s  %s  %s%s",
381
                        CREATE_HELP,
382
                        lss != nullptr ? CREATE_EXPR_HELP : "",
383
                        lss != nullptr ? CREATE_LEVEL_HELP : "",
384
                        ttt != nullptr ? MIN_MAX_TIME_HELP : "",
385
                        EDIT_HELP,
386
                        DELETE_HELP,
387
                        FILTERING_HELP,
388
                        tss->tss_apply_filters ? "Disable Filtering"
×
389
                                               : "Enable Filtering");
390
                }
391
            }
392
        } else if ((lnav_data.ld_mode == ln_mode_t::FILES
10✔
393
                    || lnav_data.ld_mode == ln_mode_t::FILE_DETAILS)
2✔
394
                   && lnav_data.ld_session_loaded)
4✔
395
        {
396
            const auto& lv = lnav_data.ld_files_view;
4✔
397
            auto sel = files_model::from_selection(lv.get_selection());
4✔
398

399
            sel.match(
4✔
400
                [this](files_model::no_selection) { this->fss_help.clear(); },
2✔
401
                [this](files_model::stub_selection) {
×
402
                    this->fss_help.set_value("  %s", CLOSE_HELP);
×
403
                },
404
                [this](files_model::other_selection) {
×
405
                    this->fss_help.clear();
×
406
                },
407
                [this](files_model::file_selection& fs) {
6✔
408
                    auto& lss = lnav_data.ld_log_source;
2✔
409
                    auto vis_help = "Hide";
2✔
410
                    auto ld_opt = lss.find_data(*fs.sb_iter);
2✔
411
                    if (ld_opt && !ld_opt.value()->ld_visible) {
2✔
412
                        vis_help = "Show";
×
413
                    }
414
                    const auto* focus_details_help
2✔
415
                        = lnav_data.ld_mode == ln_mode_t::FILES
2✔
416
                        ? FOCUS_DETAILS_HELP
2✔
417
                        : "";
418

419
                    this->fss_help.set_value("  %s%s  %s  %s",
2✔
420
                                             ENABLE_HELP,
421
                                             vis_help,
422
                                             JUMP_HELP,
423
                                             focus_details_help);
424
                });
425
        }
4✔
426
    };
427

428
    return 1;
10✔
429
}
430

431
status_field&
432
filter_help_status_source::statusview_value_for_field(int field)
10✔
433
{
434
    if (!this->fss_error.empty()) {
10✔
435
        return this->fss_error;
×
436
    }
437

438
    if (!this->fss_prompt.empty()) {
10✔
439
        return this->fss_prompt;
×
440
    }
441

442
    return this->fss_help;
10✔
443
}
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