Text-Based Progress Bar

General FreeBASIC programming questions.
Post Reply
NorbyDroid
Posts: 72
Joined: May 21, 2016 22:55

Text-Based Progress Bar

Post by NorbyDroid »

Here is a progress bar program I put together for use in other projects. There are plenty of options available.

Place the bar on the screen with or without being surrounded by a box. There are multiple different box styles to choose from, different bar styles as well.

With the box option you can have up to a 78 character bar. 80 without the box. Justify is available which is used to place the bar on the Left, Center, or Right. Bar and Box Colors can be changed.

Progress Info has 5 different options (6 if ya count Off). 1 displays just the values, 2 will display just the simple percentage, 3 is the same as 2 but adds the word "complete" if that is desired, 4 is #1 and #2 together,and the last one 5 is #1 and #3 together.

To add more styles all ya need to do is add them to Progress.Init and you are all set.

On with the code. Let me know what ya think and if I missed any features, or there are some feature ya would like to see added. Enjoy.

Code: Select all

Type NewBar
  As Integer ErrorFlag

  ' Bar Position {Note: BoxPos is Row-1, Col-1}
  As Integer Row, Col
  
  ' Current, Start, and Finish Values
  As Integer ValueBar, LowBar, HighBar
  
  As Integer WidthBar

  ' 0 Left, 1 Center, 2 Right
  As Integer Justify

  ' Colors 1-15
  As Integer ColorBar, ColorBox

  ' 0 Off, 1 On
  As Integer ShowBox

  ' 0 Off           , 1 Values
  ' 2 Percent       , 3 Percent Complete
  ' 4 Values+Percent, 5 Values+Percent Complete
  As Integer ShowInfo

  As Integer BarMaxStyle, StyleBar, BoxMaxStyle, StyleBox

  ' Look of the Bar and Box
  As String BarStyles, Center, Sides
  As String LeftSide, BottomLeft, TopLeft
  As String RightSide, BottomRight, TopRight

  Declare Function BarStyle(Tag As Integer, Length As Integer) As String
  Declare Function BoxCenter(Tag As Integer, Length As Integer) As String

  Declare Sub DebugInfo()
  Declare Sub ProgressBar()
  Declare Sub ProgressInit()
  Declare Sub ProgressInfo(Percent As Integer)
  Declare Sub ProgressBox(LengthBar As Integer)
End Type

Sub NewBar.DebugInfo()
  Color 7
  Locate 2, 2: Print "Bar Position: "; Str(Row); ","; Str(Col);
  Locate 2, 32: Print "Bar Length: "; Str(WidthBar);
  
  Locate 4, 2: Print "Justify: ";
  Select Case Justify
    Case 0: Print "0 Off   "
    Case 1: Print "1 Left  "
    Case 2: Print "2 Center"
    Case 3: Print "3 Right "
  End Select
  
  Locate 4, 32: Print "ShowBox: ";
  If ShowBox Then Print "On "; Else Print "Off";
  
  Locate 4, 52: Print "ShowInfo: "; Str(ShowInfo)

  Locate 6, 2: Print "Bar Style: "; Str(StyleBar);
  Print " ["; BarStyle(StyleBar, 1); "]";
  
  Locate 6, 2: Print "Box Style: "; Str(StyleBox);
End Sub

Function NewBar.BarStyle(Tag As Integer, Length As Integer) As String
  If Tag<0 Then Tag=0 Else If Tag>BarMaxStyle Then Tag=BarMaxStyle

  If Tag<31 Then
    ' Up to 32 Pre-Defined Bar Styles
    Return String(Length, Mid(BarStyles, Tag+1, 1))
  Else
    ' User Defined Style
    Return String(Length, Chr(Tag))
  End If
End Function

Function NewBar.BoxCenter(Tag As Integer, Length As Integer) As String
  If Tag<0 Then Tag=0 Else If Tag>BoxMaxStyle Then Tag=BoxMaxStyle

  Return String(Length, Mid(Center, Tag+1, 1))
End Function

