Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
Jens Geyer | 3d55624 | 2018-01-24 19:14:32 +0100 | [diff] [blame] | 20 | unit ConsoleHelper; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 21 | |
| 22 | interface |
| 23 | |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 24 | uses Classes, SysUtils, SyncObjs; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 25 | |
| 26 | type |
| 27 | TThriftConsole = class |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 28 | strict private |
| 29 | FLock : TCriticalSection; |
| 30 | strict protected |
| 31 | procedure Lock; |
| 32 | procedure UnLock; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | public |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 34 | constructor Create; |
| 35 | destructor Destroy; override; |
| 36 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | procedure Write( const S: string); virtual; |
| 38 | procedure WriteLine( const S: string); virtual; |
| 39 | end; |
| 40 | |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 41 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 42 | TGUIConsole = class( TThriftConsole ) |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 43 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 44 | FLineBreak : Boolean; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 45 | FMemo : TStrings; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 46 | |
| 47 | procedure InternalWrite( const S: string; bWriteLine: Boolean); |
| 48 | public |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 49 | constructor Create( AMemo: TStrings); |
| 50 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 51 | procedure Write( const S: string); override; |
| 52 | procedure WriteLine( const S: string); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 53 | end; |
| 54 | |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 55 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 56 | function Console: TThriftConsole; |
| 57 | procedure ChangeConsole( AConsole: TThriftConsole ); |
| 58 | procedure RestoreConsoleToDefault; |
| 59 | |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 60 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 61 | implementation |
| 62 | |
| 63 | var |
| 64 | FDefaultConsole : TThriftConsole; |
| 65 | FConsole : TThriftConsole; |
| 66 | |
| 67 | function Console: TThriftConsole; |
| 68 | begin |
| 69 | Result := FConsole; |
| 70 | end; |
| 71 | |
| 72 | { TThriftConsole } |
| 73 | |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 74 | constructor TThriftConsole.Create; |
| 75 | begin |
| 76 | inherited Create; |
| 77 | FLock := TCriticalSection.Create; |
| 78 | end; |
| 79 | |
| 80 | destructor TThriftConsole.Destroy; |
| 81 | begin |
| 82 | FreeAndNil( FLock); |
| 83 | inherited Destroy; |
| 84 | end; |
| 85 | |
| 86 | procedure TThriftConsole.Lock; |
| 87 | begin |
| 88 | FLock.Enter; |
| 89 | end; |
| 90 | |
| 91 | procedure TThriftConsole.UnLock; |
| 92 | begin |
| 93 | FLock.Leave; |
| 94 | end; |
| 95 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 96 | procedure TThriftConsole.Write(const S: string); |
| 97 | begin |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 98 | Lock; |
| 99 | try |
| 100 | System.Write( S ); |
| 101 | finally |
| 102 | Unlock; |
| 103 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | end; |
| 105 | |
| 106 | procedure TThriftConsole.WriteLine(const S: string); |
| 107 | begin |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 108 | Lock; |
| 109 | try |
| 110 | System.Writeln( S ); |
| 111 | finally |
| 112 | Unlock; |
| 113 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 114 | end; |
| 115 | |
| 116 | procedure ChangeConsole( AConsole: TThriftConsole ); |
| 117 | begin |
| 118 | FConsole := AConsole; |
| 119 | end; |
| 120 | |
| 121 | procedure RestoreConsoleToDefault; |
| 122 | begin |
| 123 | FConsole := FDefaultConsole; |
| 124 | end; |
| 125 | |
| 126 | { TGUIConsole } |
| 127 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 128 | constructor TGUIConsole.Create( AMemo: TStrings); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 129 | begin |
| 130 | inherited Create; |
| 131 | FMemo := AMemo; |
| 132 | FLineBreak := True; |
| 133 | end; |
| 134 | |
| 135 | procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean); |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 136 | var idx : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 137 | begin |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 138 | Lock; |
| 139 | try |
| 140 | |
| 141 | if FLineBreak then begin |
Jens Geyer | b636ffb | 2018-01-19 19:20:29 +0100 | [diff] [blame] | 142 | FMemo.Add( S ) |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 143 | end |
| 144 | else begin |
| 145 | idx := FMemo.Count - 1; |
| 146 | if idx < 0 |
| 147 | then FMemo.Add( S ) |
| 148 | else FMemo[idx] := FMemo[idx] + S; |
| 149 | end; |
| 150 | FLineBreak := bWriteLine; |
| 151 | |
| 152 | finally |
| 153 | Unlock; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 154 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 155 | end; |
| 156 | |
| 157 | procedure TGUIConsole.Write(const S: string); |
| 158 | begin |
| 159 | InternalWrite( S, False); |
| 160 | end; |
| 161 | |
| 162 | procedure TGUIConsole.WriteLine(const S: string); |
| 163 | begin |
| 164 | InternalWrite( S, True); |
| 165 | end; |
| 166 | |
| 167 | initialization |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 168 | FDefaultConsole := TThriftConsole.Create; |
| 169 | FConsole := FDefaultConsole; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 170 | |
| 171 | finalization |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 172 | FDefaultConsole.Free; |
Jens Geyer | 48d3bef | 2022-09-08 21:48:41 +0200 | [diff] [blame] | 173 | FDefaultConsole := nil; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | |
| 175 | end. |
| 176 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 177 | |