' PROGRTM(1.1)  Display File Process Progr Ind with Times  06/27/1994-02/14/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1994-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Routine

' Parameters -------------------------------------------------------------------

'    File%    - File number (as it was opened in the calling program)
'    Nam$     - Program name and version (E.g.: "CONVERT(0.0)")
'    Title$   - Process title string (truncated to 64 chars if longer)
'    Record$  - Current input record
'    Mode$    - Progress indicator display mode:
'               S  - Start
'               P  - Progress
'               F  - Finish (any value different from "S" or "P" is equivalent to "F")
'    Appear$  - Appearance control string (fixed structure 6-character):
'               +------- Incomplete symbol          (Default: ">")
'               |+------ Complete   symbol          (Default: "=")
'               ||+----- Incomplete color attribute (Default: "E"  - Yellow)
'               |||+---- Complete   color attribute (Default: "B"  - Bright Cayan)
'               ||||+--- Running    color attribute (Default: "A"  - Bright Green)
'               |||||+-- Background color attribute (Default: "0"  - Black)
'               ||||||
'               123456
'               >=EBA0 - Default appearance control string (padded with blanks)
'                        Individual blanks default to corresponding symbols
'                 Note:  This parameter is processed only in "Start" mode
'                        (in "Progress" and "Finish" mode it is ignored)

' Notes ------------------------------------------------------------------------

'  - Routine is intended for use in file processing/conversion procedures
'    running from command prompt and showing processing progress on a screen.
'  - Routine properly handles running time of 99 minutes MAXimum.
'  - Use it with long running (over 1 minute) procedures.
'  - For short procedures see routine PROGRES (without elapsed/left time display).
'  - Rotating indicator wheel is synchronized with completeness % display.
'  - Processing elapsed time & estimate of processing left time is shown as MM:SS.
'  - Evaluation and display of left time starts after 5% processing completeness.

' Examples ---------------------------------------------------------------------

'    Start: CALL PROGRTM(1,"CONVERT(0.0)","File...","","S",">=EBA0")  - After OPEN

'    CONVERT(0.0)  File SOURCE.TXT is converted to TARGET.TXT   Elap   Left
'         0% [-]   >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

'    Progress: CALL PROGRTM(1,"","",Input.Record$,"P","")  - After every LINE INPUT

'    CONVERT(0.0)  File SOURCE.TXT is converted to TARGET.TXT   Elap   Left
'        50% [|]   =====================>>>>>>>>>>>>>>>>>>>>>   6:30   6:20

'    Finish: CALL PROGRTM(1,"","","","F","")  - After encountering EOF

'    CONVERT(0.0)  File SOURCE.TXT is converted to TARGET.TXT   Elap   Left
'       100% [*]   ==========================================  12:50   0:00

' Start Routine ----------------------------------------------------------------

     DEFINT A-Z     ' All defaulted variables are integer

     SUB PROGRTM(File%,Nam$,Title$,Record$,Mode$,Appear$) STATIC

     DIM Wheel$(4)

' Initialize Routine Constants (Start) -----------------------------------------

     SELECT CASE (UCASE$(Mode$))
            CASE ("S")
                 Wheel$(1)="-"
                 Wheel$(2)="\"
                 Wheel$(3)="|"
                 Wheel$(4)="/"
                 IF (LEN(Appear$)=0) THEN
                    App$=">=EBA0"
                 ELSE
                    App$=Appear$+"      "
                 END IF
                 Incompl_Symb$    =MID$(App$,1,1)
                 Complete_Symb$   =MID$(App$,2,1)
                 Incompl_Color$   =MID$(App$,3,1)
                 Complete_Color$  =MID$(App$,4,1)
                 Running_Color$   =MID$(App$,5,1)
                 Background_Color$=MID$(App$,6,1)
                 IF (Incompl_Symb$    =" ") THEN Incompl_Symb$    =">"
                 IF (Complete_Symb$   =" ") THEN Complete_Symb$   ="="
                 IF (Incompl_Color$   =" ") THEN Incompl_Color$   ="E"
                 IF (Complete_Color$  =" ") THEN Complete_Color$  ="B"
                 IF (Running_Color$   =" ") THEN Running_Color$   ="A"
                 IF (Background_Color$=" ") THEN Background_Color$="0"
                 Incompl_Color   =VAL("&H"+Incompl_Color$)
                 Complete_Color  =VAL("&H"+Complete_Color$)
                 Running_Color   =VAL("&H"+Running_Color$)
                 Background_Color=VAL("&H"+Background_Color$)

