1 module terminalwrapper;
2 
3 import arsd.terminal;
4 
5 public alias Color = arsd.terminal.Color;
6 
7 /// Wrapper to arsd.terminal to make it work bit more like termbox-d
8 class TermWrapper{
9 private:
10 	Terminal _term;
11 	RealTimeConsoleInput _input;
12 public:
13 	/// constructor
14 	this(){
15 		_term = Terminal(ConsoleOutputType.cellular);
16 		_input = RealTimeConsoleInput(&_term,ConsoleInputFlags.allInputEvents);
17 	}
18 	~this(){
19 		_term.clear;
20 		.destroy(_input);
21 		.destroy(_term);
22 	}
23 	/// Returns: width of termial
24 	@property int width(){
25 		return _term.width;
26 	}
27 	/// Returns: height of terminal
28 	@property int height(){
29 		return _term.height;
30 	}
31 	/// sets terminal colors. `fg` is foreground, `bg` is background
32 	void color(Color fg, Color bg){
33 		_term.color(fg, bg);
34 	}
35 	/// fills all cells with a character
36 	void fill(char ch){
37 		int _w = width, _h = height;
38 		char[] line;
39 		line.length = _w;
40 		line[] = ch;
41 		// write line _h times
42 		for (uint i = 0; i < _h; i ++){
43 			_term.moveTo(0,i);
44 			_term.write(line);
45 		}
46 	}
47 	/// fills a rectangle with a character
48 	void fill(char ch, int x1, int x2, int y1, int y2){
49 		char[] line;
50 		line.length = (x2 - x1) + 1;
51 		line[] = ch;
52 		foreach(i; y1 .. y2 +1){
53 			_term.moveTo(x1, i);
54 			_term.write(line);
55 		}
56 	}
57 	/// flush to terminal
58 	void flush(){
59 		_term.moveTo(width+1, height+1);
60 		_term.hideCursor();
61 		_term.flush();
62 	}
63 	/// writes a character `ch` at a position `(x, y)`
64 	void put(int x, int y, char ch){
65 		_term.moveTo(x, y);
66 		_term.write(ch);
67 	}
68 	/// writes a character `ch` at a position `(x, y)` with `fg` as foreground ang `bg` as background color
69 	void put(int x, int y, char ch, Color fg, Color bg){
70 		_term.color(fg, bg);
71 		_term.moveTo(x, y);
72 		_term.write(ch);
73 	}
74 	/// Returns: a key press which is a char, if no char pressed, returns 0x00
75 	char getKey(){
76 		KeyboardEvent k;
77 		k.which = _input.getch(true);
78 		if (k.isCharacter)
79 			return cast(char)k.which;
80 		return 0x00;
81 	}
82 }