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 	}
21 	/// Returns: width of termial
22 	@property int width(){
23 		return _term.width;
24 	}
25 	/// Returns: height of terminal
26 	@property int height(){
27 		return _term.height;
28 	}
29 	/// sets terminal colors. `fg` is foreground, `bg` is background
30 	void color(Color fg, Color bg){
31 		_term.color(fg, bg);
32 	}
33 	/// fills all cells with a character
34 	void fill(char ch){
35 		int _w = width, _h = height;
36 		char[] line;
37 		line.length = _w;
38 		line[] = ch;
39 		// write line _h times
40 		for (uint i = 0; i < _h; i ++){
41 			_term.moveTo(0,i);
42 			_term.write(line);
43 		}
44 	}
45 	/// fills a rectangle with a character
46 	void fill(char ch, int x1, int x2, int y1, int y2){
47 		char[] line;
48 		line.length = (x2 - x1) + 1;
49 		line[] = ch;
50 		foreach(i; y1 .. y2 +1){
51 			_term.moveTo(x1, i);
52 			_term.write(line);
53 		}
54 	}
55 	/// flush to terminal
56 	void flush(){
57 		_term.moveTo(width+1, height+1);
58 		_term.hideCursor();
59 		_term.flush();
60 	}
61 	/// writes a character `ch` at a position `(x, y)`
62 	void put(int x, int y, char ch){
63 		_term.moveTo(x, y);
64 		_term.write(ch);
65 	}
66 	/// writes a character `ch` at a position `(x, y)` with `fg` as foreground ang `bg` as background color
67 	void put(int x, int y, char ch, Color fg, Color bg){
68 		_term.color(fg, bg);
69 		_term.moveTo(x, y);
70 		_term.write(ch);
71 	}
72 	/// Returns: a key press which is a char, if no char pressed, returns 0x00
73 	char getKey(){
74 		KeyboardEvent k;
75 		k.which = _input.getch(true);
76 		if (k.isCharacter)
77 			return cast(char)k.which;
78 		return 0x00;
79 	}
80 }