summaryrefslogtreecommitdiffstats
path: root/src/breadcrumb_curses.cc
blob: 623222137a019721545ec7c76a1a7c0a4314b0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
 * Copyright (c) 2022, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "breadcrumb_curses.hh"

#include "base/itertools.hh"
#include "itertools.similar.hh"

using namespace lnav::roles::literals;

void
breadcrumb_curses::no_op_action(breadcrumb_curses&)
{
}

breadcrumb_curses::breadcrumb_curses()
{
    this->bc_match_search_overlay.sos_parent = this;
    this->bc_match_source.set_reverse_selection(true);
    this->bc_match_view.set_title("breadcrumb popup");
    this->bc_match_view.set_selectable(true);
    this->bc_match_view.set_overlay_source(&this->bc_match_search_overlay);
    this->bc_match_view.set_sub_source(&this->bc_match_source);
    this->bc_match_view.set_height(0_vl);
    this->bc_match_view.set_show_scrollbar(true);
    this->bc_match_view.set_default_role(role_t::VCR_POPUP);
    this->add_child_view(&this->bc_match_view);
}

bool
breadcrumb_curses::do_update()
{
    if (!this->bc_line_source) {
        return false;
    }

    size_t crumb_index = 0;
    size_t sel_crumb_offset = 0;
    auto width = static_cast<size_t>(getmaxx(this->bc_window));
    auto crumbs = this->bc_focused_crumbs.empty() ? this->bc_line_source()
                                                  : this->bc_focused_crumbs;
    if (this->bc_last_selected_crumb
        && this->bc_last_selected_crumb.value() >= crumbs.size())
    {
        this->bc_last_selected_crumb = crumbs.size() - 1;
    }
    this->bc_displayed_crumbs.clear();
    attr_line_t crumbs_line;
    for (const auto& crumb : crumbs) {
        auto accum_width = crumbs_line.column_width();
        auto elem_width = crumb.c_display_value.column_width();
        auto is_selected = this->bc_selected_crumb
            && (crumb_index == this->bc_selected_crumb.value());

        if (is_selected && ((accum_width + elem_width) > width)) {
            crumbs_line.clear();
            crumbs_line.append("\u22ef\uff1a"_breadcrumb);
            accum_width = 2;
        }

        line_range crumb_range;
        crumb_range.lr_start = (int) crumbs_line.length();
        crumbs_line.append(crumb.c_display_value);
        crumb_range.lr_end = (int) crumbs_line.length();
        if (is_selected) {
            sel_crumb_offset = accum_width;
            crumbs_line.get_attrs().emplace_back(
                crumb_range, VC_STYLE.template value(text_attrs{A_REVERSE}));
        }

        this->bc_displayed_crumbs.emplace_back(
            line_range{(int) accum_width,
                       (int) (accum_width + elem_width),
                       line_range::unit::codepoint},
            crumb_index);
        crumb_index += 1;
        crumbs_line.append(" \uff1a"_breadcrumb);
    }

    line_range lr{0, static_cast<int>(width)};
    view_curses::mvwattrline(
        this->bc_window, this->vc_y, 0, crumbs_line, lr, role_t::VCR_STATUS);

    if (this->bc_selected_crumb) {
        this->bc_match_view.set_x(sel_crumb_offset);
    }
    view_curses::do_update();

    return true;
}

