Goby3  3.1.5
2024.05.14
flex_ncurses.h
Go to the documentation of this file.
1 // Copyright 2012-2021:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_UTIL_DEBUG_LOGGER_FLEX_NCURSES_H
26 #define GOBY_UTIL_DEBUG_LOGGER_FLEX_NCURSES_H
27 
28 #include <cstddef> // for size_t
29 #include <map> // for multimap, multimap<>...
30 #include <set> // for set
31 #include <string> // for string
32 #include <vector> // for vector
33 
34 #include <boost/date_time/posix_time/ptime.hpp> // for ptime
35 
36 #include "term_color.h" // for Colors, Colors::Color
37 
38 namespace goby
39 {
40 namespace util
41 {
42 namespace logger
43 {
44 class Group;
45 }
46 
49 {
50  public:
52 
53  FlexNCurses() = default;
55  {
56  alive_ = false;
57  cleanup();
58  }
60 
62 
63  void startup();
64  void add_win(const logger::Group* title);
66 
68 
69  void insert(boost::posix_time::ptime t, const std::string& s, logger::Group* g);
72 
74 
76  void recalculate_win();
77  void cleanup();
78  void alive(bool alive);
80 
82 
83  void run_input();
86 
87  private:
88  void putline(const std::string& s, unsigned scrn, bool refresh = true);
89 
90  void putlines(unsigned scrn,
91  const std::multimap<boost::posix_time::ptime, std::string>::const_iterator& alpha,
92  const std::multimap<boost::posix_time::ptime, std::string>::const_iterator& omega,
93  bool refresh = true);
94 
95  void update_size();
96  long color2attr_t(Colors::Color c);
97  size_t find_containing_window(int y, int x);
98 
99  // void* is WINDOW*
100  bool in_window(void* p, int y, int x);
101 
102  void write_head_title(size_t i);
103  void deselect_all();
104  void select_all();
105  void select(size_t gt);
106  void home();
107  void end();
108 
109  size_t down() { return down(find_first_selected()); }
110  size_t up() { return up(find_first_selected()); }
111  size_t left() { return left(find_first_selected()); }
112  size_t right() { return right(find_first_selected()); }
113  size_t down(size_t curr);
114  size_t up(size_t curr);
115  size_t left(size_t curr);
116  size_t right(size_t curr);
117  size_t find_first_selected();
118  void shift(size_t next);
119 
120  bool last_in_col(size_t val);
121  bool first_in_col(size_t val);
122 
123  void move_up();
124  void move_down();
125  void move_right();
126  void move_left();
127 
128  void combine();
129  void uncombine_all();
130  void uncombine(size_t i);
131  void uncombine_selected();
132  int lines_from_beg(int l, size_t i);
133 
134  void scroll_up();
135  void scroll_down();
136  void page_up();
137  void page_down();
138  void scroll_end();
139  void scroll_home();
140 
141  void winlock();
142  void winunlock();
143 
144  void grow_all();
145  void shrink_all();
146  void grow(int i);
147  void shrink(int i);
148 
149  void restore_order();
150 
151  void toggle_minimized(int i);
152  void redraw_lines(int j, int offset = -1);
153 
154  std::multimap<boost::posix_time::ptime, std::string> get_history(size_t i, int how_much = -1);
155  size_t get_history_size(size_t i);
156 
157  template <typename T> T min(const T& a, const T& b) { return (a < b ? a : b); }
158 
159  template <typename T> T max(const T& a, const T& b) { return (a > b ? a : b); }
160 
161  private:
162  int xmax_{0};
163  int ymax_{0};
164 
165  int xwinN_{1};
166  int ywinN_{1};
167 
168  int last_select_x_;
169  int last_select_y_;
170 
171  // void* is WINDOW*
172  std::vector<void*> vert_windows_;
173  // bottom of the column (indexed by column)
174  std::vector<void*> col_end_windows_;
175  void* foot_window_{nullptr};
176 
177  bool is_locked_{false};
178  int locked_panel_{0};
179 
180  class Panel
181  {
182  public:
183  Panel(const logger::Group* group = nullptr) : group_(group) {}
184 
185  void window(void* v) { window_ = v; }
186  void head_window(void* v) { head_window_ = v; }
187  void group(const logger::Group* g) { group_ = g; }
188  // returns number of lines to grow/shrink
189  void set_minimized(bool b) { minimized_ = b; }
190  int minimized(bool b);
191  int toggle_minimized() { return minimized(minimized() ^ true); }
192  void selected(bool b) { selected_ = b; }
193  void ywidth(int i) { ywidth_ = i; }
194  int lines_from_beg(int i);
195  void grow() { ++ywidth_; }
196  void locked(bool b) { locked_ = b; }
197  bool shrink()
198  {
199  if (ywidth_ == HEAD_Y)
200  return false;
201  else
202  {
203  --ywidth_;
204  return true;
205  }
206  }
207  void col(int i) { col_ = i; }
208  void original_order(int i) { original_order_ = i; }
209  void add_combined(size_t i) { combined_.insert(i); }
210  void clear_combined() { combined_.clear(); }
211 
212  void* window() const { return window_; }
213  void* head_window() const { return head_window_; }
214  const logger::Group* group() const { return group_; }
215  bool minimized() const { return minimized_; }
216  bool selected() const { return selected_; }
217  int ywidth() const { return ywidth_; }
218  int col() const { return col_; }
219  bool locked() const { return locked_; }
220  int lines_from_beg() const { return lines_from_beg_; }
221  int original_order() const { return original_order_; }
222  std::multimap<boost::posix_time::ptime, std::string>& history() { return history_; }
223  unsigned max_hist() const { return max_hist_; }
224  const std::set<size_t>& combined() const { return combined_; }
225 
226  private:
227  const logger::Group* group_;
228  void* window_{nullptr};
229  void* head_window_{nullptr};
230  bool minimized_{false};
231  bool selected_{false};
232  bool locked_{false};
233  int ywidth_;
234  int unminimized_ywidth_;
235  int col_{1};
236  int lines_from_beg_{0};
237  int original_order_{0};
238  unsigned max_hist_{20000};
239  std::multimap<boost::posix_time::ptime, std::string> history_;
240  std::set<size_t> combined_;
241  };
242 
243  std::vector<Panel> panels_;
244  std::set<size_t> unique_panels_;
245 
246  // extra lines if everyone is minimized
247  // indexed by column
248  std::vector<int> line_buffer_;
249 
250  bool alive_{true};
251 
252  // ideal number of characters per line
253  enum
254  {
255  CHARS_PER_LINE = 85
256  };
257 
258  enum
259  {
260  HEAD_Y = 1
261  };
262  enum
263  {
264  FOOTER_Y = 3
265  };
266 };
267 } // namespace util
268 } // namespace goby
269 
270 #endif
goby::util::logger::Group
Defines a group of messages to be sent to the Goby logger. For Verbosity == verbose streams,...
Definition: logger_manipulators.h:68
goby::util::FlexNCurses::FlexNCurses
FlexNCurses()=default
goby::util::FlexNCurses::~FlexNCurses
~FlexNCurses()
Definition: flex_ncurses.h:54
goby::util::Colors::Color
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:111
goby::util::FlexNCurses::recalculate_win
void recalculate_win()
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::FlexNCurses::startup
void startup()
term_color.h
group
goby::util::logger::GroupSetter group(std::string n)
Definition: logger_manipulators.h:134
goby::util::FlexNCurses::run_input
void run_input()
run in its own thread to take input from the user
goby::util::FlexNCurses::panel_from_group
size_t panel_from_group(logger::Group *g)
goby::util::FlexNCurses::insert
void insert(boost::posix_time::ptime t, const std::string &s, logger::Group *g)
Add a string to the gui.
goby::util::FlexNCurses::cleanup
void cleanup()
goby::util::FlexNCurses
Enables the Verbosity == gui mode of the Goby logger and displays an NCurses gui for the logger conte...
Definition: flex_ncurses.h:48
goby::util::FlexNCurses::alive
void alive(bool alive)
goby::util::FlexNCurses::add_win
void add_win(const logger::Group *title)