/cui/vt100.d

http://github.com/wilkie/djehuty · D · 218 lines · 174 code · 27 blank · 17 comment · 90 complexity · ae3b9d3529f48948ca2f4fae9bb45288 MD5 · raw file

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