help_panel.cpp
Go to the documentation of this file.
1 
29 
30 #include <cmath>
31 #include <log_view/utils.h>
32 
33 namespace log_view {
34 
35 // TODO handle case where terminal is too small
36 
37 // TODO support show/hide timestamps
38 
39 HelpPanel::HelpPanel(int height, int width, int y, int x) :
40  PanelInterface(height, width, y, x),
41  keys_({
42  {2, "CTRL-c", "Exit log viewer"},
43  {3, "CTRL-h", "Show/hide this help screen"},
44  {5, "F1", "Show/hide debug level"},
45  {6, "F2", "Show/hide info level"},
46  {7, "F3", "Show/hide warning level"},
47  {8, "F4", "Show/hide error level"},
48  {9, "F5", "Show/hide fatal level"},
49  {10, "F7", "Enable/disable node filter"},
50  {11, "CTRL-a", "Select all"},
51  {12, "CTRL-i", "Invert node selection"},
52  {13, "CTRL-n", "Show/hide node selection"},
53  {14, "CTRL-s", "Search for matching string"},
54  {15, "CTRL-x", "Clear search"},
55  {16, "Backspace", "Prev match"},
56  {17, "Enter", "Next match"},
57  {18, "CTRL-e", "Enable/disable text exclude filter"},
58  {19, "CTRL-f", "Enable/disable text include filter"}})
59 {
60  for (const auto& key: keys_) {
61  longest_key_ = std::max(longest_key_, key.key.length());
62  }
63 
64  for (const auto& key: keys_) {
65  longest_line_ = std::max(longest_line_, longest_key_ + 11 + key.description.length());
66  }
67 }
68 
70  box(window_, 0, 0);
71  mvwprintw(window_, 0, width_ / 2 - 3, " help ");
72  for (const auto& key: keys_) {
73  printKeybinding(key);
74  }
75 }
76 
77 void HelpPanel::resize(int height, int width, int y, int x) {
78  int w = width;
79  if (w > longest_line_) {
80  w = longest_line_;
81  x += (width - w) / 2;
82  }
83  PanelInterface::resize(height, w, y, x);
84 }
85 
86 bool HelpPanel::handleKey(int key) {
87  if (hidden()) {
88  return false;
89  }
90 
91  if (key == ctrl('h')) {
92  hide(visible());
93  }
94  else if (key == KEY_RESIZE || key == ctrl('q') || key == ctrl('c')) {
95  return false;
96  }
97 
98  return true;
99 }
100 
101 void HelpPanel::printKeybinding(const HelpText& help_text) {
102  mvwprintw(window_, help_text.line, 3, help_text.key.c_str());
103 
104  int max_size = std::max(0, width() - (static_cast<int>(longest_key_) + 10));
105  auto desc = help_text.description;
106  if (desc.size() > max_size) {
107  desc.resize(max_size);
108  }
109  mvwprintw(window_, help_text.line, longest_key_ + 8, desc.c_str());
110 
111  int line_start = help_text.key.length() + 4;
112  int line_end = longest_key_ + 7;
113 
114  wattron(window_, COLOR_PAIR(CP_GREY));
115  mvwhline(window_, help_text.line, line_start, 0, line_end - line_start);
116  wattroff(window_, COLOR_PAIR(CP_GREY));
117 }
118 
119 } // namespace log_view
std::string key
Definition: help_panel.h:40
virtual void refresh()
Definition: help_panel.cpp:69
virtual int width() const
virtual void resize(int height, int width, int y, int x)
virtual int y() const
virtual void resize(int height, int width, int y, int x)
Definition: help_panel.cpp:77
virtual bool hidden() const
virtual void hide(bool enable)
void printKeybinding(const HelpText &text)
Definition: help_panel.cpp:101
std::vector< HelpText > keys_
Definition: help_panel.h:58
virtual int x() const
virtual bool visible() const
int ctrl(char key)
Definition: utils.cpp:121
virtual bool handleKey(int key)
Definition: help_panel.cpp:86
std::string description
Definition: help_panel.h:41
HelpPanel(int height, int width, int y, int x)
Definition: help_panel.cpp:39
virtual int height() const


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