Sub NewBar.ProgressBar()
  ErrorFlag=0

  If ShowBox Then
    If Row-1<1 Then Row=2
    If Col-1<1 Then Col=2
    If WidthBar>78 Then WidthBar=78
  Else
    If Row<1 Then Row=1
    If Col<1 Then Col=1
    If WidthBar>80 Then WidthBar=80
  End If

  ' Place Box on the Left, in the Center, or on the Right
  If Justify Then
    ' Is Justify valid? (Left/Center/Right)
    If Justify>3 Then Justify=3

    ' Left
    If Justify=1 Then If ShowBox Then Col=2 Else Col=1

    ' Center
    If Justify=2 Then Col=40-WidthBar\2+1
    
    ' Right
    If Justify=3 Then Col=81-WidthBar
  End If

  ' DebugInfo()
  
  ' Work with Low to High ranges or High to Low ranges
  If LowBar>HighBar Then Swap LowBar, HighBar

  ' Get Percentage and Progress Bar length
  Dim As Integer Percent=Int((ValueBar/HighBar)*100)
  Dim As Integer LengthBar=Int((WidthBar*Percent)/100)

  If ShowBox Then
    ' Show a box around the Progress Bar
    ProgressBox LengthBar
  Else
    If Row<1 Then Row=1
    If Row>25 Then Row=25

    If Col<1 Then Col=1
    If WidthBar>80 Then WidthBar=80

    ' Show Progress Bar
    Color ColorBar: Locate Row, Col, 0
    Print BarStyle(StyleBar, LengthBar); Space(WidthBar-LengthBar);
  End If

  ' Show Progress Information
  If ShowInfo>0 Then ProgressInfo Percent
End Sub

Sub NewBar.ProgressBox(LengthBar As Integer)
  Color ColorBox: Locate Row-1, Col-1, 0
  Print Mid(TopLeft, StyleBox+1, 1); BoxCenter(StyleBox, WidthBar); Mid(TopRight, StyleBox+1, 1);

  Locate Row, Col-1, 0
  Color ColorBox: Print Mid(Sides, StyleBox+1, 1);
  
  Color ColorBar
  Print BarStyle(StyleBar, LengthBar); Space(WidthBar-LengthBar);

  Color ColorBox: Print Mid(Sides, StyleBox+1, 1);

  Color ColorBox: Locate Row+1, Col-1, 0
  Print Mid(BottomLeft, StyleBox+1, 1); BoxCenter(StyleBox, WidthBar); Mid(BottomRight, StyleBox+1, 1);
End Sub

Sub NewBar.ProgressInfo(Percent As Integer)
  Dim As String tValue=Str(ValueBar)+" Of "+Str(HighBar)
  Dim As String tProg=Right("  "+Str(Percent), 3)+"% Complete"
  
  If ShowInfo=2 Or ShowInfo=4 Then tProg=Left(tProg, 4)

  Dim tStr As String
  If ShowInfo=1 Then tStr=tValue
  If ShowInfo=2 Or ShowInfo=3 Then tStr=tProg

  If ShowInfo=4 Or ShowInfo=5 Then tStr=tValue+" "+Chr(179)+" "+tProg

  ' Does Info fit on the bar?
  If Len(tStr)+2>WidthBar-2 Then tStr=""

  If ShowBox Then
    Locate Row-1, Col+WidthBar\2-Len(tStr)\2
    Color ColorBox: Print Mid(LeftSide, StyleBox+1, 1);
    Color ColorBar: Print " "; tStr; " ";
    Color ColorBox: Print Mid(RightSide, StyleBox+1, 1);
  Else
    Color ColorBar
    Locate Row-1, Col+WidthBar\2-Len(tStr)\2: Print " "; tStr; " ";
  End If
End Sub

Sub NewBar.ProgressInit()
  BarStyles=Chr(176)+Chr(177)+Chr(178)+Chr(219)+Chr(254)

  Sides=Chr(179)+Chr(179)+Chr(186)+Chr(186)+"|"
  Center=Chr(196)+Chr(205)+Chr(205)+Chr(196)+"-"
  
  TopLeft=Chr(218)+Chr(213)+Chr(201)+Chr(214)+"+"
  TopRight=Chr(191)+Chr(184)+Chr(187)+Chr(183)+"+"
  
  BottomLeft=Chr(192)+Chr(212)+Chr(200)+Chr(211)+"+"
  BottomRight=Chr(217)+Chr(190)+Chr(188)+Chr(189)+"+"
  
  LeftSide=Chr(180)+Chr(181)+Chr(185)+Chr(182)
  RightSide=Chr(195)+Chr(198)+Chr(204)+Chr(199)
  
  BoxMaxStyle=Len(Sides)
  BarMaxStyle=Len(BarStyles)
