blob: 0a8ddcf10c94ef4a19d7803dd38a0abf381e90cc [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
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 Geyer3d556242018-01-24 19:14:32 +010020unit ConsoleHelper;
Jens Geyerd5436f52014-10-03 19:50:38 +020021
22interface
23
Jens Geyer9f7f11e2016-04-14 21:37:11 +020024uses Classes;
Jens Geyerd5436f52014-10-03 19:50:38 +020025
26type
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 Geyer9f7f11e2016-04-14 21:37:11 +020036 FMemo : TStrings;
Jens Geyerd5436f52014-10-03 19:50:38 +020037
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 Geyer9f7f11e2016-04-14 21:37:11 +020042 constructor Create( AMemo: TStrings);
Jens Geyerd5436f52014-10-03 19:50:38 +020043 end;
44
45function Console: TThriftConsole;
46procedure ChangeConsole( AConsole: TThriftConsole );
47procedure RestoreConsoleToDefault;
48
49implementation
50
51var
52 FDefaultConsole : TThriftConsole;
53 FConsole : TThriftConsole;
54
55function Console: TThriftConsole;
56begin
57 Result := FConsole;
58end;
59
60{ TThriftConsole }
61
62procedure TThriftConsole.Write(const S: string);
63begin
64 System.Write( S );
65end;
66
67procedure TThriftConsole.WriteLine(const S: string);
68begin
69 System.Writeln( S );
70end;
71
72procedure ChangeConsole( AConsole: TThriftConsole );
73begin
74 FConsole := AConsole;
75end;
76
77procedure RestoreConsoleToDefault;
78begin
79 FConsole := FDefaultConsole;
80end;
81
82{ TGUIConsole }
83
Jens Geyer9f7f11e2016-04-14 21:37:11 +020084constructor TGUIConsole.Create( AMemo: TStrings);
Jens Geyerd5436f52014-10-03 19:50:38 +020085begin
86 inherited Create;
87 FMemo := AMemo;
88 FLineBreak := True;
89end;
90
91procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean);
92var
93 idx : Integer;
94begin
95 if FLineBreak then
96 begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +020097 FMemo.Add( S );
Jens Geyerd5436f52014-10-03 19:50:38 +020098 end else
99 begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200100 idx := FMemo.Count - 1;
Jens Geyerd5436f52014-10-03 19:50:38 +0200101 if idx < 0 then
Jens Geyerb636ffb2018-01-19 19:20:29 +0100102 FMemo.Add( S )
103 else
104 FMemo[idx] := FMemo[idx] + S;
Jens Geyerd5436f52014-10-03 19:50:38 +0200105 end;
106 FLineBreak := bWriteLine;
107end;
108
109procedure TGUIConsole.Write(const S: string);
110begin
111 InternalWrite( S, False);
112end;
113
114procedure TGUIConsole.WriteLine(const S: string);
115begin
116 InternalWrite( S, True);
117end;
118
119initialization
120begin
121 FDefaultConsole := TThriftConsole.Create;
122 FConsole := FDefaultConsole;
123end;
124
125finalization
126begin
127 FDefaultConsole.Free;
128end;
129
130end.
131
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200132