logfilter/UWriter.pas

98 lines
2.0 KiB
Plaintext
Raw Normal View History

unit UWriter;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, UFilter, crt, fgl;
type
TWriter = class
public
procedure WriteContent(AContent: String;
AHighlights: THighlights); virtual; abstract;
end;
TWriterList = specialize TFPGObjectList<TWriter>;
{ TConsoleWriter }
TConsoleWriter = class(TWriter)
public
procedure WriteContent(AContent: String;
AHighlights: THighlights); override;
end;
{ THTMLWriter }
THTMLWriter = class(TWriter)
protected
FFileStream: TFileStream;
procedure WriteLine(AContent: String);
public
constructor Create(AFileName: String);
destructor Destroy; override;
procedure WriteContent(AContent: String;
AHighlights: THighlights); override;
end;
implementation
{ THTMLWriter }
procedure THTMLWriter.WriteLine(AContent: String);
begin
AContent := AContent + LineEnding;
FFileStream.Write(AContent[1], Length(AContent));
end;
constructor THTMLWriter.Create(AFileName: String);
begin
FFileStream := TFileStream.Create(AFileName, fmCreate);
WriteLine('<html>');
WriteLine('<body>');
end;
destructor THTMLWriter.Destroy;
begin
WriteLine('</body>');
WriteLine('</html>');
FFileStream.Free;
inherited Destroy;
end;
procedure THTMLWriter.WriteContent(AContent: String; AHighlights: THighlights);
begin
end;
{ TConsoleWriter }
procedure TConsoleWriter.WriteContent(AContent: String; AHighlights: THighlights
);
var
matchPos, offset, lastPos: Integer;
highlight: THighlight;
begin
lastPos := 1;
for highlight in AHighlights do
begin
matchPos := highlight.Start;
offset := highlight.Length;
write(Copy(AContent, lastPos, matchPos - lastPos));
if highlight.FGColor < $FF then
TextColor(highlight.FGColor);
if highlight.BGColor < $FF then
TextBackground(highlight.BGColor);
write(Copy(AContent, matchPos, offset));
NormVideo;
lastPos := matchPos + offset;
end;
writeln(Copy(AContent, lastPos, Length(AContent)));
end;
end.