End Sub
NorbyDroid
Posts: 72
Joined: May 21, 2016 22:55

Re: Text-Based Progress Bar

Post by NorbyDroid »

Here is an update that includes a Demo with it. Check the TYPE at the top of the code where the comments explain what the different options does and there value.

Code: Select all

Type NewBar
  ' Bar Position {Note: BoxPos is Row-1, Col-1}
  As Integer Row, Col
  
  ' Current, Start, and Finish Values
  As Integer ValueBar, LowBar, HighBar
  
  As Integer WidthBar

  ' Justification
  ' 0 Left, 1 Center, 2 Right
  As Integer Justify

  ' Colors 1-15
  As Integer ColorBar, ColorBox

  ' Show a box around the Progress Bar
  ' 0 Off, 1 On
  As Integer ShowBox

  ' Show Information above the Progress Bar
  ' 0 Off           , 1 Values
  ' 2 Percent       , 3 Percent Complete
  ' 4 Values+Percent, 5 Values+Percent Complete
  As Integer ShowInfo

  As Integer BarMaxStyle, StyleBar, BoxMaxStyle, StyleBox

  ' Change the look of the Bar and Box
  As String BarStyles, Center, Sides
  As String LeftSide, BottomLeft, TopLeft
  As String RightSide, BottomRight, TopRight

  Declare Function BarStyle(Tag As Integer, Length As Integer) As String
  Declare Function BoxCenter(Tag As Integer, Length As Integer) As String

  Declare Sub DebugInfo()
  Declare Sub ProgressBar()
  Declare Sub ProgressInit()
  Declare Sub ProgressInfo(Percent As Integer)
  Declare Sub ProgressBox(LengthBar As Integer)
End Type

Sub NewBar.DebugInfo()
  Color 7
  Locate 2, 2: Print "Bar Position: "; Str(Row); ","; Str(Col);
  Locate 2, 32: Print "Bar Length: "; Str(WidthBar);
  
  Locate 4, 2: Print "Justify: ";
  Select Case Justify
    Case 0: Print "0 Off   "
    Case 1: Print "1 Left  "
    Case 2: Print "2 Center"
    Case 3: Print "3 Right "
  End Select
  
  Locate 4, 32: Print "ShowBox: ";
  If ShowBox Then Print "On "; Else Print "Off";
  
  Locate 4, 52: Print "ShowInfo: "; Str(ShowInfo)

  Locate 6, 2: Print "Bar Style: "; Str(StyleBar);
  Print " ["; BarStyle(StyleBar, 1); "]";
  
  Locate 6, 2: Print "Box Style: "; Str(StyleBox);
End Sub

Function NewBar.BarStyle(Tag As Integer, Length As Integer) As String
  If Tag<0 Then Tag=0 Else If Tag>BarMaxStyle Then Tag=BarMaxStyle

  If Tag<31 Then
    ' Up to 32 Pre-Defined Bar Styles
    Return String(Length, Mid(BarStyles, Tag+1, 1))
  Else
    ' User Defined Style
    Return String(Length, Chr(Tag))
  End If
End Function

Function NewBar.BoxCenter(Tag As Integer, Length As Integer) As String
  If Tag<0 Then Tag=0 Else If Tag>BoxMaxStyle Then Tag=BoxMaxStyle

  Return String(Length, Mid(Center, Tag+1, 1))
End Function

Sub NewBar.ProgressBar()
  If ShowBox Then
    If Row-1<1 Then Row=2
    If Col-1<1 Then Col=2
    If WidthBar>78 Then WidthBar=78
  Else
    If Row<1 Then Row=1
    If Col<1 Then Col=1
    If WidthBar>80 Then WidthBar=80
  End If

  ' Place Box on the Left, in the Center, or on the Right
  If Justify Then
    ' Is Justify valid? (Left/Center/Right)
    If Justify>3 Then Justify=3

    ' Left
    If Justify=1 Then If ShowBox Then Col=2 Else Col=1

    ' Center
    If Justify=2 Then Col=40-WidthBar\2+1
    
    ' Right
    If Justify=3 Then Col=81-WidthBar
  End If
  
  ' Work with Low to High ranges or High to Low ranges
  If LowBar>HighBar Then Swap LowBar, HighBar

  ' Get Percentage and Progress Bar length
  Dim As Integer Percent=Int((ValueBar/HighBar)*100)
  Dim As Integer LengthBar=Int((WidthBar*Percent)/100)

  If ShowBox Then
    ' Show a box around the Progress Bar
    ProgressBox LengthBar
  Else
    If Row<1 Then Row=1
    If Row>25 Then Row=25

    If Col<1 Then Col=1
    If WidthBar>80 Then WidthBar=80

    ' Show Progress Bar
    Color ColorBar: Locate Row, Col, 0
    Print BarStyle(StyleBar, LengthBar); Space(WidthBar-LengthBar);
  End If

  ' Show Progress Information
  If ShowInfo>0 Then ProgressInfo Percent
