unit DEngLogParagraph;

//
//  Simplifies and standardize printing of long paragraphs.
//

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

uses
  DEng,
  DEngLog;

type
TLogParagraphPrinter = object
private
  FPreviousLine: AnsiString;
public
  procedure Init;
  procedure Done;

  procedure Start(const Heading: AnsiString);
  procedure Finish;

  procedure Line(const S: AnsiString);
end;

implementation

procedure TLogParagraphPrinter.Init;
begin
end;

procedure TLogParagraphPrinter.Done;
begin
end;

procedure TLogParagraphPrinter.Start(const Heading: AnsiString);
var
  I, First, Len: PtrInt;
  HLine: AnsiString;
begin
  First := 1;
  while (First <= Length(Heading)) and (Heading[First] = ' ') do
    Inc(First);
  Len := 0;
  while (First + Len <= Length(Heading)) and (Heading[First + Len] <> ' ') do
    Inc(Len);
  SetLength(HLine, First - 1 + Len);
  for I := 1 to First - 1 do
    HLine[I] := ' ';
  for I := First to First + Len - 1 do
    HLine[I] := '-';
  LogTagged(' ', '');
  LogTagged('^', Heading);
  // LogTagged('|', HLine);
  FPreviousLine := HLine;
end;

procedure TLogParagraphPrinter.Finish;
begin
  LogTagged('|', FPreviousLine);
  LogTagged('v', '');
  LogTagged(' ', '');
end;

procedure TLogParagraphPrinter.Line(const S: AnsiString);
begin
  LogTagged('|', FPreviousLine);
  FPreviousLine := S;
end;

end.
