-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pas
1551 lines (1310 loc) · 60.7 KB
/
main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{%RunWorkingDir /Users/Max/ESL}
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, process, Forms, Controls, Graphics, Dialogs, StdCtrls,DateUtils,
AsyncProcess, ExtCtrls, Menus, ComCtrls, ValEdit, Types,LCLType,umodasyncProcess,Math,uAbout{$IFDEF Windows},shellapi {$ENDIF};
const
BUF_SIZE = 1024; // Buffer size for reading the output in chunks
var
WorkingDir,DefaultExecutable,Separator,bash_mount,CurrentFileName,LastWorkbook,AppDir:String;
DefaultTimeThreshold,FormWidth,FormHeight,FormTop,FormLeft,OutputHeight:integer;
LoadLastWorkbook,DefaultOpening:boolean;
type
{ TFMain }
TFMain = class(TForm)
AsyncProcess1: TAsyncProcess;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBoxParams: TGroupBox;
GroupBoxOutputs: TGroupBox;
ImageList1: TImageList;
ImageList2: TImageList;
Label1: TLabel;
Label2: TLabel;
ListBoxScripts: TListBox;
MainMenu1: TMainMenu;
MemoDocStrings: TMemo;
MemoParams: TMemo;
MenuExit: TMenuItem;
MenuAbout: TMenuItem;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItemTimerThreshold: TMenuItem;
MenuItemppNormal: TMenuItem;
MenuItemppRealTime: TMenuItem;
MenuItemppIdle: TMenuItem;
MenuItemppHigh: TMenuItem;
MenuItemPriority: TMenuItem;
MenuItemEditParams: TMenuItem;
MenuItemErrors: TMenuItem;
MenuItemRename: TMenuItem;
MenuItemStart: TMenuItem;
MenuItemStop: TMenuItem;
MenuIOpenWorkbook: TMenuItem;
MenuItemRunNext: TMenuItem;
MenuItemSaveWb: TMenuItem;
MenuItem8: TMenuItem;
MenuItem9: TMenuItem;
OpenDialogScript: TOpenDialog;
OpenDialogNew: TOpenDialog;
PageControlOutput: TPageControl;
Panel1: TPanel;
Panel2: TPanel;
PanelLegend: TPanel;
PanelScripts: TPanel;
PopupMenupar: TPopupMenu;
PopupMenuScripts: TPopupMenu;
PopupMenu_Par: TPopupMenu;
SaveDialog1: TSaveDialog;
Splitter1: TSplitter;
Splitter2: TSplitter;
Splitter3: TSplitter;
StatusBar1: TStatusBar;
TimerStatus: TTimer;
TimerScripts: TTimer;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButtonNew: TToolButton;
ToolButtonRemove: TToolButton;
ToolButtonSaveAs: TToolButton;
ToolButtonSave: TToolButton;
ToolButton2: TToolButton;
ToolButtonAbout: TToolButton;
ToolButtonExit: TToolButton;
procedure AsyncProcess1Terminate(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure EditInputDblClick(Sender: TObject);
procedure EditInputKeyPress(Sender: TObject; var Key: char);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormShow(Sender: TObject);
procedure ListBoxScriptsClick(Sender: TObject);
procedure ListBoxScriptsDblClick(Sender: TObject);
procedure ListBoxScriptsDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure ListBoxScriptsMeasureItem(Control: TWinControl; Index: Integer;
var AHeight: Integer);
procedure MemoArgsDblClick(Sender: TObject);
procedure MemoParamsChange(Sender: TObject);
procedure MemoParamsDblClick(Sender: TObject);
procedure MenuAboutClick(Sender: TObject);
procedure MenuExitClick(Sender: TObject);
procedure MenuIOpenWorkbookClick(Sender: TObject);
procedure MenuItemEditParamsClick(Sender: TObject);
procedure MenuItemErrorsClick(Sender: TObject);
procedure MenuItemppHighClick(Sender: TObject);
procedure MenuItemppIdleClick(Sender: TObject);
procedure MenuItemppNormalClick(Sender: TObject);
procedure MenuItemppRealTimeClick(Sender: TObject);
procedure MenuItemRenameClick(Sender: TObject);
procedure MenuItemStartClick(Sender: TObject);
procedure MenuItemStopClick(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem8Click(Sender: TObject);
procedure MenuItem9Click(Sender: TObject);
procedure MenuItemRunNextClick(Sender: TObject);
procedure MenuItemSaveWbClick(Sender: TObject);
procedure MenuItemTimerThresholdClick(Sender: TObject);
procedure LoadParameters(AScriptID:integer);
procedure PopupMenuScriptsPopup(Sender: TObject);
function ExtractDocStrings(aFilename:string):string;
procedure SaveWorkBook;
procedure OpenWorkbook;
procedure TabSheet1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
procedure Timer1Timer(Sender: TObject);
procedure TimerScriptsTimer(Sender: TObject);
procedure TimerStatusTimer(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure ToolButtonNewClick(Sender: TObject);
procedure ToolButtonRemoveClick(Sender: TObject);
procedure ToolButtonSaveAsClick(Sender: TObject);
procedure ToolButtonAboutClick(Sender: TObject);
procedure ToolButtonExitClick(Sender: TObject);
procedure ToolButtonSaveClick(Sender: TObject);
private
public
end;
var
FMain: TFMain;
implementation
{$R *.lfm}
{ TFMain }
procedure TFMain.SaveWorkBook;
{ Parse the list of loaded scripts and their parameters and writes the contents
to a textfile MM 20200430}
var
WBStringList:TStringList;
i,j:integer;
AThreadParameter:String;
begin
try
WBStringList:=TStringList.Create;
for i:=0 to ListboxScripts.Count-1 do
begin
with (ListBoxScripts.Items.Objects[i] as TModAsyncProcess) do
begin
WBStringList.Add('ScriptName='+Parameters[0]);
if successors <>'' then WBStringList.Add('*Successors='+Successors);
if predecessor<>-1 then WBStringList.Add('*Predecessor='+IntToStr(Predecessor));
if Executable<>DefaultExecutable then WBStringList.Add('*Executable='+Executable);
if TimeThreshold<>DefaultTimeThreshold then WBStringList.Add('*TimeThreshold='+IntToStr(TimeThreshold));
if NickName<>'' then WBStringList.Add('*NickName='+NickName);
if Priority<>ppNormal then
begin
if Priority = ppHigh then WBStringList.Add('*Priority=ppHigh') else
if Priority = ppIdle then WBStringList.Add('*Priority=ppIdle') else
if Priority = ppRealTime then WBStringList.Add('*Priority=ppRealTime')
end;
for j:=1 to Parameters.Count-1 do
begin
AThreadParameter:=Parameters[j];
if length(AThreadParameter)>0 then WBStringList.Add(AThreadParameter);
end
end;
WBStringList.Add(Separator)
end;
if WBStringList.Count>0 then WBStringList.SaveToFile(CurrentFileName);
StatusBar1.Panels[1].Text:=CurrentFileName;
finally
WBStringList.Free
end;
end;
procedure TFMain.OpenWorkBook;
{ Workbooks are text files a list of python scripts and their parameters.
The file is read sequentially:
1) The name of the python script with the keyword "ScriptName="
2) A series of parameters taken by the python script.
The procedure opens the textfile and for each script:
- it creates an instance of TModAsyncProcess and loads its parameters.
- it creates an entry in the listbox of scripts "ListBoxScripts" with
a filename and the associated TModAsyncProcess object MM 20200430}
const
KeyWordScript = 'ScriptName=';
var
WBStringList,ParamStringList,ThreadStringList,aDocStringsParams:TStringList;
i,WBLinesNb,FileTag,Counter,APredecessor,ParamDocStringPosition:integer;
Aline,anExecutable,aThreadParam,aScriptFilename,aNIckName,ATimeThreshold,Asuccessors,aRawDocStrings:String;
APriority: TProcessPriority;// (ppHigh,ppIdle,ppNormal,ppRealTime);
anAsyncProcess:TModAsyncProcess;
ATabSheet:TTabSheet;
AListBox,AnErrorListBox:TListBox;
ASplitter: TSplitter;
APanelInput:Tpanel;
anEdit:TEdit;
aLabel:Tlabel;
BeginBlock,EndBlock:boolean;
begin
try
StatusBar1.Panels[1].Text:=CurrentFileName;
WBStringList:=TStringList.Create; // Stores the entire workbook
ParamStringList:=TStringList.Create; // Stores the parameters of a thread
ThreadStringList:=TStringList.Create; // Stores some other values of a thread. Convention: these start with "*"
WBStringList.LoadFromFile(CurrentFileName); //The workbook is loaded in WBStringList, a TStringList instance
WBLinesNb:=WBStringList.Count;
Asuccessors:='';
Counter:=0;
BeginBlock:=False;
EndBlock:=False;
if WBLinesNb>0 then // If the workbook isn't empty
begin
GroupBoxOutputs.visible:=true; // Show the StdOutput panel
i:=0;
while i<= (WBLinesNb-1) do
begin
ALine:= WBStringList[i]; // Reads a line
FileTag:=pos(KeyWordScript,ALine); // Checks if the line is a script filename
if FileTag<>0 then // If the line is a script filename
begin
BeginBlock:=True;
Aline:=trim(RightStr(Aline,length(Aline)-length(KeyWordScript))); // Extracts the value of the filename
end;
// if Aline is a thread value store it in the thread params list (not a parameter to be provided to the script )
if length(Aline)>0 then
begin
if Aline[1]='*' then ThreadStringList.add(RightStr(Aline,length(Aline)-1))
else
if Aline<>Separator then ParamStringList.add(Aline); //Separators are ignored
if (i = WBLinesNb-1) then
EndBlock := True
else
EndBlock := pos(KeyWordScript, WBStringList[i+1])<>0;
end;
{Parsing of a thread block in the workbook is done: create the thread and add a corresponding entry in the list of scripts}
if (BeginBlock and EndBlock) and (ParamStringList.count>0) then
begin
{Step1: check if the workbook has special settings for the thread that override default value}
anExecutable:=trim(ThreadStringList.Values['Executable']);
if anExecutable='' then anExecutable:= DefaultExecutable; //DefaultExecutable is set in the ini file
ATimeThreshold:=trim(ThreadStringList.Values['TimeThreshold']);
if ATimeThreshold='' then ATimeThreshold:= IntToStr(DefaultTimeThreshold);//DefaultTimeThreshold is the TimeThreshold set in the ini file
aThreadParam:=trim(ThreadStringList.Values['Priority']);
if aThreadParam='' then APriority:= ppNormal else
if aThreadParam='ppHigh' then APriority:= ppHigh else
if aThreadParam='ppIdle' then APriority:= ppIdle else
if aThreadParam='ppRealTime' then APriority:= ppRealTime;
Asuccessors:=trim(ThreadStringList.Values['Successors']);
aThreadParam:=trim(ThreadStringList.Values['Predecessor']);
if aThreadParam='' then APredecessor:=-1 else APredecessor:=StrToInt(aThreadParam);
aNIckName:=trim(ThreadStringList.Values['NickName']);
{------ End of Step1}
{Step2: Create an instance of a TModAsyncProcess and set its values and the associated script parameters}
anAsyncProcess:=TModAsyncProcess.create(FMain);
with anAsyncProcess do
begin
Active:=False;
{Assign values set at step 1 above}
Executable:=anExecutable;
Priority := APriority ;
Successors:=Asuccessors;
Predecessor:=APredecessor;
NIckName:=aNIckName;
TimeThreshold:=StrToInt(ATimeThreshold);
{--}
FillAttribute:=0;
Options:=[poUsePipes,poNoConsole] ;
ShowWindow:=swoNone;
PipeBufferSize:=BUF_SIZE;
CurrentDirectory:=WorkingDir;
ErrorCode:=0;
{$IFDEF Windows}
ShowWindow := swoHIDE;
OnTerminate:=@AsyncProcess1Terminate; // this works only on Windows, not on Linux Manjaro
{$ENDIF}
{Assign script parameters}
Parameters.addstrings(ParamStringList);
{ The Tag below is important for Linux because OnTerminate is not fired and
a workaround fix is for a timer "TimerScripts" to execute running threads
(those with a Tag=1) to the OnTerminate event handler}
Tag:=0;
aScriptFilename:= ParamStringList[0];
{ in my version of Windows 10, I installed bash through WSL. Bash uses the linux mount of the file.
I have to take this into account}
{$IFDEF Windows}
if Executable='bash' then
begin
aScriptFilename:=RightStr(aScriptFilename,length(aScriptFilename)-length(bash_mount));
aScriptFilename:=StringReplace(aScriptFilename,'/','\',[rfReplaceAll]);
Insert(':',aScriptFilename,2);
end;
{$ENDIF}
{Exctracts the docstrings and the parameters list included in the docstrings.
convention is to have "Parameters=" to start the list of parameters separated by "|"}
if FileExists(aScriptFilename) then
begin
aRawDocStrings:=ExtractDocStrings(aScriptFilename);
ParamDocStringPosition:=pos('Parameters=',aRawDocStrings);
if ParamDocStringPosition<>0 then
try
aDocStringsParams:=TStringList.create;
aDocStringsParams.Delimiter:='|';
if ParamDocStringPosition>1 then
begin
DocStrings:=Copy(aRawDocStrings,1,ParamDocStringPosition-1);
end;
aRawDocStrings:=Copy(aRawDocStrings,ParamDocStringPosition+11, length(aRawDocStrings)-10-ParamDocStringPosition);
aDocStringsParams.DelimitedText:=aRawDocStrings;
DocStrings:=DocStrings+Chr(13)+'Parameters are such:'+chr(13)+chr(13)+aDocStringsParams.Text;
finally
aDocStringsParams.Free
end;
end
else
begin
DocStrings:='';
Message:='Error=Script not found: ' + aScriptFilename
end;
ScriptId:=Counter;
Counter:=Counter+1;
end;
if aNickName='' then aNickName:=ExtractFileName(aScriptFilename);
ListBoxScripts.AddItem(aNickName,anAsyncProcess);//Add item on list of scripts
{------ End of Step2}
{Step3: Add a tabsheet for this thread's StdOutput}
ATabSheet:= PageControlOutput.AddTabSheet;
with ATabSheet do
begin
Caption:= aNickName;
TabVisible:=False;
Color:=clBlack;
end;
{Displays StdOutput }
AListBox:=TListBox.Create(ATabSheet);
AListBox.ParentColor:=false;;
with AListBox do
begin
Parent:=ATabSheet;
BorderStyle :=bsNone;
Align:=alBottom;
Color:=clBlack;
Font.Color:=clWhite;
visible:=true;
Height:=50;
end;
{Displays StdError}
AnErrorListBox:=TListBox.Create(ATabSheet);
with AnErrorListBox do
begin
Parent:=ATabSheet;
BorderStyle :=bsNone;
Align:=alTop;
Color:=clBlack;
Font.Color:=clYellow;
visible:=true;
Height:=50;
end;
{A Splitter to increase/decrease the size of the StdError display}
ASplitter:=TSplitter.Create(ATabSheet);
with ASplitter do
begin
Parent :=ATabSheet;
Cursor := crVSplit;
top:=20;
ResizeAnchor := akBottom;
Left := 0;
Height := 2;
ParentColor:=False;
Color:=clDefault;
Align:= alTop;
end;
AListBox.Align:=alClient;
{Create container panel for StdInput.
To mimick the prompt, a TLabel is placed on the left of a TEdit (see below)}
APanelInput:=Tpanel.create(ATabSheet);
with APanelInput do
begin
Parent:=ATabSheet;
Left := 0;
Height := 30;
Align := alBottom;
BevelOuter := bvNone;
BorderStyle:=bsSingle;
Color := clBlack;
ParentColor := False;
ParentFont := True;
end;
{Create an input line for StdInput}
anEdit:=TEdit.Create(APanelInput);
with anEdit do
begin
Parent:=APanelInput;
Left := 9*(Length(anExecutable)+5)+1;
Height := 30;
Top := 1;
Anchors := [akTop, akLeft, akRight];
echoMode:=emNormal;
onKeyPress:=@EditInputKeyPress;
onDblClick:=@EditInputDblClick;
BorderStyle := bsNone;
Color := clBlack;
Font.Color := clLime;
TabOrder := 0;
// ParentFont := False;
Text :=''
end;
{Create a prompt label for StdInput}
aLabel :=TLabel.create(APanelInput);
with aLabel do
begin
Parent:=APanelInput;
Left := 1 ;
Height := 25 ;
Top := 0;
Width := 20;
Alignment := taLeftJustify;
AutoSize := True ;
Caption := anExecutable+' >>> ' ;
Color := clBlack ;
Font.Color := clLime;
ParentColor := False;
ParentFont := False;
Transparent := False ;
end;
{------ End of Step3}
{Step4: Reset reading blocs}
ParamStringList.Clear;
ThreadStringList.Clear;
end;
i:=i+1;
end;
end;
ToolButtonSaveAs.Enabled:=True;
if ListBoxScripts.Count>0 then
begin
ListBoxScripts.ItemIndex:=0;
ListBoxScripts.Selected[0]:=true;
ListBoxScriptsClick(nil)
end;
ListBoxScripts.Repaint;
Application.ProcessMessages;
finally
WBStringList.Free;
ParamStringList.Free;
ThreadStringList.Free;
end;
end;
procedure TFMain.TabSheet1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
end;
procedure TFMain.Timer1Timer(Sender: TObject);
begin
end;
procedure TFMain.TimerScriptsTimer(Sender: TObject);
{$IFDEF Windows}
{$ELSE}
var
index:integer;
AnAsyncProcess: TModAsyncProcess;
{$ENDIF}
begin
{$IFDEF Windows}
{$ELSE} //{$IFDEF Linux} //Workaround because OnTerminate not called for TModAsyncProcess on Linux
for index:=0 to ListBoxScripts.Count-1 do
begin
AnAsyncProcess:=ListBoxScripts.Items.Objects[index] as TModAsyncProcess;
if AnAsyncProcess.Tag = 1 then
begin
if not(AnAsyncProcess.Running) then
begin
AnAsyncProcess.tag:=0;
AsyncProcess1Terminate(AnAsyncProcess)
end;
end;
end;
{$ENDIF}
end;
procedure TFMain.TimerStatusTimer(Sender: TObject);
Var
ReadCount,ReadErrCount,PreviousLength,PreviousLengthErr,AScriptIndex{$IFDEF Linux},lbPreviousCount {$ENDIF}: integer;
AModAsyncProcess:TModAsyncProcess;
anOutput,anError:string;
SomeOutPut,someErrors:boolean;
SecondsSinceLastUpdate:float;
begin
TimerStatus.Enabled:=False;
SomeOutput:=False ;
SomeErrors:=False;
try
for AScriptIndex:=0 to ListBoxScripts.count-1 do
begin
try
AModAsyncProcess:=ListBoxScripts.Items.Objects[AScriptIndex] as TModAsyncProcess;
if AModAsyncProcess.Running then
begin
{Read and assign the output pipe value to the thread}
ReadCount:= AModAsyncProcess.Output.NumBytesAvailable;
anOutput:=AModAsyncProcess.StdOutput; // What is currently in the thread StdOutput
{$IFDEF Linux}lbPreviousCount:=(PageControlOutput.Pages[AScriptIndex].Components[0] as TListbox).count;{$ENDIF}
while ReadCount> 0 do
begin
PreviousLength:=Length(anOutput);
SetLength(anOutput,PreviousLength+ReadCount);
AModAsyncProcess.Output.Read(anOutput[PreviousLength+1], ReadCount);
ReadCount:= AModAsyncProcess.Output.NumBytesAvailable;
AModAsyncProcess.LastUpdateTime:=now();
end;
AModAsyncProcess.StdOutput:=anOutput;
with PageControlOutput.Pages[AScriptIndex].Components[0] as TListbox do
if anOutput<>'' then
begin
Items.text:=anOutput;
{$IFDEF Linux}color:=clBlack; if Count<>lbPreviousCount then {$ENDIF}
ItemIndex:=count-1;
Visible:=True;
Repaint;
SomeOutput:=True;
end ;
{Read and assign the Error pipe value to the thread}
ReadErrCount:= AModAsyncProcess.Stderr.NumBytesAvailable;
anError:=AModAsyncProcess.Errors;
while ReadErrCount> 0 do
begin
PreviousLengthErr:=Length(anError);
SetLength(anError,PreviousLengthErr+ReadErrCount);
AModAsyncProcess.Stderr.Read(anError[PreviousLengthErr+1], ReadErrCount);
ReadErrCount:= AModAsyncProcess.Stderr.NumBytesAvailable;
AModAsyncProcess.LastUpdateTime:=now();
end;
AModAsyncProcess.Errors:=anError;
with PageControlOutput.Pages[AScriptIndex].Components[1] as TListbox do
if anError<>'' then
begin
Items.text:=anError;
{$IFDEF Linux}color:=clBlack; {$ENDIF}
ItemIndex:=count-1;
Visible:=True;
Repaint;
SomeErrors:=True;
end
else visible:=False;
SecondsSinceLastUpdate:= SecondsBetween(now,AModAsyncProcess.LastUpdateTime);
if SecondsSinceLastUpdate>AModAsyncProcess.TimeThreshold then
begin
AModAsyncProcess.LastUpdateTime:=Now();
if MessageDlg('Warning - Process: '+AModAsyncProcess.NickName,'It has been more than '+IntToStr(AModAsyncProcess.TimeThreshold)+' seconds since last response'+chr(13)+
'Stop the process ?',mtConfirmation,[mbNo,mbYes],0)=mrYes then
begin
AModAsyncProcess.Terminate(0);
AModAsyncProcess.ErrorCode:=1;
end
end;
end;
PageControlOutput.Pages[AScriptIndex].TabVisible:=(AModAsyncProcess.StdOutput<>'') or (AModAsyncProcess.Errors<>'');
finally
end;
end;
finally
GroupBoxOutputs.visible:=(SomeOutput or SomeErrors or GroupBoxOutputs.visible);
application.ProcessMessages;
TimerStatus.Enabled:=true;
end
end;
procedure TFMain.ToolButton2Click(Sender: TObject);
begin
with OpenDialogScript do
begin
FilterIndex:=2;
if Execute then
begin
CurrentFileName:=Filename;
OpenWorkBook;
ListBoxScripts.repaint;
application.ProcessMessages;
end;
end;
end;
procedure TFMain.ToolButtonNewClick(Sender: TObject);
var AStringList:TStringList;
begin
with OpenDialogNew do
if Execute then
try
AStringList:=TStringList.Create;
AStringList.Add('ScriptName='+FileName);
CurrentFileName:='/Users/Max/ESL/temp.wbl';
AStringList.SaveToFile(CurrentFileName);
OpenWorkBook;
SaveDialog1.FileName:=CurrentFileName;
ToolButtonSaveAsClick(nil)
finally
AStringList.Free
end;
end;
procedure TFMain.ToolButtonRemoveClick(Sender: TObject);
var i:integer;
begin
ListBoxScripts.Clear;
for i:=0 to PageControlOutput.PageCount-1 do
begin
PageControlOutput.Pages[0].Destroy;
end;
MemoDocStrings.Clear;
MemoParams.clear ;
Statusbar1.Panels[1].Text:='';
GroupBoxOutputs.Visible:=False;
end;
procedure TFMain.ToolButtonSaveAsClick(Sender: TObject);
begin
with SaveDialog1 do
begin
FileName:=OpenDialogScript.Filename;
FilterIndex:=2;
if Execute then
begin
CurrentFileName:=FileName;
SaveWorkbook
end;
end;
end;
procedure TFMain.ToolButtonAboutClick(Sender: TObject);
begin
FAbout.ShowModal;
end;
procedure TFMain.ToolButtonExitClick(Sender: TObject);
begin
MenuExitClick(Sender)
end;
procedure TFMain.ToolButtonSaveClick(Sender: TObject);
begin
SaveWorkBook;
ToolButtonSave.Enabled:=False;
end;
procedure TFMain.AsyncProcess1Terminate(Sender: TObject);
var
aDest:string;
ReadCount,PreviousLength,anIndex,AScriptID:integer;
anOutput,anError:string;
SomeOutput,SomeErrors:boolean;
ListOfSuccessors:TStringList;
begin
adest:='';
with sender as TModAsyncProcess do
begin
FinishTime:=now();
SomeOutput:=False;
SomeErrors:=False;
{Reading StdOut one more time}
ReadCount := Output.NumBytesAvailable;
if ReadCount> 0 then
begin
anOutput := StdOutput;
PreviousLength:=Length(anOutput);
SetLength( anOutput,PreviousLength+ReadCount);
Output.Read(anOutput[PreviousLength+1], ReadCount);
StdOutput:= anOutput;
PageControlOutput.ActivePageIndex:=ScriptID;
with PageControlOutput.Pages[ScriptID].Components[0] as TListbox do
begin
if anOUtput<>'' then
begin
Items.text:=anOutput;
ItemIndex:=count-1;
SomeOutput:=True;
Visible:=True;
Repaint;
end
end;
end;
{Reading StErr one more time}
ReadCount := StdErr.NumBytesAvailable;
if ReadCount> 0 then
begin
anError := Errors;
PreviousLength:=Length(anError);
SetLength( anError,PreviousLength+ReadCount);
StdErr.Read(anError[PreviousLength+1], ReadCount);
Errors:= anError;
PageControlOutput.ActivePageIndex:=ScriptID;
with PageControlOutput.Pages[ScriptID].Components[1] as TListbox do
begin
if anError<>'' then
begin
Items.text:=anError;
ItemIndex:=count-1;
SomeErrors:=True;
Visible:=True;
Repaint;
end
end;
end;
if Errors<>'' then ErrorCode:=2;
adest:= Parameters.Values['destination'];
runNb:=RunNb+1;
if (Successors<>'') and (ErrorCode=0) then
Try
ListOfSuccessors:=TStringList.create;
ListOfSuccessors.Delimiter:=';';
ListOfSuccessors.DelimitedText:=Successors;
for anIndex:=0 to ListOfSuccessors.Count-1 do
begin
AScriptID:= strToInt(ListOfSuccessors[anIndex]);
if (AScriptID <= ListBoxScripts.Count-1) and (AScriptID>=0) then
with (ListBoxScripts.Items.Objects[AScriptID] as TModAsyncProcess)do
begin
if Running then MessageDlg('Error','Process: '+NickName+' is already running!',mterror,[mbok],0) else
begin
Tag:=1;
StartTime:=now();
LastUpdateTime:= StartTime;
ErrorCode:=0;
Execute;
end
end;
end;
if adest<>'' then
begin
{$IFDEF Windows}
ShellExecute(FMain.Handle, PChar ('open'), PChar (adest),PChar (''), PChar (''), 1);
{$ENDIF}
end;
finally
ListOfSuccessors.free
end;
end;
GroupBoxOutputs.visible:=(SomeOutput or SomeErrors or GroupBoxOutputs.visible);
ListBoxScripts.Repaint;
application.ProcessMessages;
end;
procedure TFMain.Edit1Change(Sender: TObject);
begin
end;
procedure TFMain.EditInputDblClick(Sender: TObject);
begin
with (Sender as TEdit) do
begin
if EchoMode = emPassword then EchoMode:= emNormal
else EchoMode:= emPassword
end;
end;
procedure TFMain.EditInputKeyPress(Sender: TObject; var Key: char);
var
aCmd:String;
aCmdSize,ScriptID:integer;
AModAsyncProcess:TModAsyncProcess;
begin
try
if Key = #13 then
begin
aCmd:=(Sender as TEdit).Text+#10;
(Sender as TEdit).Text:='';
aCmdSize:=length(aCmd);
ScriptID:= ((((Sender as TEdit).Parent as TPanel).parent as TTabsheet).Parent as TPageControl).TabIndex;
AModAsyncProcess:=ListBoxScripts.Items.Objects[ScriptID] as TModAsyncProcess;
AModAsyncProcess.Input.Write(aCmd[1],aCmdSize);
end;
Except
MessageDlg('Error','External hread was not expecting input',mtinformation,[mbOk],0)
end;
end;
procedure TFMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var AStringList:TStringList;
begin
try
AStringList:=TStringList.Create;
AStringList.LoadFromFile(AppDir+'easyscriptlauncher.ini');
AStringList.Values['FORM WIDTH']:=IntToStr(FMain.Width);
AStringList.Values['FORM HEIGHT']:=IntToStr(FMain.Height);
AStringList.Values['FORM TOP']:=IntToStr(FMain.Top);
AStringList.Values['FORM LEFT']:=IntToStr(FMain.Left);
if ListBoxScripts.Count >0 then AStringList.Values['LAST WORKBOOK']:= OpenDialogScript.FileName else
AStringList.Values['LAST WORKBOOK']:='' ;
AStringList.SaveToFile(AppDir+'easyscriptlauncher.ini');
finally
AStringList.Free
end;
end;
procedure TFMain.FormShow(Sender: TObject);
var
AStringList:TStringList;
begin
try
{Loads and reads program initialization file}
AStringList:=TStringList.Create;
AppDir:=Application.Location;
{$IFDEF DARWIN}
AppDir:=LeftStr(AppDir,length(AppDir)-length('/Contents/MacOS/'));
{$ENDIF}
AppDir:=ExtractFilePath(AppDir);
AStringList.LoadFromFile(AppDir+'easyscriptlauncher.ini');
WorkingDir:= Trim(AStringList.Values['DIRECTORY']);
DefaultExecutable:= Trim(AStringList.Values['EXECUTABLE']);
Separator:=Trim(AStringList.Values['SEPARATOR']);
DefaultTimeThreshold:=StrToInt(Trim(AStringList.Values['TIME THRESHOLD']));
bash_mount:=Trim(AStringList.Values['BASH MOUNT']);
FormWidth :=StrToInt(Trim(AStringList.Values['FORM WIDTH']));
FormHeight :=StrToInt(Trim(AStringList.Values['FORM HEIGHT']));
FormTop :=StrToInt(Trim(AStringList.Values['FORM TOP']));
FormLeft :=StrToInt(Trim(AStringList.Values['FORM LEFT']));
LastWorkbook :=Trim(AStringList.Values['LAST WORKBOOK']);
LoadLastWorkbook :=StrToBool(Trim(AStringList.Values['LOAD LAST WORKBOOK']));
DefaultOpening :=StrToBool(Trim(AStringList.Values['DEFAULT OPENING']));
OutputHeight := StrToInt(Trim(AStringList.Values['OUTPUT HEIGHT']));
OpenDialogScript.FileName:=LastWorkbook;
if LastWorkbook<>'' then
begin
CurrentFileName:=LastWorkbook;
OpenDialogScript.FileName:=LastWorkbook;
SaveDialog1.FileName:=LastWorkbook;
OpenWorkBook;
end;
FMain.Width:=FormWidth;
FMain.Height:=FormHeight;
FMain.Top:=FormTop;
FMain.Left:=FormLeft;
GroupBoxOutputs.Height:=OutputHeight;
OpenDialogScript.InitialDir:= WorkingDir;
OpenDialogNew.InitialDir:= WorkingDir;
SaveDialog1.InitialDir:= WorkingDir;
finally
AStringList.Free
end;
end;
procedure TFMain.ListBoxScriptsClick(Sender: TObject);
var
ScriptPosition:integer;
anAsyncProcess:TModAsyncProcess;
ExecTimes:String;
begin
ScriptPosition:=ListBoxScripts.ItemIndex;
ListBoxScripts.Repaint;///
if ScriptPosition <>-1 then
begin