1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import java.io.IOException;
14 import java.io.OutputStream;
15
16 import org.eclipse.jgit.lib.BatchingProgressMonitor;
17 import org.eclipse.jgit.lib.Constants;
18
19
20 class SideBandProgressMonitor extends BatchingProgressMonitor {
21 private final OutputStream out;
22
23 private boolean write;
24
25 SideBandProgressMonitor(OutputStream os) {
26 out = os;
27 write = true;
28 }
29
30
31 @Override
32 protected void onUpdate(String taskName, int workCurr) {
33 StringBuilder s = new StringBuilder();
34 format(s, taskName, workCurr);
35 s.append(" \r");
36 send(s);
37 }
38
39
40 @Override
41 protected void onEndTask(String taskName, int workCurr) {
42 StringBuilder s = new StringBuilder();
43 format(s, taskName, workCurr);
44 s.append(", done\n");
45 send(s);
46 }
47
48 private void format(StringBuilder s, String taskName, int workCurr) {
49 s.append(taskName);
50 s.append(": ");
51 s.append(workCurr);
52 }
53
54
55 @Override
56 protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
57 StringBuilder s = new StringBuilder();
58 format(s, taskName, cmp, totalWork, pcnt);
59 s.append(" \r");
60 send(s);
61 }
62
63
64 @Override
65 protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
66 StringBuilder s = new StringBuilder();
67 format(s, taskName, cmp, totalWork, pcnt);
68 s.append("\n");
69 send(s);
70 }
71
72 private void format(StringBuilder s, String taskName, int cmp,
73 int totalWork, int pcnt) {
74 s.append(taskName);
75 s.append(": ");
76 if (pcnt < 100)
77 s.append(' ');
78 if (pcnt < 10)
79 s.append(' ');
80 s.append(pcnt);
81 s.append("% (");
82 s.append(cmp);
83 s.append("/");
84 s.append(totalWork);
85 s.append(")");
86 }
87
88 private void send(StringBuilder s) {
89 if (write) {
90 try {
91 out.write(Constants.encode(s.toString()));
92 out.flush();
93 } catch (IOException err) {
94 write = false;
95 }
96 }
97 }
98 }