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

tstack / lnav / 20114321350-2741

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

push

github

tstack
[filters] add level filter

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

447 existing lines in 8 files now uncovered.

51534 of 74865 relevant lines covered (68.84%)

434761.7 hits per line

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

65.15
/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
static constexpr auto TOGGLE_MSG = "Press " ANSI_HOTKEY("TAB") " to edit "_frag;
40
static constexpr auto EXIT_MSG = "Press " ANSI_HOTKEY("ESC") " to exit "_frag;
41

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

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

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

72
    this->tss_fields[TSF_COUNT].set_min_width(16);
1,168✔
73
    this->tss_fields[TSF_COUNT].set_share(1);
1,168✔
74
    this->tss_fields[TSF_COUNT].set_role(role_t::VCR_STATUS);
1,168✔
75

76
    this->tss_fields[TSF_FILTERED].set_min_width(20);
1,168✔
77
    this->tss_fields[TSF_FILTERED].set_share(1);
1,168✔
78
    this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
1,168✔
79

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

88
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_width(2);
1,168✔
89
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
1,168✔
90
        role_t::VCR_STATUS, role_t::VCR_STATUS);
91

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

97
    this->tss_error.set_min_width(20);
1,168✔
98
    this->tss_error.set_share(1);
1,168✔
99
    this->tss_error.set_role(role_t::VCR_ALERT_STATUS);
1,168✔
100
}
1,168✔
101

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

120
    if (lnav_data.ld_mode == ln_mode_t::FILES
14✔
121
        || lnav_data.ld_mode == ln_mode_t::FILE_DETAILS
14✔
122
        || lnav_data.ld_mode == ln_mode_t::SEARCH_FILES)
14✔
123
    {
124
        this->tss_fields[TSF_FILES_TITLE].set_value(
×
125
            " " ANSI_ROLE("F") "iles ", role_t::VCR_STATUS_TITLE_HOTKEY);
126
        this->tss_fields[TSF_FILES_TITLE].set_role(role_t::VCR_STATUS_TITLE);
×
127
        this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
×
128
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
129
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
130
        this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
×
131
                                              role_t::VCR_STATUS_HOTKEY);
132
        this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_DISABLED_TITLE);
×
133
        this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(role_t::VCR_STATUS,
×
134
                                                            role_t::VCR_STATUS);
135
    } else {
136
        this->tss_fields[TSF_FILES_TITLE].set_value(" " ANSI_ROLE("F") "iles ",
14✔
137
                                                    role_t::VCR_STATUS_HOTKEY);
138
        if (lnav_data.ld_active_files.fc_name_to_stubs->readAccess()->empty()) {
14✔
139
            this->tss_fields[TSF_FILES_TITLE].set_role(
14✔
140
                role_t::VCR_STATUS_DISABLED_TITLE);
141
        } else {
142
            size_t error_count = 0;
×
143
            size_t other_count = 0;
×
144

145
            auto& fc = lnav_data.ld_active_files;
×
146
            {
147
                auto stub_map = fc.fc_name_to_stubs->readAccess();
×
148

149
                for (const auto& stub : *stub_map) {
×
150
                    switch (stub.second.fsi_description.um_level) {
×
151
                        case lnav::console::user_message::level::raw:
×
152
                        case lnav::console::user_message::level::ok:
153
                        case lnav::console::user_message::level::info:
154
                        case lnav::console::user_message::level::warning:
155
                            other_count += 1;
×
156
                            break;
×
157
                        case lnav::console::user_message::level::error:
×
158
                            error_count += 1;
×
159
                            break;
×
160
                    }
161
                }
162
            }
163

164
            if (error_count > 0) {
×
165
                this->tss_fields[TSF_FILES_TITLE].set_role(
×
166
                    role_t::VCR_ALERT_STATUS);
167
            }
168
            if (error_count == 1) {
×
169
                this->tss_error.set_value(
×
170
                    " error: a file cannot be opened "_frag);
×
171
            } else if (error_count > 1) {
×
172
                this->tss_error.set_value(" error: %zu files cannot be opened ",
×
173
                                          error_count);
174
            }
175
        }
176
        this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
14✔
177
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE,
178
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL);
179
        this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
