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

tstack / lnav / 18954735303-2618

30 Oct 2025 08:49PM UTC coverage: 68.778% (+0.7%) from 68.076%
18954735303-2618

push

github

tstack
[filters] add min/max time filters to UI

Related to #1275
Related to #1576

175 of 525 new or added lines in 12 files covered. (33.33%)

582 existing lines in 3 files now uncovered.

50384 of 73256 relevant lines covered (68.78%)

426743.35 hits per line

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

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

42
static constexpr auto CREATE_HELP
43
    = ANSI_BOLD("i") "/" ANSI_BOLD("o") ": Create in/out";
44
static constexpr auto MIN_MAX_TIME_HELP
45
    = ANSI_BOLD("m") "/" ANSI_BOLD("M") ": Set min/max time";
46
static constexpr auto ENABLE_HELP = ANSI_BOLD("SPC") ": ";
47
static constexpr auto EDIT_HELP = ANSI_BOLD("ENTER") ": Edit";
48
static constexpr auto TOGGLE_HELP = ANSI_BOLD("t") ": To ";
49
static constexpr auto DELETE_HELP = ANSI_BOLD("D") ": Delete";
50
static constexpr auto FILTERING_HELP = ANSI_BOLD("f") ": ";
51
static constexpr auto JUMP_HELP = ANSI_BOLD("ENTER") ": Jump To";
52
static constexpr auto CLOSE_HELP = ANSI_BOLD("X") ": Close";
53
static constexpr auto FOCUS_DETAILS_HELP
54
    = ANSI_BOLD("CTRL-]") ": Focus on details view";
55

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

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

70
    this->tss_fields[TSF_COUNT].set_min_width(16);
1,142✔
71
    this->tss_fields[TSF_COUNT].set_share(1);
1,142✔
72
    this->tss_fields[TSF_COUNT].set_role(role_t::VCR_STATUS);
1,142✔
73

74
    this->tss_fields[TSF_FILTERED].set_min_width(20);
1,142✔
75
    this->tss_fields[TSF_FILTERED].set_share(1);
1,142✔
76
    this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
1,142✔
77

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

86
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_width(2);
1,142✔
87
    this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
1,142✔
88
        role_t::VCR_STATUS, role_t::VCR_STATUS);
89

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

95
    this->tss_error.set_min_width(20);
1,142✔
96
    this->tss_error.set_share(1);
1,142✔
97
    this->tss_error.set_role(role_t::VCR_ALERT_STATUS);
1,142✔
98
}
1,142✔
99

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

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

144
            auto& fc = lnav_data.ld_active_files;
×
145
            if (fc.fc_name_to_errors->readAccess()->size() == 1) {
×
146
                this->tss_error.set_value(
×
147
                    " error: a file cannot be opened "_frag);
×
148
            } else {
149
                this->tss_error.set_value(
×
150
                    " error: %u files cannot be opened ",
151
                    lnav_data.ld_active_files.fc_name_to_errors->readAccess()
×
152
                        ->size());
153
            }
154
        }
155
        this->tss_fields[TSF_FILES_RIGHT_STITCH].set_stitch_value(
14✔
156
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE,
157
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL);
158
        this->tss_fields[TSF_TITLE].set_value(" " ANSI_ROLE("T") "ext Filters ",
14✔
159
                                              role_t::VCR_STATUS_TITLE_HOTKEY);
160
        this->tss_fields[TSF_TITLE].set_role(role_t::VCR_STATUS_TITLE);
14✔
161
        this->tss_fields[TSF_STITCH_TITLE].set_stitch_value(
14✔
162
            role_t::VCR_STATUS_STITCH_TITLE_TO_NORMAL,
163
            role_t::VCR_STATUS_STITCH_NORMAL_TO_TITLE);
164
    }
165

166
    lnav_data.ld_view_stack.top() | [this](auto tc) {
14✔
167
        text_sub_source* tss = tc->get_sub_source();
14✔
168
        if (tss == nullptr) {
14✔
169
            return;
×
170
        }
171

172
        filter_stack& fs = tss->get_filters();
14✔
173
        auto enabled_count = 0, filter_count = 0;
14✔
174

175
        for (const auto& tf : fs) {
23✔
176
            if (tf->is_enabled()) {
9✔
177
                enabled_count += 1;
2✔
178
            }
179
            filter_count += 1;
9✔
180
        }
181
        if (filter_count == 0) {
14✔
182
            this->tss_fields[TSF_COUNT].set_value(""_frag);
5✔
183
        } else {
184
            this->tss_fields[TSF_COUNT].set_value(
9✔
185
                " " ANSI_BOLD("%d") " of " ANSI_BOLD("%d") " enabled ",
186
                enabled_count,
187
                filter_count);
188
        }
189
    };
