You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProgressSpinner.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. package org.eclipse.jgit.transport;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. import java.util.concurrent.TimeUnit;
  48. /**
  49. * A simple spinner connected to an {@code OutputStream}.
  50. * <p>
  51. * This is class is not thread-safe. The update method may only be used from a
  52. * single thread. Updates are sent only as frequently as {@link #update()} is
  53. * invoked by the caller, and are capped at no more than 2 times per second by
  54. * requiring at least 500 milliseconds between updates.
  55. *
  56. * @since 4.2
  57. */
  58. public class ProgressSpinner {
  59. private static final long MIN_REFRESH_MILLIS = 500;
  60. private static final char[] STATES = new char[] { '-', '\\', '|', '/' };
  61. private final OutputStream out;
  62. private String msg;
  63. private int state;
  64. private boolean write;
  65. private boolean shown;
  66. private long nextUpdateMillis;
  67. /**
  68. * Initialize a new spinner.
  69. *
  70. * @param out
  71. * where to send output to.
  72. */
  73. public ProgressSpinner(OutputStream out) {
  74. this.out = out;
  75. this.write = true;
  76. }
  77. /**
  78. * Begin a time consuming task.
  79. *
  80. * @param title
  81. * description of the task, suitable for human viewing.
  82. * @param delay
  83. * delay to wait before displaying anything at all.
  84. * @param delayUnits
  85. * unit for {@code delay}.
  86. */
  87. public void beginTask(String title, long delay, TimeUnit delayUnits) {
  88. msg = title;
  89. state = 0;
  90. shown = false;
  91. long now = System.currentTimeMillis();
  92. if (delay > 0) {
  93. nextUpdateMillis = now + delayUnits.toMillis(delay);
  94. } else {
  95. send(now);
  96. }
  97. }
  98. /**
  99. * Update the spinner if it is showing.
  100. */
  101. public void update() {
  102. long now = System.currentTimeMillis();
  103. if (now >= nextUpdateMillis) {
  104. send(now);
  105. state = (state + 1) % STATES.length;
  106. }
  107. }
  108. private void send(long now) {
  109. StringBuilder buf = new StringBuilder(msg.length() + 16);
  110. buf.append('\r').append(msg).append("... ("); //$NON-NLS-1$
  111. buf.append(STATES[state]);
  112. buf.append(") "); //$NON-NLS-1$
  113. shown = true;
  114. write(buf.toString());
  115. nextUpdateMillis = now + MIN_REFRESH_MILLIS;
  116. }
  117. /**
  118. * Denote the current task completed.
  119. *
  120. * @param result
  121. * text to print after the task's title
  122. * {@code "$title ... $result"}.
  123. */
  124. public void endTask(String result) {
  125. if (shown) {
  126. write('\r' + msg + "... " + result + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
  127. }
  128. }
  129. private void write(String s) {
  130. if (write) {
  131. try {
  132. out.write(s.getBytes(UTF_8));
  133. out.flush();
  134. } catch (IOException e) {
  135. write = false;
  136. }
  137. }
  138. }
  139. }