14✔
180
                                              role_t::VCR_STATUS_TITLE_HOTKEY);
181
        this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_TITLE);
14✔
182
        this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(
14✔
183
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
184
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
185
    }
186

187
    lnav_data.ld_view_stack.top() | [this](auto tc) {
14✔
188
        text_sub_source* tss = tc->get_sub_source();
14✔
189
        if (tss == nullptr) {
14✔
190
            return;
×
191
        }
192

193
        filter_stack& fs = tss->get_filters();
14✔
194
        auto enabled_count = 0, filter_count = 0;
14✔
195

196
        for (const auto& tf : fs) {
23✔
197
            if (tf->is_enabled()) {
9✔
198
                enabled_count += 1;
2✔
199
            }
200
            filter_count += 1;
9✔
201
        }
202
        if (filter_count == 0) {
14✔
203
            this->tss_fields[TSF_COUNT].set_value(""_frag);
5✔
204
        } else {
205
            this->tss_fields[TSF_COUNT].set_value(
9✔
206
                " " ANSI_BOLD("%d") " of " ANSI_BOLD("%d") " enabled ",
207
                enabled_count,
208
                filter_count);
209
        }
210
    };
211

212
    return TSF__MAX;
14✔
213
}
214

215
status_field&
216
filter_status_source::statusview_value_for_field(int field)
98✔
217
{
218
    if (field == TSF_FILTERED
98✔
219
        && !lnav_data.ld_active_files.fc_name_to_stubs->readAccess()->empty())
98✔
220
    {
221
        return this->tss_error;
×
222
    }
223

224
    return this->tss_fields[field];
98✔
225
}
226

227
bool
228
filter_status_source::update_filtered(text_sub_source* tss)
3,061✔
229
{
230
    if (tss == nullptr) {
3,061✔
231
        return false;
611✔
232
    }
233

234
    auto& sf = this->tss_fields[TSF_FILTERED];
2,450✔
235
    auto retval = false;
2,450✔
236

237
    auto curr_filtered_count = tss->get_filtered_count();
2,450✔
238
    if (curr_filtered_count == 0) {
2,450✔
239
        if (tss->tss_apply_filters) {
2,303✔
240
            if (!sf.empty()) {
2,301✔
241
                sf.clear();
15✔
242
                retval = true;
15✔
243
            }
244
        } else {
245
            retval = sf.set_value(
2✔
246
                " \u2718 Filtering disabled, re-enable with " ANSI_BOLD_START
247
                ":toggle-filtering" ANSI_NORM);
248
        }
249
        this->bss_last_filtered_count = curr_filtered_count;
2,303✔
250
    } else {
251
        auto& timer = ui_periodic_timer::singleton();
147✔
252
        auto& al = sf.get_value();
147✔
253

254
        if (curr_filtered_count == this->bss_last_filtered_count) {
147✔
255
            if (timer.fade_diff(this->bss_filter_counter) == 0
83✔
256
                && this->tss_fields[TSF_FILTERED].get_role()
83✔
257
                    != role_t::VCR_STATUS)
258
            {
259
                this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
×
260
                al.with_attr(
×
261
                    string_attr(line_range{0, -1},
×
262
                                VC_STYLE.value(text_attrs::with_bold())));
×
263
                retval = true;
×
264
            }
265
        } else {
266
            this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_ALERT_STATUS);
64✔
267
            this->bss_last_filtered_count = tss->get_filtered_count();
64✔
268
            timer.start_fade(this->bss_filter_counter, 3);
64✔
269
            retval = sf.set_value("%'9d Lines not shown ",
64✔
270
                                  tss->get_filtered_count());
64✔
271
        }
272
    }
273

274
    return retval;
2,450✔
275
}
276