' Display Initial Processing Progress Message ----------------------------------

     Start_Time!=TIMER
     File_Size!=LOF(File%)  ' Get input file size
     CONSOLE GET SCREEN TO Rows&,Columns&
     Max_Title_Len=Columns&-LEN(Nam$)-16
     IF (LEN(Title$)>Max_Title_Len) THEN Title$=LEFT$(Title$,Max_Title_Len)
     Title_Len=LEN(Title$)
     Percent_Pos=LEN(Nam$)-8
     Time_Pos=LEN(Nam$)+LEN(Title$)+5
     Share!=File_Size!/Title_Len
     IF (Share!=0) THEN Share!=1
     Limit!=Share!
     COLOR Complete_Color,Background_Color
     PRINT Nam$;"  ";Title$;"   Elap   ";
     COLOR Running_Color,Background_Color
     PRINT "Left"
     PRINT TAB(Percent_Pos);"  0% [";Wheel$(1);"]   ";
     COLOR Incompl_Color,Background_Color
     PRINT STRING$(Title_Len,Incompl_Symb$);"       ";
     Length_Ratio!=Title_Len/File_Size!
     Curs_Line=CSRLIN

' Redisplay Processing Progress Message ----------------------------------------

     CASE ("P")
          Curr_Size!=Curr_Size!+LEN(Record$)+2
          Percent_Compl=Curr_Size!/File_Size!*100
          Elapsed_Time=TIMER-Start_Time!
          IF (Elapsed_Time>Prev_Time) THEN
             Prev_Time=Elapsed_Time
             COLOR Running_Color,Background_Color
             LOCATE Curs_Line,Time_Pos
             Dspl_Time=Elapsed_Time
             GOSUB Display_Time
             IF (Percent_Compl>4) THEN
                Left_Time=Elapsed_Time*(File_Size!-Curr_Size!)/Curr_Size!
                LOCATE Curs_Line,Time_Pos+7
                Dspl_Time=Left_Time
                GOSUB Display_Time
             END IF
          END IF
          LOCATE Curs_Line,Percent_Pos
          COLOR Running_Color,Background_Color
          PRINT USING$("###",Percent_Compl);
          PRINT "% [";Wheel$((Percent_Compl MOD 4)+1);"]   ";
          IF (Curr_Size!>Limit!) THEN
             COLOR Complete_Color,Background_Color
             PRINT STRING$(Curr_Size!*Length_Ratio!,Complete_Symb$);
             Counter=Counter+1
             Limit!=Counter*Share!
          END IF

' Redisplay Process Finish Message ---------------------------------------------

     CASE ELSE
          COLOR Complete_Color,Background_Color
          LOCATE Curs_Line,Time_Pos
          Dspl_Time=Elapsed_Time
          GOSUB Display_Time
          LOCATE Curs_Line,Percent_Pos
          PRINT "100% [*]   ";STRING$(Title_Len,Complete_Symb$)

     END SELECT

     GOTO Finish_Routine

Display_Time:  ' Routine ------------------------------------------------------

     Dspl_Mins=INT(Dspl_Time/60)
'    Dspl_Mins=Dspl_Mins+10  ' Uncomment for testing of 10+ minute span formatting
     Dspl_Secs=INT(Dspl_Time MOD 60)
     PRINT USING$("##",Dspl_Mins);
     PRINT ":";
     IF (Dspl_Secs<10) THEN
        PRINT "0";
        PRINT USING$("#",Dspl_Secs);
     ELSE
        PRINT USING$("##",Dspl_Secs);
     END IF

     RETURN  ' From Display_Time routine

Finish_Routine: ' --------------------------------------------------------------

     COLOR 7,0  ' Restore Gray on Black

     END SUB