- Timestamp:
- Aug 3, 2015, 4:17:15 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/linetime.lpr
r5 r6 19 19 procedure DoRun; override; 20 20 private 21 UseEscapeChars: Boolean; 22 TestMode: Boolean; 23 TestLineDelay: TDateTime; 24 TestString: string; 21 25 FirstLine: Boolean; 26 TextOnLine: Boolean; 22 27 StartTime: TDateTime; 23 28 NewLine: Boolean; 29 NewLineEnding: string; 24 30 ShowTime: Boolean; 25 31 LastLineTime: TDateTime; … … 28 34 TermString: string; 29 35 TermStringIndex: Integer; 30 function FindLineEnding(Buffer: string; var LineEndingLength: Integer): Integer;36 function FindLineEnding(Buffer: string; var FoundLineEnding: string): Integer; 31 37 procedure WriteRaw(Text: string); 32 38 procedure WriteLog(Text: string); 33 39 procedure PrintOutput(Output: string); 40 procedure ShowTimePrefix; 34 41 procedure ProcessBuffer(Buffer: string); 35 42 public … … 46 53 ErrorMsg: String; 47 54 Buffer: string; 48 InputStream: T IOStream;55 InputStream: TStream; 49 56 Count: Integer; 50 57 LogDir: string; … … 52 59 begin 53 60 // Quick check parameters 54 ErrorMsg := CheckOptions('hclt: ', 'help csv log termstr');61 ErrorMsg := CheckOptions('hclt:e', 'help csv log termstr escape'); 55 62 if ErrorMsg <> '' then begin 56 63 ShowException(Exception.Create(ErrorMsg)); … … 71 78 72 79 FormatCSV := HasOption('c', 'csv'); 80 UseEscapeChars := HasOption('e', 'escape'); 73 81 LogEnabled := HasOption('l', 'log'); 74 82 if LogEnabled then begin … … 85 93 LastLineTime := 0; 86 94 TermStringIndex := 1; 95 TextOnLine := False; 87 96 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); 89 103 repeat 90 104 SetLength(Buffer, 1000); … … 93 107 ProcessBuffer(Buffer); 94 108 until (Count = 0) or Terminated; 109 NewLineEnding := LineEnding; 95 110 PrintOutput(''); 96 111 finally … … 103 118 end; 104 119 105 function TMyApplication.FindLineEnding(Buffer: string; var LineEndingLength: Integer): Integer;120 function TMyApplication.FindLineEnding(Buffer: string; var FoundLineEnding: string): Integer; 106 121 const 107 122 LineEnding1 = #13#10; … … 112 127 begin 113 128 Result := 0; 114 LineEndingLength := 0;129 FoundLineEnding := ''; 115 130 116 131 P := Pos(LineEnding1, Buffer); 117 132 if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin 118 133 Result := P; 119 LineEndingLength := Length(LineEnding1);134 FoundLineEnding := LineEnding1; 120 135 end; 121 136 P := Pos(LineEnding2, Buffer); 122 137 if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin 123 138 Result := P; 124 LineEndingLength := Length(LineEnding2);139 FoundLineEnding := LineEnding2; 125 140 end; 126 141 P := Pos(LineEnding3, Buffer); 127 142 if (P > 0) and (((Result > 0) and (P < Result)) or (Result = 0)) then begin 128 143 Result := P; 129 LineEndingLength := Length(LineEnding3);144 FoundLineEnding := LineEnding3; 130 145 end; 131 146 end; … … 145 160 146 161 procedure TMyApplication.PrintOutput(Output: string); 147 var148 LineTime: TDateTime;149 TimeStr: string;150 162 begin 151 163 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; 156 173 end; 157 174 WriteLog(LineEnding); … … 159 176 FirstLine := False; 160 177 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; 171 181 ShowTime := False; 172 LastLineTime := LineTime;173 182 end; 174 183 WriteRaw(Output); 175 184 WriteLog(Output); 185 end; 186 187 procedure TMyApplication.ShowTimePrefix; 188 var 189 LineTime: TDateTime; 190 TimeStr: string; 191 begin 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)); 176 205 end; 177 206 … … 181 210 I: Integer; 182 211 Part: string; 183 LineEndingLength: Integer; 184 begin 212 FoundLineEnding: string; 213 begin 214 NewLineEnding := LineEnding; 185 215 repeat 186 P := FindLineEnding(Buffer, LineEndingLength); 216 FoundLineEnding := ''; 217 P := FindLineEnding(Buffer, FoundLineEnding); 187 218 if P > 0 then begin 188 219 Part := Copy(Buffer, 1, P - 1); … … 190 221 Part := StringReplace(Part, '"', '""', [rfReplaceAll]); 191 222 PrintOutput(Part); 192 Delete(Buffer, 1, P + LineEndingLength - 1); 223 NewLineEnding := FoundLineEnding; 224 Delete(Buffer, 1, P - 1 + Length(FoundLineEnding)); 193 225 NewLine := True; 194 226 ShowTime := True; … … 214 246 inherited Create(TheOwner); 215 247 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; 216 256 end; 217 257 … … 224 264 procedure TMyApplication.WriteHelp; 225 265 begin 226 { add your help code here }227 WriteLn(' Usage: ', ExeName, ' -h');266 WriteLn('Usage: ', ExeName, ' <parameters>'); 267 WriteLn(' -h --help Show this help'); 228 268 WriteLn(' -c --csv Print lines in CSV format'); 229 269 WriteLn(' -l --log Log output to log files in ~/.linetime/logs'); 230 270 WriteLn(' -t --termstr <string> Termination string'); 271 WriteLn(' -e --escape Use escape characters for color change'); 231 272 end; 232 273
Note:
See TracChangeset
for help on using the changeset viewer.