190

191
    return TSF__MAX;
14✔
192
}
193

194
status_field&
195
filter_status_source::statusview_value_for_field(int field)
98✔
196
{
197
    if (field == TSF_FILTERED
98✔
198
        && !lnav_data.ld_active_files.fc_name_to_errors->readAccess()->empty())
98✔
199
    {
200
        return this->tss_error;
×
201
    }
202

203
    return this->tss_fields[field];
98✔
204
}
205

206
bool
207
filter_status_source::update_filtered(text_sub_source* tss)
2,949✔
208
{
209
    if (tss == nullptr) {
2,949✔
210
        return false;
590✔
211
    }
212

213
    auto& sf = this->tss_fields[TSF_FILTERED];
2,359✔
214
    auto retval = false;
2,359✔
215

216
    if (tss->get_filtered_count() == 0) {
2,359✔
217
        if (tss->tss_apply_filters) {
2,226✔
218
            if (!sf.empty()) {
2,224✔
219
                sf.clear();
12✔
220
                retval = true;
12✔
221
            }
222
        } else {
223
            sf.set_value(
2✔
224
                " \u2718 Filtering disabled, re-enable with " ANSI_BOLD_START
225
                ":toggle-filtering" ANSI_NORM);
226
        }
227
    } else {
228
        auto& timer = ui_periodic_timer::singleton();
133✔
229
        auto& al = sf.get_value();
133✔
230

231
        if (tss->get_filtered_count() == this->bss_last_filtered_count) {
133✔
232
            if (timer.fade_diff(this->bss_filter_counter) == 0) {
76✔
233
                this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_STATUS);
×
234
                al.with_attr(
×
235
                    string_attr(line_range{0, -1},
×
236
                                VC_STYLE.value(text_attrs::with_bold())));
×
237
                retval = true;
×
238
            }
239
        } else {
240
            this->tss_fields[TSF_FILTERED].set_role(role_t::VCR_ALERT_STATUS);
57✔
241
            this->bss_last_filtered_count = tss->get_filtered_count();
57✔
242
            timer.start_fade(this->bss_filter_counter, 3);
57✔
243
            sf.set_value("%'9d Lines not shown ", tss->get_filtered_count());
57✔
244
            retval = true;
57✔
245
        }
246
    }
247

248
    return retval;
2,359✔
249
}
250

251
filter_help_status_source::filter_help_status_source()
1,142✔
252
{
253
    this->fss_help.set_min_width(10);
1,142✔
254
    this->fss_help.set_share(1);
1,142✔
255
    this->fss_prompt.set_left_pad(1);
1,142✔
256
    this->fss_prompt.set_min_width(35);
1,142✔
257
    this->fss_prompt.set_share(1);
1,142✔
258
    this->fss_error.set_left_pad(25);
1,142✔
259
    this->fss_error.set_min_width(35);
1,142✔
260
    this->fss_error.set_share(1);
1,142✔
261
}
1,142✔
262