void
breadcrumb_curses::reload_data()
{
    if (!this->bc_selected_crumb) {
        return;
    }

    auto& selected_crumb_ref
        = this->bc_focused_crumbs[this->bc_selected_crumb.value()];
    this->bc_possible_values = selected_crumb_ref.c_possibility_provider();

    nonstd::optional<size_t> selected_value;
    this->bc_similar_values = this->bc_possible_values
        | lnav::itertools::similar_to(
                                  [](const auto& elem) { return elem.p_key; },
                                  this->bc_current_search,
                                  128)
        | lnav::itertools::sort_with(breadcrumb::possibility::sort_cmp);
    if (selected_crumb_ref.c_key.is<std::string>()
        && selected_crumb_ref.c_expected_input
            != breadcrumb::crumb::expected_input_t::anything)
    {
        auto& selected_crumb_key = selected_crumb_ref.c_key.get<std::string>();
        auto found_poss_opt = this->bc_similar_values
            | lnav::itertools::find_if([&selected_crumb_key](const auto& elem) {
                                  return elem.p_key == selected_crumb_key;
                              });

        if (found_poss_opt) {
            selected_value = std::distance(this->bc_similar_values.begin(),
                                           found_poss_opt.value());
        } else {
            selected_value = 0;
        }
    }

    auto matches = attr_line_t().join(
        this->bc_similar_values
            | lnav::itertools::map(&breadcrumb::possibility::p_display_value),
        "\n");
    this->bc_match_source.replace_with(matches);
    auto width = this->bc_possible_values
        | lnav::itertools::fold(
                     [](const auto& match, auto& accum) {
                         auto mlen = match.p_display_value.length();
                         if (mlen > accum) {
                             return mlen;
                         }
                         return accum;
                     },
                     selected_crumb_ref.c_display_value.length());

    if (static_cast<ssize_t>(selected_crumb_ref.c_search_placeholder.size())
        > width)
    {
        width = selected_crumb_ref.c_search_placeholder.size();
    }
    this->bc_match_view.set_height(vis_line_t(
        std::min(this->bc_match_source.get_lines().size() + 1, size_t{4})));
    this->bc_match_view.set_width(width + 3);
    this->bc_match_view.set_needs_update();
    this->bc_match_view.set_selection(
        vis_line_t(selected_value.value_or(-1_vl)));
    if (selected_value) {
        this->bc_match_view.set_top(vis_line_t(selected_value.value()));
    }
    this->bc_match_view.reload_data();
    this->set_needs_update();
}

void
breadcrumb_curses::focus()
{
    this->bc_match_view.set_y(this->vc_y + 1);
    this->bc_focused_crumbs = this->bc_line_source();
    if (this->bc_focused_crumbs.empty()) {
        return;
    }

    this->bc_current_search.clear();
    if (this->bc_last_selected_crumb
        && this->bc_last_selected_crumb.value()
            >= this->bc_focused_crumbs.size())
    {
        this->bc_last_selected_crumb = this->bc_focused_crumbs.size() - 1;
    }
    this->bc_selected_crumb = this->bc_last_selected_crumb.value_or(0);
    this->reload_data();
}

void
breadcrumb_curses::blur()
{
    this->bc_last_selected_crumb = this->bc_selected_crumb;
    this->bc_focused_crumbs.clear();
    this->bc_selected_crumb = nonstd::nullopt;
    this->bc_current_search.clear();
    this->bc_match_view.set_height(0_vl);
    this->bc_match_view.set_selection(-1_vl);
    this->bc_match_source.clear();
    this->set_needs_update();
}

