Changeset 6 for trunk


Ignore:
Timestamp:
Aug 3, 2015, 4:17:15 PM (9 years ago)
Author:
chronos
Message:
  • Added: Better handling of all three basic new line characters.
  • Modified: Colored text need to be activated with -e switch.
  • Added: Option to show help
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/linetime.lpr

    r5 r6  
    1919    procedure DoRun; override;
    2020  private
     21    UseEscapeChars: Boolean;
     22    TestMode: Boolean;
     23    TestLineDelay: TDateTime;
     24    TestString: string;
    2125    FirstLine: Boolean;
     26    TextOnLine: Boolean;
    2227    StartTime: TDateTime;
    2328    NewLine: Boolean;
     29    NewLineEnding: string;
    2430    ShowTime: Boolean;
    2531    LastLineTime: TDateTime;
     
    2834    TermString: string;
    2935    TermStringIndex: Integer;
    30     function FindLineEnding(Buffer: string; var LineEndingLength: Integer): Integer;
     36    function FindLineEnding(Buffer: string; var FoundLineEnding: string): Integer;
    3137    procedure WriteRaw(Text: string);
    3238    procedure WriteLog(Text: string);
    3339    procedure PrintOutput(Output: string);
     40    procedure ShowTimePrefix;
    3441    procedure ProcessBuffer(Buffer: string);
    3542  public
     
    4653  ErrorMsg: String;
    4754  Buffer: string;
    48   InputStream: TIOStream;
     55  InputStream: TStream;
    4956  Count: Integer;
    5057  LogDir: string;
     
    5259begin
    5360  // Quick check parameters
    54   ErrorMsg := CheckOptions('hclt:', 'help csv log termstr');
     61  ErrorMsg := CheckOptions('hclt:e', 'help csv log termstr escape');
    5562  if ErrorMsg <> '' then begin
    5663    ShowException(Exception.Create(ErrorMsg));
     
    7178
    7279  FormatCSV := HasOption('c', 'csv');
     80  UseEscapeChars := HasOption('e', 'escape');
    7381  LogEnabled := HasOption('l', 'log');
    7482  if LogEnabled then begin
     
    8593  LastLineTime := 0;
    8694  TermStringIndex := 1;
     95  TextOnLine := False;
    8796  try
    88     InputStream := TIOStream.Create(iosInput);
     97    if TestMode then begin
     98      InputStream := TMemoryStream.Create;
     99      InputStream.Write(TestString[1], Length(TestString));
     100      InputStream.Position := 0;
     101    end else
     102      InputStream := TIOStream.Create(iosInput);
    89103    repeat
    90104      SetLength(Buffer, 1000);
     
    93107      ProcessBuffer(Buffer);
    94108    until (Count = 0) or Terminated;
     109    NewLineEnding := LineEnding;
    95110    PrintOutput('');
    96111  finally
     
    103118end;
    104119
    105 function TMyApplication.FindLineEnding(Buffer: string; var LineEndingLength: Integer): Integer;
     120function TMyApplication.FindLineEnding(Buffer: string; var FoundLineEnding: string): Integer;
    106121const
    107122  LineEnding1 = #13#10;
     
    112127begin
    113128  Result := 0;
    114   LineEndingLength := 0;
     129  FoundLineEnding := '';
    115130
    116131  P := Pos(LineEnding1, Buffer);
    117132  if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin
    118133    Result := P;
    119     LineEndingLength := Length(LineEnding1);
     134    FoundLineEnding := LineEnding1;
    120135  end;
    121136  P := Pos(LineEnding2, Buffer);
    122137  if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin
    123138    Result := P;
    124     LineEndingLength := Length(LineEnding2);
     139    FoundLineEnding := LineEnding2;
    125140  end;
    126141  P := Pos(LineEnding3, Buffer);
    127142  if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin
    128143    Result := P;
    129     LineEndingLength := Length(LineEnding3);
     144    FoundLineEnding := LineEnding3;
    130145  end;
    131146end;
     
    145160
    146161procedure TMyApplication.PrintOutput(Output: string);
    147 var
    148   LineTime: TDateTime;
    149   TimeStr: string;
    150162begin
    151163  if NewLine then begin
    152     if FormatCSV then begin
    153       if not FirstLine then WriteRaw('"' + LineEnding);
    154     end else begin
    155       if not FirstLine then WriteRaw(LineEnding);
     164    if not FirstLine then begin
     165      // If previous line was empty then print time prefix before move to next line
     166      if (not TextOnLine) then ShowTimePrefix;
     167      if FormatCSV then begin
     168        WriteRaw('"' + LineEnding);
     169      end else begin
     170        WriteRaw(NewLineEnding);
     171      end;
     172      TextOnLine := False;
    156173    end;
    157174    WriteLog(LineEnding);
     
    159176    FirstLine := False;
    160177  end;
    161   if (Length(Output) > 0) and ShowTime then begin
    162     LineTime := Now - StartTime;
    163     TimeStr := FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2);
    164     if FormatCSV then begin
    165       WriteRaw(TimeStr + ',' +
    166         FloatToStrF((LineTime - LastLineTime) / OneSecond, ffFixed, 10, 2) + ',"');
    167     end else begin
    168       WriteRaw(#$1b'[0;32m' + TimeStr + #$1b'[0m ');
    169     end;
    170     WriteLog(TimeStr + ' ');
     178  if Length(Output) > 0 then TextOnLine := True;
     179  if TextOnLine and ShowTime then begin
     180    ShowTimePrefix;
    171181    ShowTime := False;
    172     LastLineTime := LineTime;
    173182  end;
    174183  WriteRaw(Output);
    175184  WriteLog(Output);
     185end;
     186
     187procedure TMyApplication.ShowTimePrefix;
     188var
     189  LineTime: TDateTime;
     190  TimeStr: string;
     191begin
     192  LineTime := Now - StartTime;
     193  TimeStr := FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2);
     194  if FormatCSV then begin
     195    WriteRaw(TimeStr + ',' +
     196      FloatToStrF((LineTime - LastLineTime) / OneSecond, ffFixed, 10, 2) + ',"');
     197  end else begin
     198    if UseEscapeChars then
     199      WriteRaw(#$1b'[0;32m' + TimeStr + #$1b'[0m ')
     200      else WriteRaw(TimeStr + ' ');
     201  end;
     202  WriteLog(TimeStr + ' ');
     203  LastLineTime := LineTime;
     204  Sleep(Trunc(TestLineDelay / OneMillisecond));
    176205end;
    177206
     
    181210  I: Integer;
    182211  Part: string;
    183   LineEndingLength: Integer;
    184 begin
     212  FoundLineEnding: string;
     213begin
     214  NewLineEnding := LineEnding;
    185215  repeat
    186     P := FindLineEnding(Buffer, LineEndingLength);
     216    FoundLineEnding := '';
     217    P := FindLineEnding(Buffer, FoundLineEnding);
    187218    if P > 0 then begin
    188219      Part := Copy(Buffer, 1, P - 1);
     
    190221        Part := StringReplace(Part, '"', '""', [rfReplaceAll]);
    191222      PrintOutput(Part);
    192       Delete(Buffer, 1, P + LineEndingLength - 1);
     223      NewLineEnding := FoundLineEnding;
     224      Delete(Buffer, 1, P - 1 + Length(FoundLineEnding));
    193225      NewLine := True;
    194226      ShowTime := True;
     
    214246  inherited Create(TheOwner);
    215247  StopOnException := True;
     248  TestLineDelay := 0;
     249  TestMode := False;
     250  //TestMode := True;
     251  if TestMode then begin
     252    TestLineDelay := 10 * OneMillisecond;
     253    //TestString := 'Line 1'#13#10'Line 2'#13'Line 3'#10'Line 4 abcd abcd xyz'#13#10#13#10'Line 9'#13#13#10'Line 10';
     254    TestString := 'sas'#13#13#10'Line 10';
     255  end;
    216256end;
    217257
     
    224264procedure TMyApplication.WriteHelp;
    225265begin
    226   { add your help code here }
    227   WriteLn('Usage: ', ExeName, ' -h');
     266  WriteLn('Usage: ', ExeName, ' <parameters>');
     267  WriteLn(' -h --help        Show this help');
    228268  WriteLn(' -c --csv         Print lines in CSV format');
    229269  WriteLn(' -l --log         Log output to log files in ~/.linetime/logs');
    230270  WriteLn(' -t --termstr <string> Termination string');
     271  WriteLn(' -e --escape      Use escape characters for color change');
    231272end;
    232273
Note: See TracChangeset for help on using the changeset viewer.