1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.util;
26
27
28 import java.util.*;
29
30 import mk.fsgrep.util.output.OutputDestination;
31
32
33
34 /***
35 * Draws a text-based progress bar (percentage completion).
36 *
37 * @author Murali Krishnan
38 *
39 */
40 public class TextProgressBar implements ProgressBar {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 private LinkedList<Integer> _segments = null;
56 private int _totalCount = 0;
57 private int _segmentCount = 0;
58 private OutputDestination _out = null;
59 private boolean _defined = false;
60
61
62
63
64
65
66 public TextProgressBar(OutputDestination pOut) {
67 _out = pOut;
68 }
69
70
71
72
73
74 protected LinkedList<Integer> getSegments() {return _segments;}
75 protected int getTotalCount() {return _totalCount;}
76 protected int getSegmentCount() {return _segmentCount;}
77 protected OutputDestination getOut() {return _out;}
78 protected boolean isDefined() {return _defined;}
79
80
81
82
83
84
85 protected void setOut(OutputDestination val) {_out=val;}
86
87
88
89
90
91 protected void decrTotalCount() {_totalCount--;}
92
93 protected void decrSegmentCount() {
94 if (--_segmentCount <= 0) {
95 getOut().print("=");
96 initializeSegment();
97 }
98 }
99
100
101 protected void initializeSegment() {
102 if (!getSegments().isEmpty()) {
103 _segmentCount = getSegments().removeFirst();
104 }
105 }
106
107
108 protected void printHeader() {
109 String header = "+-- Progress Bar -----------------------------------------------------------+";
110
111 if (getTotalCount() < 79) {
112 StringBuffer buffer = new StringBuffer("+");
113 for (int i = 1; i < getTotalCount()-1; i++) {
114 buffer.append("-");
115 }
116 buffer.append("+");
117
118 header = buffer.toString();
119 }
120
121 getOut().println(header);
122 }
123
124
125
126
127
128
129
130
131
132
133 public void update() {
134 if (isDefined()) {
135 decrSegmentCount();
136 decrTotalCount();
137 }
138 }
139
140
141 public void finish() {
142 if (isDefined()) {
143 getOut().println("");
144
145 if (getTotalCount() > 0) {
146 System.err.println("(" + getTotalCount() + " missing.)");
147 }
148 }
149 }
150
151
152 public void initialize(int pTotalCount) {
153 _segments = new LinkedList<Integer>();
154 _totalCount = 0;
155 _segmentCount = 0;
156 _defined = false;
157
158
159
160 if (pTotalCount > 100) {
161 _defined = true;
162 _totalCount = pTotalCount;
163
164 if (getTotalCount() > 79) {
165 double exactQuotient;
166 double remaider;
167 double bucket = 0.0;
168 int intPart;
169 int total = getTotalCount();
170 for (int i=79; i>0; i--) {
171 exactQuotient = ((double) total) / i;
172 intPart = (int) exactQuotient;
173 remaider = exactQuotient - intPart;
174 bucket += remaider;
175 if (bucket > 1.0) {
176 intPart++;
177 bucket -= 1.0;
178 }
179
180 getSegments().addLast(intPart);
181 total -= intPart;
182 }
183 } else {
184 for (int i = 0; i < getTotalCount(); i++) {
185 getSegments().add(1);
186 }
187 }
188
189 initializeSegment();
190
191 printHeader();
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204
205 }