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