Progress indicator for REXX programs

Дата: 1997

Скачать в ZIP архиве.

/*-----------------------------------------------------------------------*/
/*----- RxShowMe v1.2: Progress indicator for REXX programs.        -----*/
/*-----                                                             -----*/
/*----- Sample program to show usage of ShowProg routine.           -----*/
/*-----                                                             -----*/
/*----- To use this in another program, just setup the sp. variables-----*/
/*----- as shown in the Initialize routine, and include the ShowProg-----*/
/*----- procedure (not this whole file) in your program.  When your -----*/
/*----- status variables change, call ShowProg to update the screen.-----*/
/*-----                                                             -----*/
/*----- Change History                                              -----*/
/*-----                                                             -----*/
/*----- 05/10/94 Version 1.0 released to OS/2 tools.  A few bugs    -----*/
/*-----          getting off the ground ...                         -----*/
/*----- 05/11/94 Version 1.1 - bug fixes for 1.0                    -----*/
/*----- 05/13/94 Version 1.2                                        -----*/
/*-----          - Added progress indicator bar.  It just updates   -----*/
/*-----            each time ShowProg is called.  For it to be      -----*/
/*-----            really useful, i.e. display % done, you need to  -----*/
/*-----            build in the logic based on whatever your program-----*/
/*-----            is doing.  ShowProg has no way of knowing what   -----*/
/*-----            you are doing, or how far along you are.  You'd  -----*/
/*-----            probably want to base it on expected elapsed     -----*/
/*-----            time, steps completed, lines read/written, or    -----*/
/*-----            something like that.                             -----*/
/*-----          - Can now use elapsed time, progress bar, either   -----*/
/*-----            one, both, or none - see init section for flags  -----*/
/*-----          - Added little menu up front for sample display    -----*/
/*-----            mode                                             -----*/
/*-----          - Added more colours in init section for prog bar  -----*/
/*-----          - Changed sample delay statements to use SysSleep  -----*/
/*-----          - Changed to loop forever until you CTL+C.  If you -----*/
/*-----            want it to go faster, rem out the sleeps         -----*/
/*-----------------------------------------------------------------------*/

call Initialize

/*----- See which mode to display in -----*/

say
say 'RxShowMe includes an elapsed time counter and progress'
say 'bar by default.  Select another option to change it, or'
say "'Q' to quit."
say
say '1. Show elapsed time - no progress bar'
say '2. Show progress bar - no elapsed time'
say "3. Don't show either elapsed time or progress bar"
say
say 'Which one?'
pull mode
sp.show_elapsed = 1
sp.show_pbar = 1
select
   when mode = 1 then sp.show_pbar = 0
   when mode = 2 then sp.show_elapsed = 0
   when mode = 3 then do
      sp.show_elapsed = 0
      sp.show_pbar = 0
      end
   when mode = 'q' | mode = 'Q' then exit
   otherwise nop
   end

/*----- Some sample info to display -----*/

file.0 = 3
file.1 = 'CONFIG.SYS'
desc.1 = 'OS/2 initialization file'
file.2 = 'PROTOCOL.INI'
desc.2 = 'LAPS initialization file'
file.3 = 'IBMLAN.INI'
desc.3 = 'LAN initialization file'

/*----- Run ShowProg for the first time -----*/

file_name = ''
file_desc = ''
status = ''
showprog_info = 'This sample runs forever; CTL+C to quit'
call ShowProg 'INIT'

/*----- Sample main loop -----*/

err_count = 0
do forever
   do i=1 to file.0
      file_name = file.i
      file_desc = desc.i
      status = 'Processing 'file_name' file'
      call ShowProg

      /*----- Dummy wait so you can see what's happening -----*/

      call SysSleep 1

      /*----- Show a simulated error message -----*/

      err_count = err_count + 1
      if err_count = 10 then do
         status = 'Simulated ERROR!!!'
         call ShowProg
         call SysSleep 2
         err_count = 0
         end
      end
   end

status = 'Done!'
call ShowProg
exit

/*-----------------------------------------------------------------------*/
/*----- ShowProg: Show progress                                     -----*/
/*-----------------------------------------------------------------------*/

ShowProg: procedure expose sp. (sp.vals)
arg action

