aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/transport/SideBandProgressMonitor.java
blob: 1e85d81084369938cc0eedd4d4b78022033dd9c6 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2008-2010, Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.transport;

import java.io.IOException;
import java.io.OutputStream;
import java.time.Duration;

import org.eclipse.jgit.lib.BatchingProgressMonitor;
import org.eclipse.jgit.lib.Constants;

/** Write progress messages out to the sideband channel. */
class SideBandProgressMonitor extends BatchingProgressMonitor {
	private final OutputStream out;

	private boolean write;

	SideBandProgressMonitor(OutputStream os) {
		out = os;
		write = true;
	}

	@Override
	protected void onUpdate(String taskName, int workCurr, Duration duration) {
		StringBuilder s = new StringBuilder();
		format(s, taskName, workCurr, duration);
		s.append("   \r"); //$NON-NLS-1$
		send(s);
	}

	@Override
	protected void onEndTask(String taskName, int workCurr, Duration duration) {
		StringBuilder s = new StringBuilder();
		format(s, taskName, workCurr, duration);
		s.append(", done\n"); //$NON-NLS-1$
		send(s);
	}

	private void format(StringBuilder s, String taskName, int workCurr,
			Duration duration) {
		s.append(taskName);
		s.append(": "); //$NON-NLS-1$
		s.append(workCurr);
		appendDuration(s, duration);
	}

	@Override
	protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt,
			Duration duration) {
		StringBuilder s = new StringBuilder();
		format(s, taskName, cmp, totalWork, pcnt, duration);
		s.append("   \r"); //$NON-NLS-1$
		send(s);
	}

	@Override
	protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt,
			Duration duration) {
		StringBuilder s = new StringBuilder();
		format(s, taskName, cmp, totalWork, pcnt, duration);
		s.append("\n"); //$NON-NLS-1$
		send(s);
	}

	private void format(StringBuilder s, String taskName, int cmp,
			int totalWork, int pcnt, Duration duration) {
		s.append(taskName);
		s.append(": "); //$NON-NLS-1$
		if (pcnt < 100)
			s.append(' ');
		if (pcnt < 10)
			s.append(' ');
		s.append(pcnt);
		s.append("% ("); //$NON-NLS-1$
		s.append(cmp);
		s.append('/');
		s.append(totalWork);
		s.append(')');
		appendDuration(s, duration);
	}

	private void send(StringBuilder s) {
		if (write) {
			try {
				out.write(Constants.encode(s.toString()));
				out.flush();
			} catch (IOException err) {
				write = false;
			}
		}
	}
}