Quick.Console.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. { ***************************************************************************
  2. Copyright (c) 2016-2017 Kike Pérez
  3. Unit : Quick.Console
  4. Description : Console output with colors and optional file log
  5. Author : Kike Pérez
  6. Version : 1.6
  7. Created : 10/05/2017
  8. Modified : 20/10/2017
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Console;
  22. {$IFDEF CONDITIONALEXPRESSIONS}
  23. {$ifndef VER140}
  24. {$ifndef LINUX}
  25. {$define WITHUXTHEME}
  26. {$endif}
  27. {$endif}
  28. {$IF CompilerVersion >= 17.0}
  29. {$DEFINE INLINES}
  30. {$ENDIF}
  31. {$IF RTLVersion >= 14.0}
  32. {$DEFINE HASERROUTPUT}
  33. {$ENDIF}
  34. {$ENDIF}
  35. interface
  36. uses
  37. Classes,
  38. Windows,
  39. Winapi.Messages,
  40. System.SysUtils,
  41. Quick.Commons,
  42. Quick.Log;
  43. type
  44. //text colors
  45. TConsoleColor = (
  46. ccBlack = 0,
  47. ccBlue = 1,
  48. ccGreen = 2,
  49. ccCyan = 3,
  50. ccRed = 4,
  51. ccMagenta = 5,
  52. ccBrown = 6,
  53. ccLightGray = 7,
  54. ccDarkGray = 8,
  55. ccLightBlue = 9,
  56. ccLightGreen = 10,
  57. ccLightCyan = 11,
  58. ccLightRed = 12,
  59. ccLightMagenta = 13,
  60. ccYellow = 14,
  61. ccWhite = 15);
  62. TConsoleProperties = record
  63. LogVerbose : TLogVerbose;
  64. Log : TQuickLog;
  65. end;
  66. procedure cout(const cMsg : Integer; cEventType : TEventType); overload;
  67. procedure cout(const cMsg : Double; cEventType : TEventType); overload;
  68. procedure cout(const cMsg : string; cEventType : TEventType); overload;
  69. procedure cout(const cMsg : string; cColor : TConsoleColor); overload;
  70. procedure coutXY(x,y : Integer; const s : string; cEventType : TEventType);
  71. procedure coutBL(const s : string; cEventType : TEventType);
  72. procedure coutFmt(const cMsg : string; params : array of const; cEventType : TEventType);
  73. procedure TextColor(Color: TConsoleColor); overload;
  74. procedure TextColor(Color: Byte); overload;
  75. procedure TextBackground(Color: TConsoleColor); overload;
  76. procedure TextBackground(Color: Byte); overload;
  77. procedure ResetColors;
  78. function ClearScreen : Boolean;
  79. procedure ClearLine; overload;
  80. procedure ClearLine(Y : Integer); overload;
  81. procedure ConsoleWaitForEnterKey;
  82. procedure InitConsole;
  83. var
  84. Console : TConsoleProperties;
  85. CSConsole : TRTLCriticalSection;
  86. LastMode : Word;
  87. DefConsoleColor : Byte;
  88. TextAttr : Byte;
  89. hStdOut: THandle;
  90. hStdErr: THandle;
  91. ConsoleRect: TSmallRect;
  92. ScreenBufInfo : TConsoleScreenBufferInfo;
  93. implementation
  94. procedure cout(const cMsg : Integer; cEventType : TEventType);
  95. var
  96. FmtSets : TFormatSettings;
  97. begin
  98. try
  99. FmtSets := TFormatSettings.Create;
  100. FmtSets.ThousandSeparator := '.';
  101. FmtSets.DecimalSeparator := ',';
  102. cout(FormatFloat('0,',cMsg,FmtSets),cEventType);
  103. except
  104. cout(cMsg.ToString,cEventType);
  105. end;
  106. end;
  107. procedure cout(const cMsg : Double; cEventType : TEventType);
  108. var
  109. FmtSets : TFormatSettings;
  110. begin
  111. try
  112. FmtSets := TFormatSettings.Create;
  113. FmtSets.ThousandSeparator := '.';
  114. FmtSets.DecimalSeparator := ',';
  115. cout(FormatFloat('.0###,',cMsg,FmtSets),cEventType);
  116. except
  117. cout(cMsg.ToString,cEventType);
  118. end;
  119. end;
  120. procedure cout(const cMsg : string; cEventType : TEventType);
  121. begin
  122. if cEventType in Console.LogVerbose then
  123. begin
  124. EnterCriticalSection(CSConsole);
  125. try
  126. if hStdOut <> 0 then
  127. begin
  128. case cEventType of
  129. etError : TextColor(ccLightRed);
  130. etInfo : TextColor(ccWhite);
  131. etSuccess : TextColor(ccLightGreen);
  132. etWarning : TextColor(ccYellow);
  133. etDebug : TextColor(ccLightCyan);
  134. etTrace : TextColor(ccLightMagenta);
  135. else TextColor(ccWhite);
  136. end;
  137. Writeln(cMsg);
  138. TextColor(LastMode);
  139. end;
  140. finally
  141. LeaveCriticalSection(CSConsole);
  142. end;
  143. if Assigned(Console.Log) then Console.Log.Add(cMsg,cEventType);
  144. end;
  145. end;
  146. procedure cout(const cMsg : string; cColor : TConsoleColor);
  147. begin
  148. EnterCriticalSection(CSConsole);
  149. try
  150. if hStdOut <> 0 then
  151. begin
  152. TextColor(cColor);
  153. Writeln(cMsg);
  154. TextColor(LastMode);
  155. end;
  156. finally
  157. LeaveCriticalSection(CSConsole);
  158. end;
  159. end;
  160. function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
  161. var
  162. BufferInfo: TConsoleScreenBufferInfo;
  163. begin
  164. GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
  165. Result := BufferInfo.dwCursorPosition.X;
  166. end;
  167. function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
  168. var
  169. BufferInfo: TConsoleScreenBufferInfo;
  170. begin
  171. GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
  172. Result := BufferInfo.dwCursorPosition.Y;
  173. end;
  174. function GetCursorMaxBottom : Integer;
  175. var
  176. BufferInfo: TConsoleScreenBufferInfo;
  177. begin
  178. GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
  179. Result := BufferInfo.srWindow.Bottom;
  180. end;
  181. procedure SetCursorPos(NewCoord : TCoord);
  182. begin
  183. SetConsoleCursorPosition(hStdOut, NewCoord);
  184. end;
  185. procedure coutXY(x,y : Integer; const s : string; cEventType : TEventType);
  186. var
  187. NewCoord : TCoord;
  188. LastCoord : TCoord;
  189. begin
  190. if hStdOut = 0 then Exit;
  191. LastCoord.X := GetCursorX;
  192. LastCoord.Y := GetCursorY;
  193. NewCoord.X := x;
  194. NewCoord.Y := y;
  195. ClearLine(Y);
  196. SetCursorPos(NewCoord);
  197. try
  198. cout(s,cEventType);
  199. finally
  200. SetCursorPos(LastCoord);
  201. end;
  202. end;
  203. procedure coutBL(const s : string; cEventType : TEventType);
  204. begin
  205. coutXY(0,GetCurSorMaxBottom - 1,s,cEventType);
  206. end;
  207. procedure coutFmt(const cMsg : string; params : array of const; cEventType : TEventType);
  208. begin
  209. cout(Format(cMsg,params),cEventType);
  210. end;
  211. procedure TextColor(Color: TConsoleColor);
  212. begin
  213. TextColor(Integer(Color));
  214. end;
  215. procedure TextColor(Color: Byte);
  216. begin
  217. if hStdOut = 0 then Exit;
  218. LastMode := TextAttr;
  219. TextAttr := (TextAttr and $F0) or (Color and $0F);
  220. if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
  221. end;
  222. procedure TextBackground(Color: TConsoleColor);
  223. begin
  224. TextBackground(Integer(Color));
  225. end;
  226. procedure TextBackground(Color: Byte);
  227. begin
  228. if hStdOut = 0 then Exit;
  229. LastMode := TextAttr;
  230. TextAttr := (TextAttr and $0F) or ((Color shl 4) and $F0);
  231. if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
  232. end;
  233. procedure ResetColors;
  234. begin
  235. SetConsoleTextAttribute(hStdOut, DefConsoleColor);
  236. TextAttr := DefConsoleColor;
  237. end;
  238. function ClearScreen : Boolean;
  239. const
  240. BUFSIZE = 80*25;
  241. var
  242. Han, Dummy: LongWord;
  243. buf: string;
  244. coord: TCoord;
  245. begin
  246. Result := false;
  247. Han := GetStdHandle(STD_OUTPUT_HANDLE);
  248. if Han <> INVALID_HANDLE_VALUE then
  249. begin
  250. if SetConsoleTextAttribute(han, FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE) then
  251. begin
  252. SetLength(buf,BUFSIZE);
  253. FillChar(buf[1],Length(buf),' ');
  254. if WriteConsole(han,PChar(buf),BUFSIZE,Dummy,nil) then
  255. begin
  256. coord.X := 0;
  257. coord.Y := 0;
  258. if SetConsoleCursorPosition(han,coord) then Result := true;
  259. end;
  260. end;
  261. end;
  262. end;
  263. procedure ClearLine;
  264. begin
  265. ClearLine(GetCursorY);
  266. end;
  267. procedure ClearLine(Y : Integer);
  268. var
  269. dwWriteCoord: TCoord;
  270. dwCount, dwSize: DWord;
  271. begin
  272. if hStdOut = 0 then Exit;
  273. dwWriteCoord.X := 0;
  274. dwWriteCoord.Y := Y;
  275. dwSize := ConsoleRect.Right + 1;
  276. FillConsoleOutputAttribute(hStdOut, TextAttr, dwSize, dwWriteCoord, dwCount);
  277. FillConsoleOutputCharacter(hStdOut, ' ', dwSize, dwWriteCoord, dwCount);
  278. end;
  279. function ConsoleKeyPressed(ExpectedKey: Word): Boolean;
  280. var
  281. lpNumberOfEvents: DWORD;
  282. lpBuffer: TInputRecord;
  283. lpNumberOfEventsRead : DWORD;
  284. nStdHandle: THandle;
  285. begin
  286. Result := False;
  287. nStdHandle := GetStdHandle(STD_INPUT_HANDLE);
  288. lpNumberOfEvents := 0;
  289. GetNumberOfConsoleInputEvents(nStdHandle, lpNumberOfEvents);
  290. if lpNumberOfEvents <> 0 then
  291. begin
  292. PeekConsoleInput(nStdHandle, lpBuffer, 1, lpNumberOfEventsRead);
  293. if lpNumberOfEventsRead <> 0 then
  294. begin
  295. if lpBuffer.EventType = KEY_EVENT then
  296. begin
  297. if lpBuffer.Event.KeyEvent.bKeyDown and ((ExpectedKey = 0) or (lpBuffer.Event.KeyEvent.wVirtualKeyCode = ExpectedKey)) then Result := true
  298. else FlushConsoleInputBuffer(nStdHandle);
  299. end
  300. else FlushConsoleInputBuffer(nStdHandle);
  301. end;
  302. end;
  303. end;
  304. procedure ConsoleWaitForEnterKey;
  305. var
  306. msg: TMsg;
  307. begin
  308. while not ConsoleKeyPressed(VK_RETURN) do
  309. begin
  310. {$ifndef LVCL}
  311. if GetCurrentThreadID=MainThreadID then CheckSynchronize{$ifdef WITHUXTHEME}(1000){$endif} else
  312. {$endif}
  313. WaitMessage;
  314. while PeekMessage(msg,0,0,0,PM_REMOVE) do
  315. begin
  316. if Msg.Message = WM_QUIT then Exit
  317. else
  318. begin
  319. TranslateMessage(Msg);
  320. DispatchMessage(Msg);
  321. end;
  322. end;
  323. end;
  324. end;
  325. procedure InitConsole;
  326. var
  327. BufferInfo: TConsoleScreenBufferInfo;
  328. begin
  329. Console.LogVerbose := LOG_ALL;
  330. Rewrite(Output);
  331. hStdOut := TTextRec(Output).Handle;
  332. {$IFDEF HASERROUTPUT}
  333. Rewrite(ErrOutput);
  334. hStdErr := TTextRec(ErrOutput).Handle;
  335. {$ELSE}
  336. hStdErr := GetStdHandle(STD_ERROR_HANDLE);
  337. {$ENDIF}
  338. if not GetConsoleScreenBufferInfo(hStdOut, BufferInfo) then
  339. begin
  340. SetInOutRes(GetLastError);
  341. Exit;
  342. end;
  343. ConsoleRect.Left := 0;
  344. ConsoleRect.Top := 0;
  345. ConsoleRect.Right := BufferInfo.dwSize.X - 1;
  346. ConsoleRect.Bottom := BufferInfo.dwSize.Y - 1;
  347. TextAttr := BufferInfo.wAttributes and $FF;
  348. DefConsoleColor := TextAttr;
  349. LastMode := 3; //CO80;
  350. end;
  351. initialization
  352. InitializeCriticalSection(CSConsole);
  353. //init stdout if not a service
  354. if GetStdHandle(STD_OUTPUT_HANDLE) <> 0 then InitConsole;
  355. finalization
  356. DeleteCriticalSection(CSConsole);
  357. end.