PageRenderTime 73ms CodeModel.GetById 25ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 1ms

/cui/vt100.d

http://github.com/wilkie/djehuty
D | 218 lines | 174 code | 27 blank | 17 comment | 90 complexity | ae3b9d3529f48948ca2f4fae9bb45288 MD5 | raw file
  1module cui.vt100;
  2
  3import djehuty;
  4
  5import io.console;
  6
  7import cui.buffer;
  8
  9// Section: Console
 10
 11// Description: This console control is a console buffer that emulations VT100 terminal codes.
 12class CuiVT100 : CuiBuffer {
 13
 14	// Constructors
 15
 16	this( uint x, uint y, uint width, uint height) {
 17		super(x,y,width,height);
 18	}
 19
 20	alias CuiBuffer.writeChar writeChar;
 21
 22	override void writeChar(dchar chr) {
 23		if (_vt100_inescape2) {
 24			if (chr >= '0' && chr <= '9') {
 25				// another number,
 26				// add to current param
 27				_vt100_params[_vt100_curparam] *= 10;
 28				_vt100_params[_vt100_curparam] += chr - cast(ubyte)'0';
 29				_vt100_paramFilled = 1;
 30			}
 31			else if (chr == ';') {
 32				// goto next param
 33				if (_vt100_curparam < 4) {
 34					_vt100_curparam++;
 35					_vt100_params[_vt100_curparam] = 0;
 36					_vt100_paramFilled = 0;
 37				}
 38			}
 39
 40			if ((chr >= 'a' && chr <= 'z') ||
 41				(chr >= 'A' && chr <= 'Z')) {
 42				if (_vt100_curparam < 4 && _vt100_paramFilled != 0) {
 43					_vt100_curparam++;
 44				}
 45				// found a code
 46				// interpret this
 47				if (chr == 'J') {
 48					if (_vt100_params[0] == 2) {
 49						_screenfeed();
 50					}
 51					else {
 52					}
 53				}
 54				else if (chr == 's') {
 55					// save position
 56					_vt100_saved_x = _curx-this.left;
 57					_vt100_saved_y = _cury-this.top;
 58				}
 59				else if (chr == 'u') {
 60					// restore position
 61					position(_vt100_saved_x,_vt100_saved_y);
 62				}
 63				else if (chr == 'A') {
 64					if (_vt100_params[0] == 0) {
 65						_vt100_params[0] = 1;
 66					}
 67					setRelative(0, -_vt100_params[0]);
 68				}
 69				else if (chr == 'B') {
 70					if (_vt100_params[0] == 0) {
 71						_vt100_params[0] = 1;
 72					}
 73					setRelative(0, _vt100_params[0]);
 74				}
 75				else if (chr == 'C') {
 76					if (_vt100_params[0] == 0) {
 77						_vt100_params[0] = 1;
 78					}
 79					setRelative(_vt100_params[0], 0);
 80				}
 81				else if (chr == 'D') {
 82					if (_vt100_params[0] == 0) {
 83						_vt100_params[0] = 1;
 84					}
 85					setRelative(-_vt100_params[0], 0);
 86				}
 87				else if (chr == 'H' || chr == 'f') {
 88					// set cursor position
 89					if (_vt100_params[1] == 0) {
 90						_vt100_params[1] = 1;
 91					}
 92					if (_vt100_params[0] == 0) {
 93						_vt100_params[0] = 1;
 94					}
 95
 96					position(_vt100_params[1]-1, _vt100_params[0]-1);
 97				}
 98				else if (chr == 'm') {
 99					// color
100
101					int fgclr=-1;
102					int bgclr=-1;
103					int bright=-1;
104
105					for(uint i=0; i<_vt100_curparam; i++) {
106						if (_vt100_params[i] >= 30 && _vt100_params[i] <= 37) {
107							fgclr = _vt100_params[i] - 30;
108						}
109						else if (_vt100_params[i] == 39) {
110							fgclr = 8;
111						}
112						else if (_vt100_params[i] >= 40 && _vt100_params[i] <= 47) {
113							bgclr = _vt100_params[i] - 40;
114						}
115						else if (_vt100_params[i] == 49) {
116							bgclr = 0;
117						}
118						else if (_vt100_params[i] == 0) {
119							bright = 0;
120							fgclr = 8;
121							bgclr = 0;
122						}
123						else if (_vt100_params[i] < 2) {
124							bright = _vt100_params[i];
125						}
126						else if (_vt100_params[i] == 7) {
127							// invert the colors
128						}
129					}
130
131					if (bright != -1) {
132					    _cur_bright_color = bright;
133					}
134
135					if (fgclr != -1) {
136						_cur_fg_color = fgclr;
137					}
138
139					if (bgclr != -1) {
140						_cur_bg_color = bgclr;
141				    }
142
143				    _cur_fg_color = _cur_fg_color % 8;
144				    _cur_fg_color += (8 * _cur_bright_color);
145
146				    _curfg = _consoleToColor[_cur_fg_color];
147				    _curbg = _consoleToColor[_cur_bg_color];
148
149				    setColors(_curfg, _curbg);
150				}
151				else {
152					//Console.putln("!!!!!", chr , "!!!!!");
153				}
154
155				_vt100_inescape2 = false;
156			}
157			return;
158		}
159		else if (_vt100_inescape) {
160			if (chr == '[') {
161				_vt100_inescape2 = true;
162				_vt100_inescape = false;
163				return;
164			}
165			_vt100_inescape = false;
166		}
167
168		if (chr == 27) {
169			_vt100_curparam = 0;
170			_vt100_inescape = true;
171			_vt100_params[0] = 0;
172			_vt100_params[1] = 0;
173			_vt100_params[2] = 0;
174			_vt100_paramFilled = 0;
175		}
176		else {
177			//Console.position(0, this.bottom+2);
178			//Console.setColor(fgColor.White);
179			//Console.put(_curx, ", ", _cury, " : ", _buffer.length());
180			super.writeChar(chr);
181		}
182	}
183
184private:
185
186	static bool _vt100_inescape = false;
187	static bool _vt100_inescape2 = false;
188
189	static uint _vt100_saved_x = 0;
190	static uint _vt100_saved_y = 0;
191
192	static int _vt100_params[5] = [0];
193	static int _vt100_curparam = 0;
194	static int _vt100_paramFilled = 0;
195
196	static int _cur_fg_color = 8;
197	static int _cur_bg_color = 0;
198	static uint _cur_bright_color = 0;
199
200	static const Color[] _consoleToColor = [
201		Color.Black,
202		Color.DarkRed,
203		Color.DarkGreen,
204		Color.DarkYellow,
205		Color.DarkBlue,
206		Color.DarkMagenta,
207		Color.DarkCyan,
208		Color.DarkGray,
209		Color.Gray,
210		Color.Red,
211		Color.Green,
212		Color.Yellow,
213		Color.Blue,
214		Color.Magenta,
215		Color.Cyan,
216		Color.White
217	];
218}