aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProgressSpinner.java
blob: 78ed7e2123aa5277d3defd9b27c82e1fd00d697c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2015, 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 static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

/**
 * A simple spinner connected to an {@code OutputStream}.
 * <p>
 * This is class is not thread-safe. The update method may only be used from a
 * single thread. Updates are sent only as frequently as {@link #update()} is
 * invoked by the caller, and are capped at no more than 2 times per second by
 * requiring at least 500 milliseconds between updates.
 *
 * @since 4.2
 */
public class ProgressSpinner {
	private static final long MIN_REFRESH_MILLIS = 500;
	private static final char[] STATES = new char[] { '-', '\\', '|', '/' };

	private final OutputStream out;
	private String msg;
	private int state;
	private boolean write;
	private boolean shown;
	private long nextUpdateMillis;

	/**
	 * Initialize a new spinner.
	 *
	 * @param out
	 *            where to send output to.
	 */
	public ProgressSpinner(OutputStream out) {
		this.out = out;
		this.write = true;
	}

	/**
	 * Begin a time consuming task.
	 *
	 * @param title
	 *            description of the task, suitable for human viewing.
	 * @param delay
	 *            delay to wait before displaying anything at all.
	 * @param delayUnits
	 *            unit for {@code delay}.
	 */
	public void beginTask(String title, long delay, TimeUnit delayUnits) {
		msg = title;
		state = 0;
		shown = false;

		long now = System.currentTimeMillis();
		if (delay > 0) {
			nextUpdateMillis = now + delayUnits.toMillis(delay);
		} else {
			send(now);
		}
	}

	/**
	 * Update the spinner if it is showing.
	 */
	public void update() {
		long now = System.currentTimeMillis();
		if (now >= nextUpdateMillis) {
			send(now);
			state = (state + 1) % STATES.length;
		}
	}

	private void send(long now) {
		StringBuilder buf = new StringBuilder(msg.length() + 16);
		buf.append('\r').append(msg).append("... ("); //$NON-NLS-1$
		buf.append(STATES[state]);
		buf.append(")  "); //$NON-NLS-1$
		shown = true;
		write(buf.toString());
		nextUpdateMillis = now + MIN_REFRESH_MILLIS;
	}

	/**
	 * Denote the current task completed.
	 *
	 * @param result
	 *            text to print after the task's title
	 *            {@code "$title ... $result"}.
	 */
	public void endTask(String result) {
		if (shown) {
			write('\r' + msg + "... " + result + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	private void write(String s) {
		if (write) {
			try {
				out.write(s.getBytes(UTF_8));
				out.flush();
			} catch (IOException e) {
				write = false;
			}
		}
	}
}