View Javadoc
1   /*
2    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
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   * A simple progress reporter printing on a stream.
23   */
24  public class TextProgressMonitor extends BatchingProgressMonitor {
25  	private final Writer out;
26  
27  	private boolean write;
28  
29  	/**
30  	 * Initialize a new progress monitor.
31  	 */
32  	public TextProgressMonitor() {
33  		this(new PrintWriter(new OutputStreamWriter(System.err, UTF_8)));
34  	}
35  
36  	/**
37  	 * Initialize a new progress monitor.
38  	 *
39  	 * @param out
40  	 *            the stream to receive messages on.
41  	 */
42  	public TextProgressMonitor(Writer out) {
43  		this.out = out;
44  		this.write = true;
45  	}
46  
47  	/** {@inheritDoc} */
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  	/** {@inheritDoc} */
56  	@Override
57  	protected void onEndTask(String taskName, int workCurr) {
58  		StringBuilder s = new StringBuilder();
59  		format(s, taskName, workCurr);
60  		s.append("\n"); //$NON-NLS-1$
61  		send(s);
62  	}
63  
64  	private void format(StringBuilder s, String taskName, int workCurr) {
65  		s.append("\r"); //$NON-NLS-1$
66  		s.append(taskName);
67  		s.append(": "); //$NON-NLS-1$
68  		while (s.length() < 25)
69  			s.append(' ');
70  		s.append(workCurr);
71  	}
72  
73  	/** {@inheritDoc} */
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  	/** {@inheritDoc} */
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"); //$NON-NLS-1$
87  		send(s);
88  	}
89  
90  	private void format(StringBuilder s, String taskName, int cmp,
91  			int totalWork, int pcnt) {
92  		s.append("\r"); //$NON-NLS-1$
93  		s.append(taskName);
94  		s.append(": "); //$NON-NLS-1$
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; //$NON-NLS-1$
102 		if (pcnt < 100)
103 			s.append(' ');
104 		if (pcnt < 10)
105 			s.append(' ');
106 		s.append(pcnt);
107 		s.append("% ("); //$NON-NLS-1$
108 		s.append(curStr);
109 		s.append("/"); //$NON-NLS-1$
110 		s.append(endStr);
111 		s.append(")"); //$NON-NLS-1$
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 }