1 | #Region "Microsoft.VisualBasic::38debb6d764deb9a2c989eb6906b51c9, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\Utility\EventProc.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 EventProc |
35 | ' |
36 | ' Properties: Capacity |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: Tick, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.IO |
47 | Imports System.Runtime.CompilerServices |
48 | Imports Microsoft.VisualBasic.Language |
49 | Imports sys = System.Math |
50 | |
51 | Namespace Terminal.Utility |
52 | |
53 | ''' <summary> |
54 | ''' Generates the task progress for the console output.(处理任务进度) |
55 | ''' </summary> |
56 | Public Class EventProc |
57 | |
58 | ''' <summary> |
59 | ''' The total <see cref="Tick"/> |
60 | ''' </summary> |
61 | ''' <returns></returns> |
62 | Public Property Capacity As Integer |
63 | Get |
64 | Return _capacity |
65 | End Get |
66 | Set(value As Integer) |
67 | _capacity = value |
68 | delta = Capacity / 100 |
69 | current = CInt(_capacity * percentage) |
70 | End Set |
71 | End Property |
72 | |
73 | Dim _capacity As Integer |
74 | Dim delta As Integer |
75 | Dim tag As String |
76 | Dim out As StreamWriter |
77 | Dim current As int = 0 |
78 | |
79 | ''' <summary> |
80 | ''' |
81 | ''' </summary> |
82 | ''' <param name="n"></param> |
83 | ''' <param name="tag"></param> |
84 | ''' <param name="out">Default is <see cref="Console"/></param> |
85 | Sub New(n As Integer, <CallerMemberName> Optional tag As String = "", Optional out As StreamWriter = Nothing) |
86 | Me.Capacity = n |
87 | Me.tag = tag |
88 | Me.out = If(out Is Nothing, |
89 | New StreamWriter(Console.OpenStandardOutput), |
90 | out) |
91 | |
92 | If String.IsNullOrEmpty(Me.tag) Then |
93 | Me.tag = vbTab |
94 | End If |
95 | End Sub |
96 | |
97 | ''' <summary> |
98 | ''' 会自动输出进度的 |
99 | ''' </summary> |
100 | ''' <returns></returns> |
101 | Public Function Tick() As Integer |
102 | If delta = 0 Then |
103 | Return 0 |
104 | End If |
105 | |
106 | If ++current Mod delta = 0 Then |
107 | Call ToString.__DEBUG_ECHO |
108 | Else |
109 | Call out.Write(".") |
110 | End If |
111 | |
112 | Return current |
113 | End Function |
114 | |
115 | ''' <summary> |
116 | ''' Current progress percentage. |
117 | ''' </summary> |
118 | Dim percentage As Double |
119 | Dim sw As Stopwatch = Stopwatch.StartNew |
120 | ''' <summary> |
121 | ''' Previous <see cref="Stopwatch.ElapsedMilliseconds"/> |
122 | ''' </summary> |
123 | Dim preElapsedMilliseconds As Long |
124 | |
125 | ''' <summary> |
126 | ''' Generates progress output |
127 | ''' </summary> |
128 | ''' <returns></returns> |
129 | Public Overrides Function ToString() As String |
130 | If Capacity = 0 Then |
131 | Return "" |
132 | End If |
133 | |
134 | Dim dt As Long = sw.ElapsedMilliseconds - preElapsedMilliseconds |
135 | preElapsedMilliseconds = sw.ElapsedMilliseconds |
136 | percentage = current / Capacity |
137 | |
138 | Return $" [{tag}, {dt}ms] * ...... {sys.Round(100 * percentage, 2)}%" |
139 | End Function |
140 | End Class |
141 | End Namespace |