1 /**
2  * Copyright (c) 2015, 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 ON
25  * 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 #ifndef LNAV_FIELD_OVERLAY_SOURCE_H
31 #define LNAV_FIELD_OVERLAY_SOURCE_H
32 
33 #include <utility>
34 #include <vector>
35 
36 #include "listview_curses.hh"
37 #include "log_data_helper.hh"
38 #include "logfile_sub_source.hh"
39 #include "textfile_sub_source.hh"
40 
41 class field_overlay_source : public list_overlay_source {
42 public:
field_overlay_source(logfile_sub_source & lss,textfile_sub_source & tss)43     explicit field_overlay_source(logfile_sub_source &lss, textfile_sub_source &tss)
44             : fos_lss(lss), fos_tss(tss), fos_log_helper(lss) {
45 
46     };
47 
add_key_line_attrs(int key_size,bool last_line=false)48     void add_key_line_attrs(int key_size, bool last_line = false) {
49         string_attrs_t &sa = this->fos_lines.back().get_attrs();
50         struct line_range lr(1, 2);
51         sa.emplace_back(lr, &view_curses::VC_GRAPHIC, last_line ? ACS_LLCORNER : ACS_LTEE);
52 
53         lr.lr_start = 3 + key_size + 3;
54         lr.lr_end   = -1;
55         sa.emplace_back(lr, &view_curses::VC_STYLE, A_BOLD);
56     };
57 
list_value_for_overlay(const listview_curses & lv,int y,int bottom,vis_line_t row,attr_line_t & value_out)58     bool list_value_for_overlay(const listview_curses &lv,
59                                 int y, int bottom,
60                                 vis_line_t row,
61                                 attr_line_t &value_out) override {
62         if (y == 0) {
63             this->build_field_lines(lv);
64             this->build_summary_lines(lv);
65             return false;
66         }
67 
68         if (1 <= y && y <= (int)this->fos_lines.size()) {
69             value_out = this->fos_lines[y - 1];
70             return true;
71         }
72 
73         if (!this->fos_summary_lines.empty() && y == (bottom - 1)) {
74             value_out = this->fos_summary_lines[0];
75             return true;
76         }
77 
78         if (!this->fos_meta_lines.empty()) {
79             value_out = this->fos_meta_lines.front();
80             this->fos_meta_lines.erase(this->fos_meta_lines.begin());
81 
82             return true;
83         }
84 
85         if (row < lv.get_inner_height()) {
86             this->build_meta_line(lv, this->fos_meta_lines, row);
87         }
88 
89         return false;
90     };
91 
92     void build_field_lines(const listview_curses &lv);
93     void build_summary_lines(const listview_curses &lv);
94     void build_meta_line(const listview_curses &lv,
95                          std::vector<attr_line_t> &dst,
96                          vis_line_t row);
97 
98     struct context {
contextfield_overlay_source::context99         context(std::string prefix, bool show, bool show_discovered)
100             : c_prefix(std::move(prefix)), c_show(show),
101               c_show_discovered(show_discovered)
102         {}
103 
104         std::string c_prefix;
105         bool c_show{false};
106         bool c_show_discovered{true};
107     };
108 
109     bool fos_show_status{true};
110     std::stack<context> fos_contexts;
111     logfile_sub_source &fos_lss;
112     textfile_sub_source &fos_tss;
113     log_data_helper fos_log_helper;
114     int fos_known_key_size{0};
115     int fos_unknown_key_size{0};
116     std::vector<attr_line_t> fos_lines;
117     std::vector<attr_line_t> fos_summary_lines;
118     std::vector<attr_line_t> fos_meta_lines;
119 };
120 
121 #endif //LNAV_FIELD_OVERLAY_SOURCE_H
122