vrheights

diff src/console.h @ 5:053a52f0cb64

console
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Sep 2014 18:40:15 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/console.h	Fri Sep 26 18:40:15 2014 +0300
     1.3 @@ -0,0 +1,96 @@
     1.4 +#ifndef CONSOLE_H_
     1.5 +#define CONSOLE_H_
     1.6 +
     1.7 +#include <vector>
     1.8 +#include <list>
     1.9 +#include <string>
    1.10 +#include <functional>
    1.11 +#include <drawtext.h>
    1.12 +
    1.13 +#define INPUTQ_SIZE		64
    1.14 +
    1.15 +class Console {
    1.16 +public:
    1.17 +	enum Anchor {TOP_LEFT, BOTTOM_LEFT, CENTER};
    1.18 +
    1.19 +private:
    1.20 +	bool visible;
    1.21 +	int nlines, ncolumns;
    1.22 +	int pos_x, pos_y;
    1.23 +	Anchor anchor;
    1.24 +
    1.25 +	int inpq_front, inpq_back;
    1.26 +	int keybuf[INPUTQ_SIZE];
    1.27 +
    1.28 +	std::string prompt;
    1.29 +	std::string input;	// current input line
    1.30 +	int cursor;
    1.31 +	int input_win;		// first character to show in the input "window"
    1.32 +
    1.33 +	// input history
    1.34 +	int max_hist_lines;
    1.35 +	std::list<std::string> hist;
    1.36 +	bool hist_iter_valid;
    1.37 +	std::list<std::string>::const_reverse_iterator hist_iter;
    1.38 +
    1.39 +	// console output
    1.40 +	int max_output_lines;
    1.41 +	std::vector<std::string> output;
    1.42 +	bool echo;
    1.43 +
    1.44 +	std::function<void (const char*)> cmd_handler;
    1.45 +
    1.46 +	int font_size;
    1.47 +	dtx_font *font;
    1.48 +
    1.49 +	void set_cursor(int x);
    1.50 +	int get_cursor() const;
    1.51 +
    1.52 +public:
    1.53 +	enum {
    1.54 +		KEY_LEFT = 256,
    1.55 +		KEY_RIGHT,
    1.56 +		KEY_UP, KEY_DOWN,
    1.57 +		KEY_INS,
    1.58 +		KEY_DEL,
    1.59 +		KEY_HOME,
    1.60 +		KEY_END,
    1.61 +		KEY_PGUP,
    1.62 +		KEY_PGDOWN
    1.63 +	};
    1.64 +
    1.65 +	Console();
    1.66 +
    1.67 +	void set_font(dtx_font *font, int sz);
    1.68 +	void set_history_size(int hsz);
    1.69 +	void set_output_buffer_size(int sz);
    1.70 +	void set_command_func(std::function<void (const char*)> func);
    1.71 +
    1.72 +	void set_echo(bool echo);
    1.73 +	bool get_echo() const;
    1.74 +
    1.75 +	void show() { set_visible(true); }
    1.76 +	void hide() { set_visible(false); }
    1.77 +	void set_visible(bool v);
    1.78 +	bool is_visible() const;
    1.79 +
    1.80 +	void set_size(int lines, int columns = 80);
    1.81 +	int get_size_lines() const;
    1.82 +	int get_size_columns() const;
    1.83 +
    1.84 +	void set_position(int x, int y, Anchor anchor = TOP_LEFT);
    1.85 +
    1.86 +	// update returns true if what it did makes a redraw necessary
    1.87 +	bool update();
    1.88 +	void draw() const;
    1.89 +
    1.90 +	void input_key(int key);	// send a keystroke to the console
    1.91 +
    1.92 +	void putchar(char c);
    1.93 +	void puts(const char *str);
    1.94 +	void printf(const char *fmt, ...);
    1.95 +
    1.96 +	void debug();
    1.97 +};
    1.98 +
    1.99 +#endif	// CONSOLE_H_