1 /*
2 * Copyright (C) 2015, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.transport;
45
46 import static java.nio.charset.StandardCharsets.UTF_8;
47
48 import java.io.IOException;
49 import java.io.OutputStream;
50 import java.util.concurrent.TimeUnit;
51
52 /**
53 * A simple spinner connected to an {@code OutputStream}.
54 * <p>
55 * This is class is not thread-safe. The update method may only be used from a
56 * single thread. Updates are sent only as frequently as {@link #update()} is
57 * invoked by the caller, and are capped at no more than 2 times per second by
58 * requiring at least 500 milliseconds between updates.
59 *
60 * @since 4.2
61 */
62 public class ProgressSpinner {
63 private static final long MIN_REFRESH_MILLIS = 500;
64 private static final char[] STATES = new char[] { '-', '\\', '|', '/' };
65
66 private final OutputStream out;
67 private String msg;
68 private int state;
69 private boolean write;
70 private boolean shown;
71 private long nextUpdateMillis;
72
73 /**
74 * Initialize a new spinner.
75 *
76 * @param out
77 * where to send output to.
78 */
79 public ProgressSpinner(OutputStream out) {
80 this.out = out;
81 this.write = true;
82 }
83
84 /**
85 * Begin a time consuming task.
86 *
87 * @param title
88 * description of the task, suitable for human viewing.
89 * @param delay
90 * delay to wait before displaying anything at all.
91 * @param delayUnits
92 * unit for {@code delay}.
93 */
94 public void beginTask(String title, long delay, TimeUnit delayUnits) {
95 msg = title;
96 state = 0;
97 shown = false;
98
99 long now = System.currentTimeMillis();
100 if (delay > 0) {
101 nextUpdateMillis = now + delayUnits.toMillis(delay);
102 } else {
103 send(now);
104 }
105 }
106
107 /**
108 * Update the spinner if it is showing.
109 */
110 public void update() {
111 long now = System.currentTimeMillis();
112 if (now >= nextUpdateMillis) {
113 send(now);
114 state = (state + 1) % STATES.length;
115 }
116 }
117
118 private void send(long now) {
119 StringBuilder buf = new StringBuilder(msg.length() + 16);
120 buf.append('\r').append(msg).append("... ("); //$NON-NLS-1$
121 buf.append(STATES[state]);
122 buf.append(") "); //$NON-NLS-1$
123 shown = true;
124 write(buf.toString());
125 nextUpdateMillis = now + MIN_REFRESH_MILLIS;
126 }
127
128 /**
129 * Denote the current task completed.
130 *
131 * @param result
132 * text to print after the task's title
133 * {@code "$title ... $result"}.
134 */
135 public void endTask(String result) {
136 if (shown) {
137 write('\r' + msg + "... " + result + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
138 }
139 }
140
141 private void write(String s) {
142 if (write) {
143 try {
144 out.write(s.getBytes(UTF_8));
145 out.flush();
146 } catch (IOException e) {
147 write = false;
148 }
149 }
150 }
151 }