bool
breadcrumb_curses::handle_key(int ch)
{
    bool retval = false;

    switch (ch) {
        case KEY_CTRL('a'):
            if (this->bc_selected_crumb) {
                this->bc_selected_crumb = 0;
                this->bc_current_search.clear();
                this->reload_data();
            }
            retval = true;
            break;
        case KEY_CTRL('e'):
            if (this->bc_selected_crumb) {
                this->bc_selected_crumb = this->bc_focused_crumbs.size() - 1;
                this->bc_current_search.clear();
                this->reload_data();
            }
            retval = true;
            break;
        case KEY_BTAB:
        case KEY_LEFT:
            if (this->bc_selected_crumb) {
                if (this->bc_selected_crumb.value() > 0) {
                    this->bc_selected_crumb
                        = this->bc_selected_crumb.value() - 1;
                } else {
                    this->bc_selected_crumb
                        = this->bc_focused_crumbs.size() - 1;
                }
                this->bc_current_search.clear();
                this->reload_data();
            }
            retval = true;
            break;
        case '\t':
        case KEY_RIGHT:
            if (this->bc_selected_crumb) {
                this->perform_selection(perform_behavior_t::if_different);
                this->blur();
                this->focus();
                this->reload_data();
                if (this->bc_selected_crumb.value()
                    < this->bc_focused_crumbs.size() - 1)
                {
                    this->bc_selected_crumb
                        = this->bc_selected_crumb.value() + 1;
                    retval = true;
                }
                this->bc_current_search.clear();
                this->reload_data();
            } else {
                retval = true;
            }
            break;
        case KEY_HOME:
            this->bc_match_view.set_selection(0_vl);
            retval = true;
            break;
        case KEY_END:
            this->bc_match_view.set_selection(
                this->bc_match_view.get_inner_height() - 1_vl);
            retval = true;
            break;
        case KEY_NPAGE:
            this->bc_match_view.shift_selection(
                listview_curses::shift_amount_t::down_page);
            retval = true;
            break;
        case KEY_PPAGE:
            this->bc_match_view.shift_selection(
                listview_curses::shift_amount_t::up_page);
            retval = true;
            break;
        case KEY_UP:
            this->bc_match_view.shift_selection(
                listview_curses::shift_amount_t::up_line);
            retval = true;
            break;
        case KEY_DOWN:
            this->bc_match_view.shift_selection(
                listview_curses::shift_amount_t::down_line);
            retval = true;
            break;
        case KEY_DELETE:
        case KEY_BACKSPACE:
            if (!this->bc_current_search.empty()) {
                this->bc_current_search.pop_back();
                this->reload_data();
            }
            retval = true;
            break;
        case KEY_ENTER:
        case '\r':
            this->perform_selection(perform_behavior_t::always);
            break;
        case KEY_ESCAPE:
            break;
        default:
            if (isprint(ch)) {
                this->bc_current_search.push_back(ch);
                this->reload_data();
                retval = true;
            }
            break;
    }

    if (!retval) {
        this->blur();
    }
    this->set_needs_update();
    return retval;
}

void
breadcrumb_curses::perform_selection(
    breadcrumb_curses::perform_behavior_t behavior)
{
    if (!this->bc_selected_crumb) {
        return;
    }

    auto& selected_crumb_ref
        = this->bc_focused_crumbs[this->bc_selected_crumb.value()];
    auto match_sel = this->bc_match_view.get_selection();
    if (match_sel >= 0
        && match_sel < vis_line_t(this->bc_similar_values.size()))
    {
        const auto& new_value = this->bc_similar_values[match_sel].p_key;

        switch (behavior) {
            case perform_behavior_t::if_different:
                if (breadcrumb::crumb::key_t{new_value}
                    == selected_crumb_ref.c_key)
                {
                    return;
                }
                break;
            case perform_behavior_t::always:
                break;
        }
        selected_crumb_ref.c_performer(new_value);
    } else if (!this->bc_current_search.empty()) {
        switch (selected_crumb_ref.c_expected_input) {
            case breadcrumb::crumb::expected_input_t::exact:
                break;
            case breadcrumb::crumb::expected_input_t::index:
            case breadcrumb::crumb::expected_input_t::index_or_exact: {
                size_t index;

                if (sscanf(this->bc_current_search.c_str(), "%zu", &index) == 1)
                {
                    selected_crumb_ref.c_performer(index);
                }
                break;
            }
            case breadcrumb::crumb::expected_input_t::anything:
                selected_crumb_ref.c_performer(this->bc_current_search);
                break;
        }
    }
}

