| 1 |
{ |
| 2 |
Copyright 2002-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ @abstract(Progress bar displayed on console (actually, on StdErr).) |
| 17 |
|
| 18 |
You can assign ProgressConsoleInterface to Progress.UserInterface, |
| 19 |
like |
| 20 |
|
| 21 |
@longCode(# Progress.UserInterface := ProgressConsoleInterface;#) |
| 22 |
|
| 23 |
and then all progress bars will be displayed on console. |
| 24 |
|
| 25 |
This displays a title surrounded by [] characters and the progress |
| 26 |
is indicated by displaying dots. This way we visualize progress |
| 27 |
incrementing from 0 to 100%, and at the same time we use only |
| 28 |
normal streaming I/O on StdErr. Totally no console/terminal specific |
| 29 |
operations, no special codes, no Crt / Curses unit's used etc. |
| 30 |
So this unit doesn't create any terminal compatibility problems, |
| 31 |
doesn't mess standard input/output/error streams etc. |
| 32 |
|
| 33 |
The only restriction is that you should not output anything |
| 34 |
(on stdout and stderr) to not mess the displayed progress bar. |
| 35 |
Of course, the worse that will happen is that the progress bar |
| 36 |
will stop looking good for user, nothing more. |
| 37 |
|
| 38 |
If you really want a progress bar that uses your terminal capabilities |
| 39 |
see ProgressVideo unit, that displays progress on terminal by FPC's |
| 40 |
Video unit. |
| 41 |
} |
| 42 |
unit CastleProgressConsole |
| 43 |
deprecated 'use TCastleView and WaitForRenderAndCall to display progress of loading operations'; |
| 44 |
|
| 45 |
{$I castleconf.inc} |
| 46 |
|
| 47 |
interface |
| 48 |
|
| 49 |
uses CastleProgress; |
| 50 |
|
| 51 |
type |
| 52 |
{ } |
| 53 |
TProgressConsoleInterface = class(TProgressUserInterface) |
| 54 |
private |
| 55 |
{ This will grow from 0 to ConsoleWidth. } |
| 56 |
DotsWritten: Integer; |
| 57 |
public |
| 58 |
procedure Init(Progress: TProgress); override; |
| 59 |
procedure Update(Progress: TProgress); override; |
| 60 |
procedure Fini(Progress: TProgress); override; |
| 61 |
end; |
| 62 |
|
| 63 |
var |
| 64 |
{ Assign this to Progress.UserInterface to use console progress bar. |
| 65 |
This instance is created in initialization, freed in finalization. } |
| 66 |
ProgressConsoleInterface: TProgressConsoleInterface; |
| 67 |
|
| 68 |
implementation |
| 69 |
|
| 70 |
uses |
| 71 |
SysUtils, CastleUtils; |
| 72 |
|
| 73 |
const |
| 74 |
{ This is the width of the console that we can write without automatically |
| 75 |
moving to the next line. |
| 76 |
|
| 77 |
Of course, this shouldn't be a constant, |
| 78 |
but actually there is no way here to do anything better. |
| 79 |
I can't use here any console/video/terminal functions, |
| 80 |
because this unit is supposed to work only with bare StdErr. } |
| 81 |
ConsoleWidth = 60; |
| 82 |
|
| 83 |
procedure Write(const s: string); |
| 84 |
begin System.Write(ErrOutput, s); end; |
| 85 |
|
| 86 |
procedure Writeln(const s: string); |
| 87 |
begin System.Writeln(ErrOutput, s); end; |
| 88 |
|
| 89 |
{ TProgressConsoleInterface -------------------------------------------------- } |
| 90 |
|
| 91 |
procedure TProgressConsoleInterface.Init(Progress: TProgress); |
| 92 |
var |
| 93 |
LeftSpace, RightSpace: integer; |
| 94 |
begin |
| 95 |
if Length(Progress.Title) > ConsoleWidth-2 then |
| 96 |
begin |
| 97 |
Writeln(Progress.Title); |
| 98 |
Writeln('[' +StringOfChar(' ', ConsoleWidth-2) +']'); |
| 99 |
end else |
| 100 |
begin |
| 101 |
{ since Length(Progress.Title) > ConsoleWidth-2 then |
| 102 |
2 <= ConsoleWidth-Length(Progress.Title) so |
| 103 |
0 <= (ConsoleWidth-Length(Progress.Title)) div 2 - 1. } |
| 104 |
LeftSpace := (ConsoleWidth-Length(Progress.Title)) div 2 - 1; |
| 105 |
RightSpace := ConsoleWidth - LeftSpace - Length(Progress.Title) - 2; |
| 106 |
Writeln('[' +StringOfChar(' ', LeftSpace) +Progress.Title + |
| 107 |
StringOfChar(' ', RightSpace) +']'); |
| 108 |
end; |
| 109 |
|
| 110 |
DotsWritten := 0; |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TProgressConsoleInterface.Update(Progress: TProgress); |
| 114 |
var |
| 115 |
DotsNow: integer; |
| 116 |
begin |
| 117 |
DotsNow := Progress.Position * ConsoleWidth div Progress.Max; |
| 118 |
if DotsNow > DotsWritten then |
| 119 |
begin |
| 120 |
Write(StringOfChar('.', DotsNow - DotsWritten)); |
| 121 |
DotsWritten := DotsNow; |
| 122 |
end; |
| 123 |
end; |
| 124 |
|
| 125 |
procedure TProgressConsoleInterface.Fini(Progress: TProgress); |
| 126 |
begin |
| 127 |
Writeln(''); |
| 128 |
end; |
| 129 |
|
| 130 |
initialization |
| 131 |
ProgressConsoleInterface := TProgressConsoleInterface.Create; |
| 132 |
finalization |
| 133 |
FreeAndNil(ProgressConsoleInterface); |
| 134 |
end. |