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