'============================================================================== ' Source code snippet: PowerBASIC for DOS ' ' Author: James Goldbloom (James.Goldbloom@f611.n109.z1.fidonet.org) ' Copyright status: Public Domain ' ' Percent bar routine. ' '============================================================================== ' ' ' The PERCENT BAR graphical output will resemble these (for example): ' ' +-----------------|500/1000|------------------+ ' |50% ####################oooooooooooooooooooo| ' +---------------------------------------------+ ' ' +---------------------------------------------+ ' |50% ####################oooooooooooooooooooo| ' +---------------------------------------------+ ' ' The first one shows the graphics bar (the progress graphics really ' are spaces, but for purposes of this site, are graphical boxes instead) ' WITH numerical values displayed, the second without. Note that the ' numerical displays automatically format the box, so the output is clean ' and does not include unnecessary spaces and so forth. Below is the ' routine, which runs LIGHTNING fast within your nested loops during such ' operations as alphabetizing, searching, sorting, or other happenings ' where the user needs to be informed of progress. Enjoy... ' PERCENTAGE BAR ROUTINE - WORKS FORWARD OR BACKWARDS OR STATIC VALUE! ' Written by James Goldbloom (denied1@blkcat.com) for Power Basic users. ' Public domain code - distribute freely and enjoy (6/13/96 PB 3.2) ' Syntax shown below: ' ' CALL percentbar (valuebar,lowbar,highbar,ybar,xbar,widthbar,showvalues) ' ' Where: valuebar = current value of operation within the range ' lowbar = lowest possible (or beginning) value of range ' highbar = highest possible (or ending) value of range ' ybar = cursor line to place percentbar (y axis) ' xbar = corsor row to place percentbar (x axis) ' widthbar = width of percentbar dots (in columns) (max 72) ' and suggest values: 12,18,40,50,72 (they work.) ' showvalues = 0|1 (1 to display current/high valuees, 0=off) ' ' All values should be positive, but decimals are allowed. Widthbar ' seems to look the nicest at 72 (which is the limit.) Remember to freeze ' the cursor by suffixing print/screen output statements with semi-colons ' to preserve the colors and x,y positions properly within your loops. ' ' Use this routine for progress display during file/dir. reads or ANY loop ' operations by merely calling it using data from YOUR loops fed into it! ' Shows actual percentage (0-100%) to left of bar, automatically! ' Last color attribute/screen x,y position automatically preserved on exit! ' Work with high to low ranges, or low to high ranges (try it backwards!) ' Loops not required, you can optionally show a bar with "50%" instantly! DEFINT A-Z CLS COLOR 11, 0 PRINT "PERCENT BAR Demonstration...." PRINT PRINT "This is an instant loop, 50%:"; CALL percentbar( 50, 1, 100, CSRLIN, POS + 2, 36, 0 ) LOCATE 7, 1 PRINT "Another instant loop (values shown), 75%:"; CALL percentbar( 75, 1, 100, CSRLIN, POS + 2, 20, 1 ) LOCATE 11, 1 PRINT "THIS PROGRAM CAN ALSO BE USED WITHIN LOOPS, FOR/NEXT'S, ETC.!" PRINT: PRINT "A good range to test would be 1 to 10000" PRINT "And you can work backwards, too, such as 10000 to 1!" PRINT INPUT "Type in starting number"; begin IF begin = 0 THEN END INPUT "Type in ending number"; finish IF finish = 0 THEN END direction = 1 IF begin > finish THEN direction = -1 FOR count = begin TO finish STEP direction LOCATE 19, 1 COLOR 10 PRINT "Current loop value:" count "..."; CALL percentbar( count, begin, finish, 23, 1, 72, 1 ) NEXT count PRINT " Finished!" PRINT "Nifty, huh? Examine the code, see how it was done!" END 'BELOW IS THE SUB ROUTINE... '---------------------------------------------------------------------- 'PERCENTBAR Sub-Routine written by James Goldbloom (denied1@blkcat.com) '---------------------------------------------------------------------- ' 'Example: percentbar (50,1,100,24,1,72) <-- Display a 50% bar at bottom! ' of screen! ' SUB PERCENTBAR( valuebar, lowbar, highbar, ybar, xbar, widthbar, showvalues ) oldy = CSRLIN oldx = POS( 0 ) IF oldx = 1 THEN oldx = 2 ELSE IF oldx = 80 THEN oldx = 81 IF widthbar > 72 THEN widthbar = 72 fore% = SCREEN( oldy, oldx - 1, 1 ) back% = fore% fore% = fore% AND 15 SHIFT RIGHT back%, 4 IF lowbar > highbar THEN SWAP highbar, lowbar percentage = INT(( valuebar / highbar ) * 100 ) LOCATE ybar, xbar COLOR 9, 0 IF showvalues = 0 THEN PRINT CHR$( 218 ); REPEAT$( widthbar + 5, CHR$( 196 )) CHR$( 191 ); END IF IF showvalues = 1 THEN tempbar$ = MID$(STR$(valuebar), 2) + "/" + MID$(STR$(highbar), 2) PRINT CHR$(218); REPEAT$(INT(widthbar / 2) - 1, CHR$(196)) CHR$(180); COLOR 15 PRINT tempbar$; COLOR 9 PRINT CHR$(195); PRINT REPEAT$(widthbar-((widthbar/2)+LEN(tempbar$))+4,CHR$(196));CHR$(191); END IF LOCATE ybar + 1, xbar COLOR 9 PRINT CHR$(179); COLOR 14 PRINT MID$(STR$(percentage), 2);"%";SPACE$(5 - LEN(STR$(percentage))); drawbar = INT((widthbar * percentage) / 100) LOCATE ybar + 1, xbar + 6 COLOR 1, 1 PRINT REPEAT$(drawbar, CHR$(32)); IF (widthbar - drawbar) < > 1 THEN COLOR 11, 0 PRINT REPEAT$(widthbar - drawbar, CHR$(254)); COLOR 9 PRINT CHR$(179); END IF COLOR 9, 0 LOCATE ybar + 2, xbar PRINT CHR$(192); REPEAT$(widthbar + 5, CHR$(196)); CHR$(217); EXITBAR: LOCATE oldy, oldx COLOR fore%, back% tempbar$ = "" tempbar = 0 tempbarlength = 0 drawbar = 0 percentage = 0 ybar = 0 xbar = 0 END SUB