1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.lib;
13
14 import static java.nio.charset.StandardCharsets.UTF_8;
15
16 import java.io.IOException;
17 import java.io.OutputStreamWriter;
18 import java.io.PrintWriter;
19 import java.io.Writer;
20
21
22
23
24 public class TextProgressMonitor extends BatchingProgressMonitor {
25 private final Writer out;
26
27 private boolean write;
28
29
30
31
32 public TextProgressMonitor() {
33 this(new PrintWriter(new OutputStreamWriter(System.err, UTF_8)));
34 }
35
36
37
38
39
40
41
42 public TextProgressMonitor(Writer out) {
43 this.out = out;
44 this.write = true;
45 }
46
47
48 @Override
49 protected void onUpdate(String taskName, int workCurr) {
50 StringBuilder s = new StringBuilder();
51 format(s, taskName, workCurr);
52 send(s);
53 }
54
55
56 @Override
57 protected void onEndTask(String taskName, int workCurr) {
58 StringBuilder s = new StringBuilder();
59 format(s, taskName, workCurr);
60 s.append("\n");
61 send(s);
62 }
63
64 private void format(StringBuilder s, String taskName, int workCurr) {
65 s.append("\r");
66 s.append(taskName);
67 s.append(": ");
68 while (s.length() < 25)
69 s.append(' ');
70 s.append(workCurr);
71 }
72
73
74 @Override
75 protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
76 StringBuilder s = new StringBuilder();
77 format(s, taskName, cmp, totalWork, pcnt);
78 send(s);
79 }
80
81
82 @Override
83 protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
84 StringBuilder s = new StringBuilder();
85 format(s, taskName, cmp, totalWork, pcnt);
86 s.append("\n");
87 send(s);
88 }
89
90 private void format(StringBuilder s, String taskName, int cmp,
91 int totalWork, int pcnt) {
92 s.append("\r");
93 s.append(taskName);
94 s.append(": ");
95 while (s.length() < 25)
96 s.append(' ');
97
98 String endStr = String.valueOf(totalWork);
99 String curStr = String.valueOf(cmp);
100 while (curStr.length() < endStr.length())
101 curStr = " " + curStr;
102 if (pcnt < 100)
103 s.append(' ');
104 if (pcnt < 10)
105 s.append(' ');
106 s.append(pcnt);
107 s.append("% (");
108 s.append(curStr);
109 s.append("/");
110 s.append(endStr);
111 s.append(")");
112 }
113
114 private void send(StringBuilder s) {
115 if (write) {
116 try {
117 out.write(s.toString());
118 out.flush();
119 } catch (IOException err) {
120 write = false;
121 }
122 }
123 }
124 }