263
size_t
264
filter_help_status_source::statusview_fields()
6✔
265
{
266
    lnav_data.ld_view_stack.top() | [this](auto tc) {
6✔
267
        auto* tss = tc->get_sub_source();
6✔
268
        if (tss == nullptr) {
6✔
269
            return;
×
270
        }
271

272
        auto* ttt = dynamic_cast<text_time_translator*>(tss);
6✔
273

274
        if (lnav_data.ld_mode == ln_mode_t::FILTER) {
6✔
275
            static auto* editor = injector::get<filter_sub_source*>();
4✔
276
            auto& lv = lnav_data.ld_filter_view;
4✔
277
            auto sel = lv.get_selection();
4✔
278
            auto* fss = dynamic_cast<filter_sub_source*>(lv.get_sub_source());
4✔
279
            auto rows = fss->rows_for(tc);
4✔
280
            if (rows.empty()) {
4✔
281
                this->fss_help.set_value(
2✔
282
                    "  %s  %s",
283
                    CREATE_HELP,
284
                    ttt != nullptr ? MIN_MAX_TIME_HELP : "");
285
            } else {
286
                auto& row = rows[sel.value()];
2✔
287
                auto* tfr = dynamic_cast<filter_sub_source::text_filter_row*>(
2✔
288
                    row.get());
2✔
289
                if (editor->fss_editing) {
2✔
290
                    if (tfr != nullptr) {
2✔
291
                        auto& tf = tfr->tfr_filter;
2✔
292
                        auto lang = tf->get_lang() == filter_lang_t::SQL
2✔
293
                            ? "an SQL"
2✔
294
                            : "a regular";
295

296
                        if (tf->get_type() == text_filter::type_t::INCLUDE) {
2✔
NEW
297
                            this->fss_help.set_value(
×
298
                                "                           "
299
                                "Enter %s expression to match lines to filter "
300
                                "in:",
301
                                lang);
302
                        } else {
303
                            this->fss_help.set_value(
2✔
304
                                "                           "
305
                                "Enter %s expression to match lines to filter "
306
                                "out:",
307
                                lang);
308
                        }
309
                    } else {
NEW
310
                        this->fss_help.set_value(
×
311
                            "                           "
312
                            "Enter timestamp:");
313
                    }
NEW
314
                } else if (tfr != nullptr) {
×
NEW
315
                    auto& tf = tfr->tfr_filter;
×
UNCOV
316
                    this->fss_help.set_value(
×
317
                        "  %s  %s%s  %s  %s%s  %s  %s%s",
318
                        CREATE_HELP,
319
                        ENABLE_HELP,
NEW
320
                        tf->is_enabled() ? "Disable" : "Enable ",
×
321
                        EDIT_HELP,
322
                        TOGGLE_HELP,
NEW
323
                        tf->get_type() == text_filter::type_t::INCLUDE ? "OUT"
×
324
                                                                       : "IN ",
325
                        DELETE_HELP,
326
                        FILTERING_HELP,
NEW
327
                        tss->tss_apply_filters ? "Disable Filtering"
×
328
                                               : "Enable Filtering");
329
                } else {
NEW
330
                    this->fss_help.set_value("  %s  %s  %s  %s%s",
×
331
                                             CREATE_HELP,
332
                                             EDIT_HELP,
333
                                             DELETE_HELP,
334
                                             FILTERING_HELP,
NEW
335
                                             tss->tss_apply_filters
×
336
                                                 ? "Disable Filtering"
337
                                                 : "Enable Filtering");
338
                }
339
            }
340
        } else if ((lnav_data.ld_mode == ln_mode_t::FILES
6✔
341
                    || lnav_data.ld_mode == ln_mode_t::FILE_DETAILS)
2✔
342
                   && lnav_data.ld_session_loaded)
×
343
        {
344
            const auto& lv = lnav_data.ld_files_view;
×
345
            auto sel = files_model::from_selection(lv.get_selection());
×
346

347
            sel.match(
×
348
                [this](files_model::no_selection) { this->fss_help.clear(); },
×
349
                [this](files_model::error_selection) {
×
350
                    this->fss_help.set_value("  %s", CLOSE_HELP);
×
351
                },
352
                [this](files_model::other_selection) {
×
353
                    this->fss_help.clear();
×
354
                },
355
                [this](files_model::file_selection& fs) {
×
356
                    auto& lss = lnav_data.ld_log_source;
×
357
                    auto vis_help = "Hide";
×
358
                    auto ld_opt = lss.find_data(*fs.sb_iter);
×
359
                    if (ld_opt && !ld_opt.value()->ld_visible) {
×
360
                        vis_help = "Show";
×
361
                    }
362
                    const auto* focus_details_help
×
363
                        = lnav_data.ld_mode == ln_mode_t::FILES
×
364
                        ? FOCUS_DETAILS_HELP
×
365
                        : "";
366

367
                    this->fss_help.set_value("  %s%s  %s  %s",
×
368
                                             ENABLE_HELP,
369
                                             vis_help,
370
                                             JUMP_HELP,
371
                                             focus_details_help);
372
                });
373
        }
374
    };
375

376
    return 1;
6✔
377
}
378

379
status_field&
380
filter_help_status_source::statusview_value_for_field(int field)
6✔
381
{
382
    if (!this->fss_error.empty()) {
6✔
383
        return this->fss_error;
×
384
    }
385

386
    if (!this->fss_prompt.empty()) {
6✔
387
        return this->fss_prompt;
×
388
    }
389

390
    return this->fss_help;
6✔
391
}
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