ConsoleWindow.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. ConFrames - Gui Stuff for Console Windows
  3. Copyright (C) 2017-2018 Topten Software.
  4. ConFrames is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. ConFrames is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with ConFrames. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. namespace ConFrames
  21. {
  22. public class ConsoleWindow : Window
  23. {
  24. public ConsoleWindow(string title, Rect frameRectangle) : base(title, frameRectangle)
  25. {
  26. _buffer.Add(null);
  27. ScrollPos = 0;
  28. _prompt = ">";
  29. CursorVisible = true;
  30. CursorPosition = new Point(_prompt.Length, 0);
  31. }
  32. public string Prompt
  33. {
  34. get { return _prompt; }
  35. set { _prompt = value; }
  36. }
  37. public void Write(string str)
  38. {
  39. if (string.IsNullOrEmpty(str))
  40. return;
  41. str = str.Replace("\r\n", "\n").Replace('\r', '\n');
  42. var lines = str.Split('\n');
  43. bool trailingCR = str.EndsWith("\n");
  44. int lineCount = trailingCR ? lines.Length - 1 : lines.Length;
  45. int oldBufferSize = _buffer.Count;
  46. for (int i=0; i < lineCount; i++)
  47. {
  48. int count = _buffer.Count;
  49. if (_buffer[count - 1] == null)
  50. _buffer[count - 1] = lines[i];
  51. else
  52. _buffer[count - 1] = _buffer[count - 1] + lines[i];
  53. if (i < lineCount-1)
  54. _buffer.Add(null);
  55. }
  56. if (trailingCR)
  57. _buffer.Add(null);
  58. CursorY += _buffer.Count - oldBufferSize;
  59. Invalidate();
  60. ScrollToBottom();
  61. }
  62. public void WriteLine(string str)
  63. {
  64. Write(str);
  65. Write("\n");
  66. }
  67. public void WriteLine(string str, params object[] args)
  68. {
  69. WriteLine(string.Format(str, args));
  70. }
  71. public override void OnPaint(PaintContext ctx)
  72. {
  73. base.OnPaint(ctx);
  74. var height = ClientSize.Height;
  75. if (_prompt!=null)
  76. height--;
  77. int firstLine = ScrollPos;
  78. int lastLine = ScrollPos + height + 1;
  79. if (lastLine > TrimmedBufferLines)
  80. lastLine = TrimmedBufferLines;
  81. if (firstLine < _buffer.Count)
  82. {
  83. for (int i=firstLine; i<lastLine; i++)
  84. {
  85. var line = _buffer[i];
  86. ctx.WriteLine(line == null ? "" : line);
  87. }
  88. }
  89. if (lastLine == TrimmedBufferLines && _prompt!= null)
  90. {
  91. ctx.Write(_prompt);
  92. ctx.WriteLine(_inputBuffer);
  93. }
  94. }
  95. int _scrollPos;
  96. public int ScrollPos
  97. {
  98. get
  99. {
  100. return _scrollPos;
  101. }
  102. set
  103. {
  104. if (value < 0)
  105. value = 0;
  106. if (value > TrimmedBufferLines)
  107. value = TrimmedBufferLines;
  108. if (_scrollPos == value)
  109. return;
  110. CursorY += _scrollPos - value;
  111. _scrollPos = value;
  112. Invalidate();
  113. }
  114. }
  115. List<string> _buffer = new List<string>();
  116. string _prompt = null;
  117. string _inputBuffer = "";
  118. void RedrawPrompt()
  119. {
  120. var ctx = GetPaintContext();
  121. ctx.ClearLineOnReturn = true;
  122. ctx.Position = new Point(0, TrimmedBufferLines - ScrollPos);
  123. ctx.Write(_prompt);
  124. ctx.WriteLine(_inputBuffer);
  125. }
  126. int TrimmedBufferLines
  127. {
  128. get
  129. {
  130. int count = _buffer.Count;
  131. if (_buffer[count - 1] == null)
  132. count--;
  133. return count;
  134. }
  135. }
  136. void ScrollToBottom()
  137. {
  138. ScrollPos = _buffer.Count - ClientSize.Height;
  139. if (ScrollPos < 0)
  140. ScrollPos = 0;
  141. }
  142. public override bool OnKey(ConsoleKeyInfo key)
  143. {
  144. int posInBuffer = CursorX - _prompt.Length;
  145. switch (key.Key)
  146. {
  147. case ConsoleKey.LeftArrow:
  148. if (posInBuffer > 0)
  149. CursorX--;
  150. ScrollToBottom();
  151. return true;
  152. case ConsoleKey.RightArrow:
  153. if (posInBuffer < _inputBuffer.Length)
  154. CursorX++;
  155. ScrollToBottom();
  156. return true;
  157. case ConsoleKey.UpArrow:
  158. if (key.Modifiers == ConsoleModifiers.Shift)
  159. ScrollPos--;
  160. else
  161. OnCommandHistory(-1);
  162. return true;
  163. case ConsoleKey.DownArrow:
  164. if (key.Modifiers == ConsoleModifiers.Shift)
  165. ScrollPos++;
  166. else
  167. OnCommandHistory(1);
  168. return true;
  169. case ConsoleKey.Home:
  170. CursorX = _prompt.Length;
  171. ScrollToBottom();
  172. return true;
  173. case ConsoleKey.End:
  174. CursorX = _prompt.Length + _inputBuffer.Length;
  175. ScrollToBottom();
  176. return true;
  177. case ConsoleKey.Backspace:
  178. if (posInBuffer > 0)
  179. {
  180. _inputBuffer = _inputBuffer.Substring(0, posInBuffer - 1) + _inputBuffer.Substring(posInBuffer);
  181. RedrawPrompt();
  182. CursorX--;
  183. }
  184. ScrollToBottom();
  185. return true;
  186. case ConsoleKey.Delete:
  187. if (posInBuffer < _inputBuffer.Length)
  188. {
  189. _inputBuffer = _inputBuffer.Substring(0, posInBuffer) + _inputBuffer.Substring(posInBuffer + 1);
  190. RedrawPrompt();
  191. }
  192. ScrollToBottom();
  193. return true;
  194. case ConsoleKey.Escape:
  195. _inputBuffer = "";
  196. RedrawPrompt();
  197. CursorX = _prompt.Length;
  198. ScrollToBottom();
  199. return true;
  200. case ConsoleKey.Enter:
  201. {
  202. WriteLine(_prompt + _inputBuffer);
  203. string str = _inputBuffer;
  204. _inputBuffer = "";
  205. CursorX = _prompt.Length;
  206. ScrollToBottom();
  207. OnCommand(str);
  208. return true;
  209. }
  210. }
  211. if (key.KeyChar!=0)
  212. {
  213. _inputBuffer = _inputBuffer.Substring(0, posInBuffer) + key.KeyChar + _inputBuffer.Substring(posInBuffer);
  214. CursorX++;
  215. ScrollToBottom();
  216. RedrawPrompt();
  217. return true;
  218. }
  219. return false;
  220. }
  221. public void SetInputBuffer(string str)
  222. {
  223. _inputBuffer = str;
  224. RedrawPrompt();
  225. CursorX = _prompt.Length + _inputBuffer.Length;
  226. ScrollToBottom();
  227. }
  228. List<string> _commandHistory = new List<String>();
  229. int _commandHistoryPos;
  230. protected virtual void OnCommand(string command)
  231. {
  232. if (_commandHistory.Count>0)
  233. {
  234. if (_commandHistory[_commandHistory.Count - 1] == command)
  235. {
  236. _commandHistoryPos = _commandHistory.Count;
  237. return;
  238. }
  239. }
  240. _commandHistory.Add(command);
  241. _commandHistoryPos = _commandHistory.Count;
  242. }
  243. void OnCommandHistory(int delta)
  244. {
  245. int newPos = _commandHistoryPos + delta;
  246. if (newPos < 0)
  247. return;
  248. if (newPos >= _commandHistory.Count)
  249. {
  250. SetInputBuffer("");
  251. return;
  252. }
  253. else
  254. {
  255. _commandHistoryPos = newPos;
  256. SetInputBuffer(_commandHistory[newPos]);
  257. }
  258. }
  259. }
  260. }