erebus

diff src/console.h @ 37:db8a90307386

implemented console and rudimentary commandline parser
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Jun 2014 16:01:00 +0300
parents
children 2e817711d0f6
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/console.h	Mon Jun 09 16:01:00 2014 +0300
     1.3 @@ -0,0 +1,82 @@
     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 +private:
    1.17 +	bool visible;
    1.18 +	int nlines, ncolumns;
    1.19 +
    1.20 +	int inpq_front, inpq_back;
    1.21 +	int keybuf[INPUTQ_SIZE];
    1.22 +
    1.23 +	std::string input;	// current input line
    1.24 +	int cursor;
    1.25 +
    1.26 +	// input history
    1.27 +	int max_hist_lines;
    1.28 +	std::list<std::string> hist;
    1.29 +
    1.30 +	// console output
    1.31 +	int max_output_lines;
    1.32 +	std::vector<std::string> output;
    1.33 +	bool echo;
    1.34 +
    1.35 +	std::function<void (const char*)> cmd_handler;
    1.36 +
    1.37 +	int font_size;
    1.38 +	dtx_font *font;
    1.39 +
    1.40 +public:
    1.41 +	enum {
    1.42 +		KEY_LEFT = 256,
    1.43 +		KEY_RIGHT,
    1.44 +		KEY_UP, KEY_DOWN,
    1.45 +		KEY_INS,
    1.46 +		KEY_DEL,
    1.47 +		KEY_HOME,
    1.48 +		KEY_END,
    1.49 +		KEY_PGUP,
    1.50 +		KEY_PGDOWN
    1.51 +	};
    1.52 +
    1.53 +	Console();
    1.54 +
    1.55 +	void set_font(dtx_font *font, int sz);
    1.56 +	void set_history_size(int hsz);
    1.57 +	void set_output_buffer_size(int sz);
    1.58 +	void set_command_func(std::function<void (const char*)> func);
    1.59 +
    1.60 +	void set_echo(bool echo);
    1.61 +	bool get_echo() const;
    1.62 +
    1.63 +	void show() { set_visible(true); }
    1.64 +	void hide() { set_visible(false); }
    1.65 +	void set_visible(bool v);
    1.66 +	bool is_visible() const;
    1.67 +
    1.68 +	void set_size(int lines, int columns = 80);
    1.69 +	int get_size_lines() const;
    1.70 +	int get_size_columns() const;
    1.71 +
    1.72 +	// update returns true if what it did makes a redraw necessary
    1.73 +	bool update();
    1.74 +	void draw() const;
    1.75 +
    1.76 +	void input_key(int key);	// send a keystroke to the console
    1.77 +
    1.78 +	void putchar(char c);
    1.79 +	void puts(const char *str);
    1.80 +	void printf(const char *fmt, ...);
    1.81 +
    1.82 +	void debug();
    1.83 +};
    1.84 +
    1.85 +#endif	// CONSOLE_H_