End Sub

Sub NewBar.ProgressBox(LengthBar As Integer)
  Color ColorBox: Locate Row-1, Col-1, 0
  Print Mid(TopLeft, StyleBox+1, 1); BoxCenter(StyleBox, WidthBar); Mid(TopRight, StyleBox+1, 1);

  Locate Row, Col-1, 0
  Color ColorBox: Print Mid(Sides, StyleBox+1, 1);
  
  Color ColorBar
  Print BarStyle(StyleBar, LengthBar); Space(WidthBar-LengthBar);

  Color ColorBox: Print Mid(Sides, StyleBox+1, 1);

  Color ColorBox: Locate Row+1, Col-1, 0
  Print Mid(BottomLeft, StyleBox+1, 1); BoxCenter(StyleBox, WidthBar); Mid(BottomRight, StyleBox+1, 1);
End Sub

Sub NewBar.ProgressInfo(Percent As Integer)
  Dim As String tValue=Str(ValueBar)+" Of "+Str(HighBar)
  Dim As String tProg=Right("  "+Str(Percent), 3)+"% Complete"
  
  If ShowInfo=2 Or ShowInfo=4 Then tProg=Left(tProg, 4)

  Dim tStr As String
  If ShowInfo=1 Then tStr=tValue
  If ShowInfo=2 Or ShowInfo=3 Then tStr=tProg

  If ShowInfo=4 Or ShowInfo=5 Then tStr=tValue+" "+Chr(179)+" "+tProg

  ' Does Info fit on the bar?
  If Len(tStr)+2>WidthBar-2 Then tStr=""

  If ShowBox Then
    Locate Row-1, Col+WidthBar\2-Len(tStr)\2
    Color ColorBox: Print Mid(LeftSide, StyleBox+1, 1);
    Color ColorBar: Print " "; tStr; " ";
    Color ColorBox: Print Mid(RightSide, StyleBox+1, 1);
  Else
    Color ColorBar
    Locate Row-1, Col+WidthBar\2-Len(tStr)\2: Print " "; tStr; " ";
  End If
End Sub

Sub NewBar.ProgressInit()
  Row=24: Col=1
  
  ColorBar=3
  ShowInfo=3
  WidthBar=80

  'Character used to draw the Progress Bar
  BarStyles=Chr(176)+Chr(177)+Chr(178)+Chr(219)+Chr(254)

  ' Characters used to draw the box around the Progress Bar
  Sides=Chr(179)+Chr(179)+Chr(186)+Chr(186)+"|"
  Center=Chr(196)+Chr(205)+Chr(205)+Chr(196)+"-"
  TopLeft=Chr(218)+Chr(213)+Chr(201)+Chr(214)+"+"
  TopRight=Chr(191)+Chr(184)+Chr(187)+Chr(183)+"+"
  BottomLeft=Chr(192)+Chr(212)+Chr(200)+Chr(211)+"+"
  BottomRight=Chr(217)+Chr(190)+Chr(188)+Chr(189)+"+"
  
  ' Characters used to draw the characters for 'ShowInfo'
  LeftSide=Chr(180)+Chr(181)+Chr(185)+Chr(182)
  RightSide=Chr(195)+Chr(198)+Chr(204)+Chr(199)
  
  ' Length of the Data
  BoxMaxStyle=Len(Sides)
  BarMaxStyle=Len(BarStyles)
End Sub

' Just a Basic Demo

Sub Demo()
  ' Create a new progress bar called DemoBar
  Dim DemoBar As NewBar
  
  ' Setup the Progress Bar
  DemoBar.ProgressInit()

  DemoBar.LowBar=1
  DemoBar.HighBar=1000
  For t As Integer=DemoBar.LowBar to DemoBar.HighBar
    DemoBar.ValueBar=t: DemoBar.ProgressBar(): Sleep 100
  Next    
End Sub

CLS
Demo()
Post Reply