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