View Javadoc
1   /*
2    * Copyright (C) 2008-2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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  /** Write progress messages out to the sideband channel. */
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  	/** {@inheritDoc} */
31  	@Override
32  	protected void onUpdate(String taskName, int workCurr) {
33  		StringBuilder s = new StringBuilder();
34  		format(s, taskName, workCurr);
35  		s.append("   \r"); //$NON-NLS-1$
36  		send(s);
37  	}
38  
39  	/** {@inheritDoc} */
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"); //$NON-NLS-1$
45  		send(s);
46  	}
47  
48  	private void format(StringBuilder s, String taskName, int workCurr) {
49  		s.append(taskName);
50  		s.append(": "); //$NON-NLS-1$
51  		s.append(workCurr);
52  	}
53  
54  	/** {@inheritDoc} */
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"); //$NON-NLS-1$
60  		send(s);
61  	}
62  
63  	/** {@inheritDoc} */
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"); //$NON-NLS-1$
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(": "); //$NON-NLS-1$
76  		if (pcnt < 100)
77  			s.append(' ');
78  		if (pcnt < 10)
79  			s.append(' ');
80  		s.append(pcnt);
81  		s.append("% ("); //$NON-NLS-1$
82  		s.append(cmp);
83  		s.append("/"); //$NON-NLS-1$
84  		s.append(totalWork);
85  		s.append(")"); //$NON-NLS-1$
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  }