Tprocess capturing progress lazarus / 21-General ---------------------------------------------------------------------- domasz · 2024-10-10 15:34 A command line application does an operation and prints a progress bar as X%. It prints that value in the same position on screen- erases previous value and puts new one in the same place. How can I use TProcess to capture that values? Normal way with pipes and a loop just captures how the console looks when it gets to 100%. ---------------------------------------------------------------------- MarkMLl · 2024-10-10 16:45 You'll need to capture all output, and interpret the ANSI (or whatever) escape sequences that handle screen positioning. MarkMLl ---------------------------------------------------------------------- cdbc · 2024-10-10 17:09 Hi I use this procedure to move the cursor in console: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure MoveCursor(X, Y: integer);begin  Write(Format(#27'[%d;%dH', [Y, X])); //<- ansi esc codesend;Maybe you can find this sequence in tprocess' output stream... Regards Benny ---------------------------------------------------------------------- domasz · 2024-10-10 17:22 I use this code from the Wiki and it captures no such sequences Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  BUF_SIZE = 2; // Buffer size for reading the output in chunks var  AProcess     : TProcess;  OutputStream : TStream;  BytesRead    : longint;  Buffer       : array[1..BUF_SIZE] of byte; procedure Run;begin  AProcess := TProcess.Create(nil);   AProcess.Executable := 'prog.exe';  AProcess.Parameters.Add('p=1');   AProcess.Options := [poUsePipes,poStderrToOutPut];   AProcess.Execute;   OutputStream := TMemoryStream.Create;   repeat    BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);    OutputStream.Write(Buffer, BytesRead)  until BytesRead = 0;   AProcess.Free;   with TFileStream.Create('output.txt', fmCreate) do  begin    OutputStream.Position := 0;    CopyFrom(OutputStream, OutputStream.Size);    Free  end;   with TStringList.Create do  begin    OutputStream.Position := 0;    LoadFromStream(OutputStream);     Free  end;   OutputStream.Free; end; ---------------------------------------------------------------------- Bart · 2024-10-10 17:30 If "prog.exe" is a fpc program using Crt unit, I think you're out of luck. Bart ---------------------------------------------------------------------- domasz · 2024-10-10 20:17 I found the application has a switch to force it to render progress normally to stdout. ---------------------------------------------------------------------- Jurassic Pork · 2024-10-11 03:27 Hello, is  your console application writes on console like that you can see in my attachment ? code : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program percentage;uses sysutils;var  total: integer;  current: integer; begin  total := 100;  current := 0;   writeln('in progress...');  while current < total do  begin    current := current + 10;    write('[');    write(current);    write('%]');    write(#13);    flush(output);     Sleep(1000);  end;  writeln('Terminated!');end.                Where to you want to capture output ? Gui application ? Console Application ? Friendly, J.P ---------------------------------------------------------------------- domasz · 2024-10-11 09:15 It is something like this. You know how to capture in this case? ---------------------------------------------------------------------- MarkMLl · 2024-10-11 09:20 You need to look at /everything/ that's being piped out of the program via the TProcess. I suspect that you think you're only seeing the final state, when in actual fact it is (by careful design) overlaying the older stuff. If nothing relevant is being piped out, i.e. it's using the Crt unit or similar, then there's not much you can do. MarkMLl ---------------------------------------------------------------------- domasz · 2024-10-11 11:11 Here's the working code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Run;const  BUF_SIZE = 50;var AProcess: TProcess;    OutputStream: TStream;    BytesRead: longint;    Buffer: array[1..BUF_SIZE] of Char;    Buffer2: String;    P, size: Integer;  i: Integer;    Percent: String;begin  AProcess := TProcess.Create(nil);   AProcess.Executable := '7za.exe';  AProcess.Parameters.Add('a');  AProcess.Parameters.Add('-t7z');  AProcess.Parameters.Add(OutputArchiveName);  AProcess.Parameters.Add(FilenameToCompress);  AProcess.Parameters.Add('-m0=ppmd');  AProcess.Parameters.Add('-mx=9');  AProcess.Parameters.Add('-bsp1'); //switch to show %   AProcess.Options := [poUsePipes,poNoConsole];   AProcess.Execute;   OutputStream := TMemoryStream.Create;   repeat    BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);    OutputStream.Write(Buffer, BytesRead);    OutputStream.Position := 0;     Size := OutputStream.Size;    SetLength(Buffer2, Size);    OutputStream.Read(Buffer2[1], Size);    OutputStream.Position := Size;     //get percent    P := RPos('%', Buffer2);    for i:=P-1 downto 1 do      if Buffer2[i] = ' ' then begin        Percent := Trim(Copy(Buffer2, i, P-i));        break;      end;     Application.ProcessMessages;     ProgressBar1.Position := StrToIntDef(Percent, 0);     if Cancel then break;  until BytesRead = 0;   if Cancel then  AProcess.Terminate(0);   AProcess.Free;   OutputStream.Free;end; ---------------------------------------------------------------------- Jurassic Pork · 2024-10-12 01:31 Hello, Quote from: domasz on October 11, 2024, 11:15:38 amIt is something like this. You know how to capture in this case? code for this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics,  Dialogs, StdCtrls, LazUtf8, Processutils; type   { TForm1 }   TForm1 = class(TForm)    BtGo: TButton;    MemConsole: TMemo;    procedure BtGoClick(Sender: TObject);  private   public    procedure ProcessOutput(Sender:TProcessEx; output:string);    procedure ProcessError(Sender:TProcessEx; {%H-}IsException:boolean);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.BtGoClick(Sender: TObject);var Proc: TProcessEx;begin   try   Proc := TProcessEx.Create(nil);   Proc.Executable := '/home/jurassic/Lazarus/projets/biglines_output/biglines';   Proc.OnErrorM:=@(ProcessError);   Proc.OnOutputM:=@(ProcessOutput);   Proc.Execute();   finally     Proc.Free;   end;end; procedure TForm1.ProcessError(Sender: TProcessEx; IsException: boolean);begin   MemConsole.Lines.Append('Error ! ' + Sender.ExceptionInfo);end; procedure TForm1.ProcessOutput(Sender: TProcessEx; output : String);begin MemConsole.Lines.Text :=  MemConsole.Lines.Text + output;  // if you have encoding problem on windows  //MemConsole.Lines.Text := MemConsole.Lines.Text + ConsoleToUtf8(output);  // for automatic scrolling  MemConsole.SelStart := Length(MemConsole.Lines.Text)-1;  MemConsole.SelLength:=0;end; end. unit processutils is here from fpcupdeluxe project. Friendly, J.P ---------------------------------------------------------------------- domasz · 2024-10-14 15:38 Thanks!