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.

TextProgressMonitor.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. /**
  46. * A simple progress reporter printing on stderr
  47. */
  48. public class TextProgressMonitor implements ProgressMonitor {
  49. private boolean output;
  50. private long taskBeganAt;
  51. private String msg;
  52. private int lastWorked;
  53. private int totalWork;
  54. /** Initialize a new progress monitor. */
  55. public TextProgressMonitor() {
  56. taskBeganAt = System.currentTimeMillis();
  57. }
  58. public void start(final int totalTasks) {
  59. // Ignore the number of tasks.
  60. taskBeganAt = System.currentTimeMillis();
  61. }
  62. public void beginTask(final String title, final int total) {
  63. endTask();
  64. msg = title;
  65. lastWorked = 0;
  66. totalWork = total;
  67. }
  68. public void update(final int completed) {
  69. if (msg == null)
  70. return;
  71. final int cmp = lastWorked + completed;
  72. if (!output && System.currentTimeMillis() - taskBeganAt < 500)
  73. return;
  74. if (totalWork == UNKNOWN) {
  75. display(cmp);
  76. System.err.flush();
  77. } else {
  78. if ((cmp * 100 / totalWork) != (lastWorked * 100) / totalWork) {
  79. display(cmp);
  80. System.err.flush();
  81. }
  82. }
  83. lastWorked = cmp;
  84. output = true;
  85. }
  86. private void display(final int cmp) {
  87. final StringBuilder m = new StringBuilder();
  88. m.append('\r');
  89. m.append(msg);
  90. m.append(": ");
  91. while (m.length() < 25)
  92. m.append(' ');
  93. if (totalWork == UNKNOWN) {
  94. m.append(cmp);
  95. } else {
  96. final String twstr = String.valueOf(totalWork);
  97. String cmpstr = String.valueOf(cmp);
  98. while (cmpstr.length() < twstr.length())
  99. cmpstr = " " + cmpstr;
  100. final int pcnt = (cmp * 100 / totalWork);
  101. if (pcnt < 100)
  102. m.append(' ');
  103. if (pcnt < 10)
  104. m.append(' ');
  105. m.append(pcnt);
  106. m.append("% (");
  107. m.append(cmpstr);
  108. m.append("/");
  109. m.append(twstr);
  110. m.append(")");
  111. }
  112. System.err.print(m);
  113. }
  114. public boolean isCancelled() {
  115. return false;
  116. }
  117. public void endTask() {
  118. if (output) {
  119. if (totalWork != UNKNOWN)
  120. display(totalWork);
  121. System.err.println();
  122. }
  123. output = false;
  124. msg = null;
  125. }
  126. }