1 /*
2 * console.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7 
8 #ifndef _CONSOLE
9 
10 #define _CONSOLE
11 
12 #define RED console::red
13 #define GREEN console::green
14 #define YELLOW console::yellow
15 #define CYAN console::cyan
16 #define tab "  "
17 #define eol console::EOL << YELLOW
18 
19 #include "dingl.h"
20 #include "globals.h"
21 #include "color.h"
22 #include <list>
23 #include <vector>
24 #include <string>
25 #include <sstream>
26 
27 #include "arrow_button.h"
28 
29 struct mesg {
30   std::string text; // text
31   color clr; // color
mesgmesg32   mesg (const std::string& t, const color& c) : text(t), clr(c) {}
mesgmesg33   mesg (const std::string& t) : text(t), clr (1, 1, 1) {}
mesgmesg34   mesg () : text (""), clr (1, 1, 1) {}
35 };
36 
37 typedef std::list<mesg>::iterator line_iterator;
38 
39 struct console : widget, pusher<console>, click_listener {
40 
41   int maxlines;
42 
43   // colors of text (defined in main.cc)
44   static const color yellow;
45   static const color green;
46   static const color red;
47   static const color cyan;
48 	static const color white;
49 
50   static const char EOL = '\n'; // end of last mesg
51   static const char CLL = '\b'; // clear last mesg
52   static const char* precision; // for printing float, double values
53 
54   // console text is a bunch of lines
55   static const int GUTTER = 7;
56   int nlines; // number of lines in console
57   int lines_per_screen;
58   int startl;
59   line_iterator it_startl;
60   void calc_startl_iterator ();
61   std::list<mesg> lines;
62   void add_line (const mesg& ln);
63   void clear ();
64   mesg cur_line; // currently edited line
65   color clr; // its color
66 
67   console ();
68   ~console ();
69 
70   console& operator() (const std::string& cmd); // run DIN command
71 
72   // operators for appending values to console
73   console& operator<< (unsigned int i);
74   console& operator<< (unsigned long i);
75   console& operator<< (unsigned long long i);
76   console& operator<< (int i);
77   console& operator<< (const std::string& s);
78   console& operator<< (float f);
79   console& operator<< (double d);
80   console& operator<< (char c);
81   console& operator<< (const color& d);
82 
83   /*
84   int suppress; // suppress line add
85   console& operator-- () { suppress = 1; return *this; } // console closed, no more lines can be added
86 	console& operator++ () { suppress = 0; return *this; } // console opened
87   */
88 
89   void up (int i); // scroll up by i lines
90   void down (int i); // scroll down
91   void pgdn (); // page down
92   void pgup (); // page up
93   void home (); // scroll to show 1st line
94   void end (); // scroll to show last line
95   void last (); // ensures last mesg is always displayed
96   void del (); // del one character from currently edited line
97 
98   // visual
99   //
100   arrow_button b_roll; // button to roll/unroll console
101   int rollup_; // show last line only?
102   int last_rollup_;
103   void rollup (int r);
rollupconsole104   int rollup () {return rollup_;}
105 
106   int char_width;
107   box<int> win;
108   void set_window (const box<int>& w);
109   int startx, starty;
110   int curs_loc;
111   int curs_locx;
112   void calc_visual_params ();
113   void draw ();
114 
115   int handle_input ();
116   void clicked (button& b);
117 
118   // commands
119   int command_mode;
120   mesg cmd_line;
121 	void clear_cmd_line ();
122 	void set_cmd_line (const std::string& s, const color& c = white);
123   void toggle_command_mode ();
124 
125   std::vector<std::string> history; // command history
126   int hid;
127 
128 };
129 
130 extern const char spc;
131 
132 template <class T> inline console& operator<< (console& c, const box<T>& b) {
133   c << b.left << spc << b.bottom << spc << b.right << spc << b.top << spc << b.width << spc << b.height;
134   return c;
135 }
136 
137 template <class T> inline console& operator<< (console& c, const point<T>& p) {
138 	c << p.x << spc << p.y;
139 	return c;
140 }
141 
142 extern console cons;
143 
144 #endif
145