node_panel.cpp
Go to the documentation of this file.
1 
29 
30 namespace log_view {
31 
33  size_t cursor = getCursor();
34 
35  max_length_ = 0;
36  if (!cleared_) {
37  werase(window_);
38  }
39  cleared_ = false;
40 
41  box(window_, 0, 0);
42  mvwprintw(window_, 0, width_ / 2 - 3, " nodes ");
43 
44  size_t start_idx = cursor;
45  if (start_idx >= getContentHeight()) {
46  start_idx -= getContentHeight();
47  }
48  else {
49  start_idx = 0;
50  }
51 
52  std::vector<std::pair<std::string, NodeData>> nodes;
53  for (const auto& node: filter_.nodes()) {
54  nodes.push_back(node);
55  }
56 
57  bool selection_visible = false;
58  for (size_t i = 0; i < getContentHeight() && i + start_idx < getContentSize(); i++) {
59  auto name = nodes[i + start_idx].first;
60  if (name == selected_) {
61  selection_visible = true;
62  break;
63  }
64  }
65 
66  // force in focus element to be visible
67  if (!selection_visible) {
68  moveTo(cursor);
69  }
70 
71  for (size_t i = 0; i < getContentHeight() && i + start_idx < getContentSize(); i++) {
72  const auto& node_data = nodes[i + start_idx].second;
73  auto name = nodes[i + start_idx].first;
74  bool hover = focus_ && (name == selected_);
75  bool selected = !node_data.exclude;
76 
77  std::string text = name + ": " + std::to_string(node_data.count);
78 
79  if (selected) {
80  wattron(window_, A_REVERSE);
81  }
82 
83  if (hover) {
84  wattron(window_, A_BOLD);
85  }
86 
87  max_length_ = std::max(max_length_, text.size());
88 
89  if (shift_ >= text.size()) {
90  text.clear();
91  }
92  else if (shift_ > 0) {
93  text.erase(0, shift_ + 2);
94  }
95 
96  bool cropped = false;
97  if (text.size() > getContentWidth()) {
98  text.resize(getContentWidth() - 2);
99  cropped = true;
100  }
101 
102  mvwprintw(window_, i + 1, 1, text.c_str());
103 
104  if (hover) {
105  wattroff(window_, A_BOLD);
106  }
107 
108  if (selected) {
109  wattroff(window_, A_REVERSE);
110  }
111 
112  if (shift_ > 0) {
113  mvwprintw(window_, i + 1, 1, " ");
114  wattron(window_, A_REVERSE);
115  mvwprintw(window_, i + 1, 1, "<");
116  wattroff(window_, A_REVERSE);
117  }
118  if (cropped) {
119  mvwprintw(window_, i + 1, getContentWidth() - 1, " ");
120  wattron(window_, A_REVERSE);
121  mvwprintw(window_, i + 1, getContentWidth(), ">");
122  wattroff(window_, A_REVERSE);
123  }
124  }
125 
126  last_cursor_ = cursor;
127 
129 }
130 
131 bool NodePanel::handleKey(int key) {
132  if (hidden()) {
133  return false;
134  }
135 
136  if (key == ctrl('a')) {
138 
139  return true;
140  }
141  else if (key == ctrl('i')) {
143 
144  return true;
145  }
146 
147  return false;
148 }
149 
150 
151 bool NodePanel::handleMouse(const MEVENT& event) {
152  if (hidden() || !encloses(event.y, event.x)) {
153  return false;
154  }
155 
156  if (event.bstate & BUTTON1_PRESSED) {
157  int row = event.y - (y_ + 1);
158  size_t cursor = getCursor();
159  size_t start_idx = cursor;
160  if (start_idx >= getContentHeight()) {
161  start_idx -= getContentHeight();
162  }
163  else {
164  start_idx = 0;
165  }
166 
167  size_t index = start_idx + row;
168  if (index >= filter_.nodes().size()) {
169  return true;
170  }
171 
172  std::vector<std::pair<std::string, NodeData>> nodes;
173  for (const auto& node: filter_.nodes()) {
174  nodes.push_back(node);
175  }
176 
177  selected_ = nodes[index].first;
179  refresh();
180  }
181  return true;
182 }
183 
184 
185 void NodePanel::follow(bool enable) {
186  if (getContentSize() == 0) {
187  return;
188  }
189  selected_ = filter_.nodes().rbegin()->first;
190  moveTo(getCursor());
191 }
192 
193 void NodePanel::moveTo(size_t index) {
194  if (getContentSize() == 0) {
195  return;
196  }
197 
198  size_t cursor = getCursor();
199  int64_t offset = static_cast<int64_t>(index) - cursor;
200 
201  if (cursor == 0) {
202  cursor = getContentHeight();
203  setCursor(cursor);
204  }
205 
206  std::vector<std::pair<std::string, NodeData>> nodes;
207  int64_t selection = -1;
208  size_t idx = 0;
209  for (const auto& node: filter_.nodes()) {
210  nodes.push_back(node);
211  if (node.first == selected_) {
212  selection = idx;
213  }
214  idx++;
215  }
216 
217  if (selection < 0 || index == 0) {
218  selection = 0;
219  }
220  else {
221  selection += offset;
222  selection = std::max(static_cast<int64_t>(0), std::min(static_cast<int64_t>(getContentSize()) - 1, selection));
223  }
224  selected_ = nodes[selection].first;
225 
226  size_t start_idx = cursor;
227  if (start_idx >= getContentHeight()) {
228  start_idx -= getContentHeight();
229  }
230  else {
231  start_idx = 0;
232  }
233 
234  if (selection < start_idx) {
235  setCursor(selection + getContentHeight());
236  }
237  else if (selection >= start_idx + getContentHeight())
238  {
239  setCursor(selection + 1);
240  }
241 }
242 
244  int width = width_ - 2;
245  if (getContentSize() > getContentHeight()) {
246  width--;
247  }
248  return width;
249 }
250 
253 }
254 
255 } // namespace log_view
virtual bool encloses(int y, int x)
virtual void refresh()
Definition: node_panel.cpp:32
virtual int width() const
LogFilter & filter_
Definition: node_panel.h:60
void toggleNode(const std::string &node)
Definition: log_filter.cpp:116
const std::map< std::string, NodeData > & nodes() const
Definition: log_filter.h:89
virtual bool hidden() const
virtual size_t getContentSize() const
Definition: node_panel.h:48
virtual void select()
Definition: node_panel.cpp:251
virtual int getContentWidth() const
Definition: node_panel.cpp:243
std::string selected_
Definition: node_panel.h:58
virtual bool handleMouse(const MEVENT &event)
Definition: node_panel.cpp:151
virtual int getContentHeight() const
Definition: node_panel.h:49
virtual void moveTo(size_t index)
Definition: node_panel.cpp:193
virtual void follow(bool enable)
Definition: node_panel.cpp:185
int ctrl(char key)
Definition: utils.cpp:121
virtual bool handleKey(int key)
Definition: node_panel.cpp:131
virtual void drawScrollBar(size_t count, int height, int y, int x)
virtual int64_t getCursor() const
Definition: node_panel.h:54


log_view
Author(s): Marc Alban
autogenerated on Thu Mar 4 2021 03:21:52