277
filter_help_status_source::filter_help_status_source()
1,168✔
278
{
279
    this->fss_help.set_min_width(10);
1,168✔
280
    this->fss_help.set_share(1);
1,168✔
281
    this->fss_prompt.set_left_pad(1);
1,168✔
282
    this->fss_prompt.set_min_width(35);
1,168✔
283
    this->fss_prompt.set_share(1);
1,168✔
284
    this->fss_error.set_left_pad(25);
1,168✔
285
    this->fss_error.set_min_width(35);
1,168✔
286
    this->fss_error.set_share(1);
1,168✔
287
}
1,168✔
288

289
size_t
290
filter_help_status_source::statusview_fields()
6✔
291
{
292
    lnav_data.ld_view_stack.top() | [this](auto tc) {
6✔
293
        auto* tss = tc->get_sub_source();
6✔
294
        if (tss == nullptr) {
6✔
295
            return;
×
296
        }
297

298
        auto* lss = dynamic_cast<logfile_sub_source*>(tss);
6✔
299
        auto* ttt = dynamic_cast<text_time_translator*>(tss);
6✔
300

301
        if (lnav_data.ld_mode == ln_mode_t::FILTER) {
6✔
302
            static auto* editor = injector::get<filter_sub_source*>();
4✔
303
            auto& lv = lnav_data.ld_filter_view;
4✔
304
            auto sel = lv.get_selection();
4✔
305
            auto* fss = dynamic_cast<filter_sub_source*>(lv.get_sub_source());
4✔
306
            auto rows = fss->rows_for(tc);
4✔
307
            if (rows.empty()) {
4✔
308
                this->fss_help.set_value(
2✔
309
                    "  %s%s%s%s",
310
                    CREATE_HELP,
311
                    lss != nullptr ? CREATE_EXPR_HELP : "",
312
                    lss != nullptr ? CREATE_LEVEL_HELP : "",
313
                    ttt != nullptr ? MIN_MAX_TIME_HELP : "");
314
            } else {
315
                auto& row = rows[sel.value()];
2✔
316
                auto* tfr = dynamic_cast<filter_sub_source::text_filter_row*>(
2✔
317
                    row.get());
2✔
318
                auto* tir = dynamic_cast<filter_sub_source::time_filter_row*>(
2✔
319
                    row.get());
2✔
320
                if (editor->fss_editing) {
2✔
321
                    if (tfr != nullptr) {
2✔
322
                        auto& tf = tfr->tfr_filter;
2✔
323
                        auto lang = tf->get_lang() == filter_lang_t::SQL
2✔
324
                            ? "an SQL"
2✔
325
                            : "a regular";
326

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

388
            sel.match(
×
389
                [this](files_model::no_selection) { this->fss_help.clear(); },
×
390
                [this](files_model::stub_selection) {
×
391
                    this->fss_help.set_value("  %s", CLOSE_HELP);
×
392
                },
393
                [this](files_model::other_selection) {
×
394
                    this->fss_help.clear();
×
395
                },
396
                [this](files_model::file_selection& fs) {
×
397
                    auto& lss = lnav_data.ld_log_source;
×
398
                    auto vis_help = "Hide";
×
399
                    auto ld_opt = lss.find_data(*fs.sb_iter);
×
400
                    if (ld_opt && !ld_opt.value()->ld_visible) {
×
401
                        vis_help = "Show";
×
402
                    }
403
                    const auto* focus_details_help
×
404
                        = lnav_data.ld_mode == ln_mode_t::FILES
×
405
                        ? FOCUS_DETAILS_HELP
×
406
                        : "";
407

408
                    this->fss_help.set_value("  %s%s  %s  %s",
×
409
                                             ENABLE_HELP,
410
                                             vis_help,
411
                                             JUMP_HELP,
412
                                             focus_details_help);
413
                });
414
        }
415
    };
416

417
    return 1;
6✔
418
}
419

420
status_field&
421
filter_help_status_source::statusview_value_for_field(int field)
6✔
422
{
423
    if (!this->fss_error.empty()) {
6✔
424
        return this->fss_error;
×
425
    }
426

427
    if (!this->fss_prompt.empty()) {
6✔
428
        return this->fss_prompt;
×
429
    }
430

431
    return this->fss_help;
6✔
432
}
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