call SysCurState 'OFF'
if action = 'INIT' then do

   /*----- Set start time if first run through here -----*/

   if sp.show_elapsed = 1 & sp.start = '' then sp.start = time('S')

   /*----- Set column widths, using 12 as a base -----*/
   /*----- (length of "Elapsed time")            -----*/

   if sp.show_elapsed = 1 then sp.col_1 = 12
   else sp.col_1 = 0
   do i=1 to sp.line.0
      if length(sp.line.desc.i) > sp.col_1 then
         sp.col_1 = length(sp.line.desc.i)
      end
   sp.col_1 = sp.col_1 + 2
   sp.col_2 = sp.width - sp.col_1 - 7
   sp.col2_start = sp.col_1 + 5

   /*----- Set top and bottom lines -----*/

   sp.top_line = sp.clr.main||'Ъ'||copies('Д',sp.col_1+2)||'В'|| ,
      center(sp.clr.hi||' 'sp.line.title' '||sp.clr.main,sp.col_2+19,'Д')|| ,
         'Дї'||sp.clr.norm
   sp.bot_line = sp.clr.main||'А'||copies('Д',sp.col_1+2)||'Б'|| ,
      copies('Д',sp.col_2)||'Щ'||sp.clr.norm
   call SysCls

   /*----- Draw the box and left-hand column -----*/

   scr_pos = SysCurPos(2,0)
   say sp.top_line
   do i=1 to sp.line.0
      say sp.clr.main||'і '||right(sp.line.desc.i,sp.col_1,' ')||' і '|| ,
         copies(' ',sp.col_2-2)||' і'||sp.clr.norm
      end

   /*----- Elapsed time line -----*/

   if sp.show_elapsed = 1 then do
      say sp.clr.main||'і '||right('Elapsed time',sp.col_1,' ')||' і '|| ,
         copies(' ',sp.col_2-2)||' і'||sp.clr.norm
      end

   /*----- Progress bar -----*/

   if sp.show_pbar = 1 then do
      if sp.show_elapsed = 0 then do
         say sp.clr.main||'Г'||copies('Д',sp.col_1+1)||'ДБД'|| ,
            copies('Д',sp.col_2-2)||'Дґ'||sp.clr.norm
         say sp.clr.main||'і '||copies(' ',sp.width-6)||' і'||sp.clr.norm
         sp.bar_start = 1
         sp.bar_len = sp.width - 4
         end
      else do
         sp.bar_start = sp.col2_start + 9
         sp.bar_len = sp.width - sp.col2_start - 13
         end
      sp.bar_spot = sp.bar_start
      end
   if sp.show_elapsed = 1 & sp.show_pbar = 1 then
      say sp.clr.main||'Г'||copies('Д',sp.col_1+1)||'ДБД'|| ,
         copies('Д',sp.col_2-2)||'Дґ'||sp.clr.norm
      else say sp.clr.main||'Г'||copies('Д',sp.width - 4)||'ґ'||sp.clr.norm
   say sp.clr.main||'і '||copies(' ',sp.width-6)||' і'||sp.clr.norm
   say sp.clr.main||'А'||copies('Д',sp.width-4)||'Щ'||sp.clr.norm
   end

/*----- Display new status info -----*/

row = 3

/*----- Status lines -----*/

do i=1 to sp.line.0
   scr_pos = SysCurPos(row,sp.col2_start)
   say sp.clr.main||left(value(sp.line.stat.i),sp.col_2-2,' ')||sp.clr.norm
   row = row + 1
   end

/*----- Elapsed time -----*/

if sp.show_elapsed = 1 then do
   scr_pos = SysCurPos(row,sp.col2_start)
   time_now = time('S')
   time_elapsed = time_now - sp.start

   hrs = (time_elapsed/60) / 60
   hh = max(0,format(hrs-.5,,0))
   mins = (time_elapsed - ((hh*60)*60)) / 60
   mm = max(0,format(mins-.5,,0))
   secs = time_elapsed - ((hh*60)*60) - (mm*60)
   ss = max(0,format(secs,,0))

   show_et = right(hh,2,'0')||':'||right(mm,2,'0')||':'right(ss,2,'0')
   say sp.clr.main||show_et
   end

/*----- Progress bar -----*/

