1 #Region "Microsoft.VisualBasic::e93d47961896d9fcaad122fc7e9c01ed, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\ProgressBar\SwayBar.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class SwayBar
35     
36     
37     '         Enum direction
38     
39     '             left, right
40     
41     
42     
43     '  
44     
45     '     Constructor: (+1 OverloadsSub New
46     
47     '     Function: buildBlankPointer
48     
49     '     Sub: [Step], ClearBar, PlacePointer
50     
51     
52     ' /********************************************************************************/
53
54 #End Region
55
56 Imports System.Text
57
58 Namespace Terminal.ProgressBar
59
60     ''' <summary>
61     ''' 像乒乓球一样左右碰撞的进度条
62     ''' </summary>
63     Public Class SwayBar : Inherits AbstractBar
64
65         Dim bar As String
66         Dim pointer As String
67         Dim blankPointer As String
68         Dim counter As Integer
69         Dim currdir As direction
70
71         Private Enum direction
72             right
73             left
74         End Enum
75
76         ''' <summary>
77         ''' 
78         ''' </summary>
79         ''' <param name="length%">
80         ''' 进度条的碰撞区域的字符数量宽度
81         ''' </param>
82         Public Sub New(Optional length% = 50)
83             MyBase.New()
84
85             bar = $"|{New String(" "c, length)}|"
86             pointer = "***"
87             blankPointer = buildBlankPointer()
88             currdir = direction.right
89             counter = 1
90         End Sub
91
92         ''' <summary>
93         ''' sets the atribute blankPointer with a empty string the same length that the pointer
94         ''' </summary>
95         ''' <returns>A string filled with space characters</returns>
96         Private Function buildBlankPointer() As String
97             Dim blank As New StringBuilder()
98
99             For cont As Integer = 0 To pointer.Length - 1
100                 blank.Append(" ")
101             Next
102
103             Return blank.ToString()
104         End Function
105
106         ''' <summary>
107         ''' reset the bar to its original state
108         ''' </summary>
109         Private Sub ClearBar()
110             bar = bar.Replace(pointer, blankPointer)
111         End Sub
112
113         ''' <summary>
114         ''' remove the previous pointer and place it in a new possition
115         ''' </summary>
116         ''' <param name="start">start index</param>
117         ''' <param name="end">end index</param>
118         Private Sub PlacePointer(start As Integer, [end] As Integer)
119             Call ClearBar()
120
121             bar = bar.Remove(start, [end])
122             bar = bar.Insert(start, pointer)
123         End Sub
124
125         ''' <summary>
126         ''' prints the progress bar acorrding to pointers and current direction
127         ''' </summary>
128         Public Overrides Sub [Step]()
129             If currdir = direction.right Then
130                 PlacePointer(counter, pointer.Length)
131                 counter += 1
132
133                 If counter + pointer.Length = bar.Length Then
134                     currdir = direction.left
135                 End If
136             Else
137                 PlacePointer(counter - pointer.Length, pointer.Length)
138                 counter -= 1
139
140                 If counter = pointer.Length Then
141                     currdir = direction.right
142                 End If
143             End If
144
145             Call Console.Write(bar & vbCr)
146         End Sub
147     End Class
148 End Namespace