bool
breadcrumb_curses::search_overlay_source::list_static_overlay(
    const listview_curses& lv, int y, int bottom, attr_line_t& value_out)
{
    if (y != 0) {
        return false;
    }
    auto* parent = this->sos_parent;
    auto sel_opt = parent->bc_focused_crumbs
        | lnav::itertools::nth(parent->bc_selected_crumb);
    auto exp_input = sel_opt
        | lnav::itertools::map(&breadcrumb::crumb::c_expected_input)
        | lnav::itertools::unwrap_or(
                         breadcrumb::crumb::expected_input_t::exact);

    value_out.with_attr_for_all(VC_STYLE.value(text_attrs{A_UNDERLINE}));

    if (!parent->bc_current_search.empty()) {
        value_out = parent->bc_current_search;

        role_t combobox_role = role_t::VCR_STATUS;
        switch (exp_input) {
            case breadcrumb::crumb::expected_input_t::exact:
                if (parent->bc_similar_values.empty()) {
                    combobox_role = role_t::VCR_ALERT_STATUS;
                }
                break;
            case breadcrumb::crumb::expected_input_t::index: {
                size_t index;

                if (sscanf(parent->bc_current_search.c_str(), "%zu", &index)
                        != 1
                    || index < 0
                    || (index
                        >= (sel_opt | lnav::itertools::map([](const auto& cr) {
                                return cr->c_possible_range.value_or(0);
                            })
                            | lnav::itertools::unwrap_or(size_t{0}))))
                {
                    combobox_role = role_t::VCR_ALERT_STATUS;
                }
                break;
            }
            case breadcrumb::crumb::expected_input_t::index_or_exact: {
                size_t index;

                if (sscanf(parent->bc_current_search.c_str(), "%zu", &index)
                    == 1)
                {
                    if (index < 0
                        || (index
                            >= (sel_opt
                                | lnav::itertools::map([](const auto& cr) {
                                      return cr->c_possible_range.value_or(0);
                                  })
                                | lnav::itertools::unwrap_or(size_t{0}))))
                    {
                        combobox_role = role_t::VCR_ALERT_STATUS;
                    }
                } else if (parent->bc_similar_values.empty()) {
                    combobox_role = role_t::VCR_ALERT_STATUS;
                }
                break;
            }
            case breadcrumb::crumb::expected_input_t::anything:
                break;
        }
        value_out.with_attr_for_all(VC_ROLE.value(combobox_role));
        return true;
    }
    if (parent->bc_selected_crumb) {
        auto& selected_crumb_ref
            = parent->bc_focused_crumbs[parent->bc_selected_crumb.value()];

        if (!selected_crumb_ref.c_search_placeholder.empty()) {
            value_out = selected_crumb_ref.c_search_placeholder;
            value_out.with_attr_for_all(
                VC_ROLE.value(role_t::VCR_INACTIVE_STATUS));
            return true;
        }
    }

    return false;
}

bool
breadcrumb_curses::handle_mouse(mouse_event& me)
{
    if (me.me_state == mouse_button_state_t::BUTTON_STATE_PRESSED
        && this->bc_focused_crumbs.empty())
    {
        this->focus();
        this->on_focus(*this);
        this->do_update();
    }

    auto find_res = this->bc_displayed_crumbs
        | lnav::itertools::find_if([&me](const auto& elem) {
                        return me.me_button == mouse_button_t::BUTTON_LEFT
                            && elem.dc_range.contains(me.me_x);
                    });

    if (!this->bc_focused_crumbs.empty()) {
        if (me.me_y > 0 || !find_res
            || find_res.value()->dc_index == this->bc_selected_crumb)
        {
            if (view_curses::handle_mouse(me)) {
                if (me.me_y > 0
                    && (me.me_state
                            == mouse_button_state_t::BUTTON_STATE_DOUBLE_CLICK
                        || me.me_state
                            == mouse_button_state_t::BUTTON_STATE_RELEASED))
                {
                    this->perform_selection(perform_behavior_t::if_different);
                    this->blur();
                    this->reload_data();
                    this->on_blur(*this);
                    this->bc_initial_mouse_event = true;
                }
                return true;
            }
        }
        if (!this->bc_initial_mouse_event
            && me.me_state == mouse_button_state_t::BUTTON_STATE_RELEASED
            && me.me_y == 0 && find_res
            && find_res.value()->dc_index == this->bc_selected_crumb.value())
        {
            this->blur();
            this->reload_data();
            this->on_blur(*this);
            this->bc_initial_mouse_event = true;
            return true;
        }
    }

    if (me.me_state == mouse_button_state_t::BUTTON_STATE_RELEASED) {
        this->bc_initial_mouse_event = false;
    }

    if (me.me_y != 0) {
        return true;
    }

    if (find_res) {
        auto crumb_index = find_res.value()->dc_index;

        if (this->bc_selected_crumb) {
            this->blur();
            this->focus();
            this->reload_data();
            this->bc_selected_crumb = crumb_index;
            this->bc_current_search.clear();
            this->reload_data();
        }
    }

    return true;
}