Hello everyone, I’ve recently started exploring the Free Pascal source tree
and digging into the fp text-mode IDE to better understand how things are
structured and implemented. As a learning exercise, I put together a very
simple Free Vision program that runs lsblk -S and displays the output in a
scrollable TListBox.

The attached screenshot shows the issue I’m running into with the displayed
output. I’m clearly missing something fundamental here, and I’d appreciate
any pointers in the right direction.

Would this also be an appropriate question to raise on the fpc-devel list?

Thanks in advance.

Aruna Hewapathirane
program lsblk;

{$mode objfpc}{$H+}

uses
  App, Objects, Drivers, Views, Menus, Dialogs,
  Process, Classes, SysUtils;

const
  cmLsblk = 1001;

type
  TMyApplication = object(TApplication)
    procedure InitMenuBar; virtual;
    procedure HandleEvent(var Event: TEvent); virtual;
  end;

{------------------------------------------------------------
  Run lsblk -S
------------------------------------------------------------}
function RunLsblk: string;
var
  P: TProcess;
  S: TStringStream;
begin
  Result := '';
  P := TProcess.Create(nil);
  S := TStringStream.Create('');
  try
    P.Executable := '/bin/lsblk';
    P.Parameters.Add('-S');
    P.Options := [poUsePipes, poWaitOnExit];
    P.Execute;
    S.CopyFrom(P.Output, 0);
    Result := S.DataString;
  finally
    S.Free;
    P.Free;
  end;
end;

{------------------------------------------------------------
  Scrollable ListBox dialog
------------------------------------------------------------}
procedure ShowTextListDialog(const Title, Text: string);
var
  D: PDialog;
  R: Objects.TRect;
  LB: PListBox;
  Lines: PStringCollection;
  SL: TStringList;
  I: Integer;
begin
  R.Assign(5, 2, 75, 22);
  D := New(PDialog, Init(R, Title));
  if D = nil then Exit;

  Lines := New(PStringCollection, Init(100, 10));

  SL := TStringList.Create;
  try
    SL.Text := Text;
    for I := 0 to SL.Count - 1 do
      Lines^.Insert(NewStr(SL[I]));
  finally
    SL.Free;
  end;

  R.Assign(2, 2, 68, 17);
  LB := New(PListBox, Init(R, 1, nil));
  LB^.NewList(Lines);
  D^.Insert(LB);

  R.Assign(30, 18, 44, 20);
  D^.Insert(New(PButton, Init(R, '~O~K', cmOK, bfDefault)));

  Desktop^.ExecView(D);
  Dispose(D, Done);
end;

{------------------------------------------------------------
  Menu bar
------------------------------------------------------------}
procedure TMyApplication.InitMenuBar;
var
  R: Objects.TRect;
begin
  GetExtent(R);
  R.B.Y := R.A.Y + 1;

  MenuBar := New(PMenuBar, Init(R,
    NewMenu(
      NewSubMenu('~S~ystem', hcNoContext,
        NewMenu(
          NewItem('List ~B~lock Devices', 'Alt-B', kbAltB,
                  cmLsblk, hcNoContext, nil)
        ),
      nil)
    )
  ));
end;

{------------------------------------------------------------
  Event handling
------------------------------------------------------------}
procedure TMyApplication.HandleEvent(var Event: TEvent);
begin
  inherited HandleEvent(Event);

  if Event.What = evCommand then
    case Event.Command of
      cmLsblk:
        begin
          ShowTextListDialog('lsblk -S', RunLsblk);
          ClearEvent(Event);
        end;
    end;
end;

var
  MyApp: TMyApplication;

begin
  MyApp.Init;
  MyApp.Run;
  MyApp.Done;
end.
_______________________________________________
fpc-devel maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to