sources/interface.cpp

changeset 17
50341dec533e
parent 16
33bac54867bf
child 20
5f8cdc8febbb
equal deleted inserted replaced
16:33bac54867bf 17:50341dec533e
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include <string.h> 31 #include <string.h>
32 #include "interface.h" 32 #include "interface.h"
33 #include "network/rconsession.h"
33 34
34 enum { PAGE_SIZE = 10 }; 35 enum { PAGE_SIZE = 10 };
35 36
36 static String g_input; 37 static String g_input;
37 static int g_cursor = 0; 38 static int g_cursor = 0;
38 static int g_pan = 0; 39 static int g_pan = 0;
39 static bool g_needRefresh = false; 40 static bool g_needRefresh = false;
40 static bool g_needStatusBarRender = false; 41 static bool g_needStatusBarRender = false;
41 static bool g_needInputRender = false; 42 static bool g_needInputRender = false;
43 static bool g_needOutputRender = false;
42 static String g_statusBarText; 44 static String g_statusBarText;
43 static struct { char ch; int x; } g_cursorChar; 45 static struct { char ch; int x; } g_cursorChar;
44 static Vector<String> g_output = {""}; 46 static Vector<String> g_output = {""};
45 static int g_outputScroll = 0; 47 static int g_outputScroll = 0;
46 48
65 } 67 }
66 68
67 // ------------------------------------------------------------------------------------------------- 69 // -------------------------------------------------------------------------------------------------
68 // 70 //
69 static FUNCTION 71 static FUNCTION
70 interface_render_log_area() -> void 72 interface_render_output() -> void
71 { 73 {
72 int height = LINES - 3; 74 int height = LINES - 3;
73 75
74 // ensure we're within bounds 76 // ensure we're within bounds
75 if (g_outputScroll + height >= g_output.size()) 77 if (g_outputScroll + height >= g_output.size())
85 for (int i = start; i < end; ++i) 87 for (int i = start; i < end; ++i)
86 { 88 {
87 mvhline (y, 0, ' ', COLS); 89 mvhline (y, 0, ' ', COLS);
88 mvprintw (y++, 0, "%s", g_output[i].chars()); 90 mvprintw (y++, 0, "%s", g_output[i].chars());
89 } 91 }
92
93 g_needRefresh = true;
90 } 94 }
91 95
92 // ------------------------------------------------------------------------------------------------- 96 // -------------------------------------------------------------------------------------------------
93 // 97 //
94 static FUNCTION 98 static FUNCTION
95 interface_render_input() -> void 99 interface_render_input() -> void
96 { 100 {
97 static char prompt[] = "> "; 101 static char prompt[] = "> ";
98 int displaylength = COLS - strlen (prompt) - 1; 102 int displaylength = COLS - strlen (prompt) - 1;
99 int y = LINES - 2; 103 int y = LINES - 2;
104
105 // Ensure the cursor is within bounds
106 g_cursor = clamp (g_cursor, 0, g_input.length());
100 107
101 // Ensure that the cursor is always in view, adjust panning if this is not the case 108 // Ensure that the cursor is always in view, adjust panning if this is not the case
102 if (g_cursor > g_pan + displaylength) 109 if (g_cursor > g_pan + displaylength)
103 g_pan = g_cursor - displaylength; // cursor went too far right 110 g_pan = g_cursor - displaylength; // cursor went too far right
104 else if (g_cursor < g_pan) 111 else if (g_cursor < g_pan)
157 // 164 //
158 static FUNCTION 165 static FUNCTION
159 interface_render_full() -> void 166 interface_render_full() -> void
160 { 167 {
161 interface_render_titlebar(); 168 interface_render_titlebar();
162 interface_render_log_area(); 169 interface_render_output();
163 interface_render_statusbar(); 170 interface_render_statusbar();
164 interface_render_input(); 171 interface_render_input();
165 } 172 }
166 173
167 // ------------------------------------------------------------------------------------------------- 174 // -------------------------------------------------------------------------------------------------
263 } 270 }
264 break; 271 break;
265 272
266 case KEY_PPAGE: 273 case KEY_PPAGE:
267 g_outputScroll += PAGE_SIZE; 274 g_outputScroll += PAGE_SIZE;
268 interface_render_log_area(); 275 g_needOutputRender = true;
269 g_needRefresh = true;
270 break; 276 break;
271 277
272 case KEY_NPAGE: 278 case KEY_NPAGE:
273 g_outputScroll -= PAGE_SIZE; 279 g_outputScroll -= PAGE_SIZE;
274 interface_render_log_area(); 280 g_needOutputRender = true;
275 g_needRefresh = true; 281 break;
276 break; 282
277 } 283 case '\n':
278 284 case KEY_ENTER:
285 if (RCONSession::get_session()->send_command (g_input))
286 {
287 g_input.clear();
288 g_needInputRender = true;
289 }
290 break;
291 }
292 }
293
294 // -------------------------------------------------------------------------------------------------
295 //
296 FUNCTION
297 Interface::render() -> void
298 {
279 if (g_needStatusBarRender) interface_render_statusbar(); 299 if (g_needStatusBarRender) interface_render_statusbar();
280 if (g_needInputRender) interface_render_input(); 300 if (g_needInputRender) interface_render_input();
301 if (g_needOutputRender) interface_render_output();
281 302
282 if (g_needRefresh) 303 if (g_needRefresh)
283 { 304 {
284 interface_position_cursor(); 305 interface_position_cursor();
285 refresh(); 306 refresh();
286 g_needRefresh = false; 307 g_needRefresh = false;
287 } 308 }
288 } 309 }
289 310
311 // -------------------------------------------------------------------------------------------------
312 //
290 FUNCTION print_to_console (const String& a) -> void 313 FUNCTION print_to_console (const String& a) -> void
291 { 314 {
292 for (char ch : a) 315 for (char ch : a)
293 { 316 {
294 if (ch == '\n') 317 if (ch == '\n')
298 } 321 }
299 322
300 g_output[g_output.size() - 1] += ch; 323 g_output[g_output.size() - 1] += ch;
301 } 324 }
302 325
303 interface_render_log_area(); 326 g_needOutputRender = true;
304 interface_position_cursor(); 327 }
305 refresh();
306 }

mercurial