if sp.show_pbar = 1 then do
   if sp.show_elapsed = 1 then scr_pos = SysCurPos(row,sp.bar_spot)
   else scr_pos = SysCurPos(row+1,sp.bar_spot)

   /*----- Which colour? -----*/

   select
      when sp.bar_num > 21 then this_clr = sp.clr.yel
      when sp.bar_num > 14 then this_clr = sp.clr.grn
      when sp.bar_num > 7 then this_clr = sp.clr.blu
      otherwise this_clr = sp.clr.red
      end

   /*----- Which bar (shade) number? -----*/

   if sp.bar_num < 8 then this_bar = sp.bar_num
   else do
      max_this_group = format((sp.bar_num / 7)+.5,,0) * 7
      this_bar = 8 - (max_this_group - sp.bar_num + 1)
      if this_bar = 0 then this_bar = 7
      end

   if this_bar = 1 | this_bar = 7 then say this_clr'°'sp.clr.main
   if this_bar = 2 | this_bar = 6 then say this_clr'±'sp.clr.main
   if this_bar = 3 | this_bar = 5 then say this_clr'І'sp.clr.main
   if this_bar = 4 then say this_clr'Ы'sp.clr.main

   /*----- Cycle back to beginning if through this one -----*/

   if sp.bar_num = 28 then sp.bar_num = 1
      else sp.bar_num = sp.bar_num + 1

   /*----- Output the bar -----*/

   if sp.bar_spot = sp.bar_start + sp.bar_len - 1 then do
      if sp.show_elapsed = 1 then scr_pos = SysCurPos(row,sp.bar_start)
      else scr_pos = SysCurPos(row+1,sp.bar_start)
      say copies(' ',sp.bar_len)
      sp.bar_spot = sp.bar_start
      end
   else sp.bar_spot = sp.bar_spot + 1

   end

/*----- Last info line, full width of box -----*/

if sp.show_elapsed = 0 & sp.show_pbar = 1 then scr_pos = SysCurPos(row+3,2)
else scr_pos = SysCurPos(row+2,2)
if pos('ERROR',translate(value(sp.line.last))) > 0 then do
   stat_clr = sp.clr.err
   call Beep 1500, 100
   end
else stat_clr = sp.clr.hi
say stat_clr||left(value(sp.line.last),sp.width-6,' ')||sp.clr.norm

return

/*-----------------------------------------------------------------------*/
/*----- Initialize: Initialize routine                              -----*/
/*-----------------------------------------------------------------------*/

Initialize:

/*---- Need the following REXX util func -----*/

if RxFuncQuery('SysLoadFuncs') then do
   call RxFuncAdd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs'
   call SysLoadFuncs
   end

sp. = ''                               /* Array for ShowProg's use           */
sp.show_elapsed = 1                    /* 1=show elapsed time                */
sp.show_pbar    = 1                    /* 1=show progress bar                */
sp.width = 65                          /* Width of progress box              */
sp.bar_num = 1                         /* Initialize progress bar            */

sp.line.title = 'RxShowMe v1.2 05/13/94' /* Title line                       */
sp.line.last = 'status'                /* Variable name for status message   */
                                       /*     on bottom line                 */

sp.line.0 = 3                          /* Number of detail lines             */
sp.line.desc.1 = 'Info'                /* Text description for 1st line      */
sp.line.stat.1 = 'showprog_info'       /* Variable name for info on 1sd line */
sp.line.desc.2 = 'Processing file'     /* Text description for 2nd line      */
sp.line.stat.2 = 'file_name'           /* Variable name for info on 2nd line */
sp.line.desc.3 = 'Description'         /* Text description for 3rd line      */
sp.line.stat.3 = 'file_desc'           /* Variable name for info on 3rd line */

/*----- List of variable names to expose to ShowProg routine -----*/

sp.vals = sp.line.last
do i=1 to sp.line.0
   sp.vals = sp.vals||' '||sp.line.stat.i
   end

/*----- Screen colours -----*/

sp.clr.norm = d2c(27)||d2c(91)||"0m"         /* default screen colour        */
sp.clr.main = d2c(27)||d2c(91)||"1;37;40m"   /* white on black               */
sp.clr.hi   = d2c(27)||d2c(91)||"1;36;40m"   /* cyan on black                */
sp.clr.err  = d2c(27)||d2c(91)||"1;37;41m"   /* red on white                 */

sp.clr.red  = d2c(27)||d2c(91)||"1;31;40m"
sp.clr.grn  = d2c(27)||d2c(91)||"1;32;40m"
sp.clr.yel  = d2c(27)||d2c(91)||"1;33;40m"
sp.clr.blu  = d2c(27)||d2c(91)||"1;34;40m"
sp.clr.mag  = d2c(27)||d2c(91)||"1;35;40m"

return