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 | |
| 20 | unit Thrift.Console; |
| 21 | |
| 22 | interface |
| 23 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 24 | uses Classes; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 25 | |
| 26 | type |
| 27 | TThriftConsole = class |
| 28 | public |
| 29 | procedure Write( const S: string); virtual; |
| 30 | procedure WriteLine( const S: string); virtual; |
| 31 | end; |
| 32 | |
| 33 | TGUIConsole = class( TThriftConsole ) |
| 34 | private |
| 35 | FLineBreak : Boolean; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 36 | FMemo : TStrings; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | |
| 38 | procedure InternalWrite( const S: string; bWriteLine: Boolean); |
| 39 | public |
| 40 | procedure Write( const S: string); override; |
| 41 | procedure WriteLine( const S: string); override; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 42 | constructor Create( AMemo: TStrings); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 43 | end; |
| 44 | |
| 45 | function Console: TThriftConsole; |
| 46 | procedure ChangeConsole( AConsole: TThriftConsole ); |
| 47 | procedure RestoreConsoleToDefault; |
| 48 | |
| 49 | implementation |
| 50 | |
| 51 | var |
| 52 | FDefaultConsole : TThriftConsole; |
| 53 | FConsole : TThriftConsole; |
| 54 | |
| 55 | function Console: TThriftConsole; |
| 56 | begin |
| 57 | Result := FConsole; |
| 58 | end; |
| 59 | |
| 60 | { TThriftConsole } |
| 61 | |
| 62 | procedure TThriftConsole.Write(const S: string); |
| 63 | begin |
| 64 | System.Write( S ); |
| 65 | end; |
| 66 | |
| 67 | procedure TThriftConsole.WriteLine(const S: string); |
| 68 | begin |
| 69 | System.Writeln( S ); |
| 70 | end; |
| 71 | |
| 72 | procedure ChangeConsole( AConsole: TThriftConsole ); |
| 73 | begin |
| 74 | FConsole := AConsole; |
| 75 | end; |
| 76 | |
| 77 | procedure RestoreConsoleToDefault; |
| 78 | begin |
| 79 | FConsole := FDefaultConsole; |
| 80 | end; |
| 81 | |
| 82 | { TGUIConsole } |
| 83 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 84 | constructor TGUIConsole.Create( AMemo: TStrings); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 85 | begin |
| 86 | inherited Create; |
| 87 | FMemo := AMemo; |
| 88 | FLineBreak := True; |
| 89 | end; |
| 90 | |
| 91 | procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean); |
| 92 | var |
| 93 | idx : Integer; |
| 94 | begin |
| 95 | if FLineBreak then |
| 96 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 97 | FMemo.Add( S ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 98 | end else |
| 99 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 100 | idx := FMemo.Count - 1; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 101 | if idx < 0 then |
| 102 | begin |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 103 | FMemo.Add( S ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 105 | FMemo[idx] := FMemo[idx] + S; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 106 | end; |
| 107 | FLineBreak := bWriteLine; |
| 108 | end; |
| 109 | |
| 110 | procedure TGUIConsole.Write(const S: string); |
| 111 | begin |
| 112 | InternalWrite( S, False); |
| 113 | end; |
| 114 | |
| 115 | procedure TGUIConsole.WriteLine(const S: string); |
| 116 | begin |
| 117 | InternalWrite( S, True); |
| 118 | end; |
| 119 | |
| 120 | initialization |
| 121 | begin |
| 122 | FDefaultConsole := TThriftConsole.Create; |
| 123 | FConsole := FDefaultConsole; |
| 124 | end; |
| 125 | |
| 126 | finalization |
| 127 | begin |
| 128 | FDefaultConsole.Free; |
| 129 | end; |
| 130 | |
| 131 | end. |
| 132 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 133 | |