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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.eclipse.jgit.lib;
46
47 import java.io.IOException;
48 import java.io.PrintWriter;
49 import java.io.Writer;
50
51
52 public class TextProgressMonitor extends BatchingProgressMonitor {
53 private final Writer out;
54
55 private boolean write;
56
57
58 public TextProgressMonitor() {
59 this(new PrintWriter(System.err));
60 }
61
62
63
64
65
66
67
68 public TextProgressMonitor(Writer out) {
69 this.out = out;
70 this.write = true;
71 }
72
73 @Override
74 protected void onUpdate(String taskName, int workCurr) {
75 StringBuilder s = new StringBuilder();
76 format(s, taskName, workCurr);
77 send(s);
78 }
79
80 @Override
81 protected void onEndTask(String taskName, int workCurr) {
82 StringBuilder s = new StringBuilder();
83 format(s, taskName, workCurr);
84 s.append("\n");
85 send(s);
86 }
87
88 private void format(StringBuilder s, String taskName, int workCurr) {
89 s.append("\r");
90 s.append(taskName);
91 s.append(": ");
92 while (s.length() < 25)
93 s.append(' ');
94 s.append(workCurr);
95 }
96
97 @Override
98 protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
99 StringBuilder s = new StringBuilder();
100 format(s, taskName, cmp, totalWork, pcnt);
101 send(s);
102 }
103
104 @Override
105 protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
106 StringBuilder s = new StringBuilder();
107 format(s, taskName, cmp, totalWork, pcnt);
108 s.append("\n");
109 send(s);
110 }
111
112 private void format(StringBuilder s, String taskName, int cmp,
113 int totalWork, int pcnt) {
114 s.append("\r");
115 s.append(taskName);
116 s.append(": ");
117 while (s.length() < 25)
118 s.append(' ');
119
120 String endStr = String.valueOf(totalWork);
121 String curStr = String.valueOf(cmp);
122 while (curStr.length() < endStr.length())
123 curStr = " " + curStr;
124 if (pcnt < 100)
125 s.append(' ');
126 if (pcnt < 10)
127 s.append(' ');
128 s.append(pcnt);
129 s.append("% (");
130 s.append(curStr);
131 s.append("/");
132 s.append(endStr);
133 s.append(")");
134 }
135
136 private void send(StringBuilder s) {
137 if (write) {
138 try {
139 out.write(s.toString());
140 out.flush();
141 } catch (IOException err) {
142 write = false;
143 }
144 }
145 }
146 }