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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. package org.eclipse.jgit.lib;
  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import java.io.IOException;
  14. import java.io.OutputStreamWriter;
  15. import java.io.PrintWriter;
  16. import java.io.Writer;
  17. /**
  18. * A simple progress reporter printing on a stream.
  19. */
  20. public class TextProgressMonitor extends BatchingProgressMonitor {
  21. private final Writer out;
  22. private boolean write;
  23. /**
  24. * Initialize a new progress monitor.
  25. */
  26. public TextProgressMonitor() {
  27. this(new PrintWriter(new OutputStreamWriter(System.err, UTF_8)));
  28. }
  29. /**
  30. * Initialize a new progress monitor.
  31. *
  32. * @param out
  33. * the stream to receive messages on.
  34. */
  35. public TextProgressMonitor(Writer out) {
  36. this.out = out;
  37. this.write = true;
  38. }
  39. /** {@inheritDoc} */
  40. @Override
  41. protected void onUpdate(String taskName, int workCurr) {
  42. StringBuilder s = new StringBuilder();
  43. format(s, taskName, workCurr);
  44. send(s);
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. protected void onEndTask(String taskName, int workCurr) {
  49. StringBuilder s = new StringBuilder();
  50. format(s, taskName, workCurr);
  51. s.append("\n"); //$NON-NLS-1$
  52. send(s);
  53. }
  54. private void format(StringBuilder s, String taskName, int workCurr) {
  55. s.append("\r"); //$NON-NLS-1$
  56. s.append(taskName);
  57. s.append(": "); //$NON-NLS-1$
  58. while (s.length() < 25)
  59. s.append(' ');
  60. s.append(workCurr);
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
  65. StringBuilder s = new StringBuilder();
  66. format(s, taskName, cmp, totalWork, pcnt);
  67. send(s);
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
  72. StringBuilder s = new StringBuilder();
  73. format(s, taskName, cmp, totalWork, pcnt);
  74. s.append("\n"); //$NON-NLS-1$
  75. send(s);
  76. }
  77. private void format(StringBuilder s, String taskName, int cmp,
  78. int totalWork, int pcnt) {
  79. s.append("\r"); //$NON-NLS-1$
  80. s.append(taskName);
  81. s.append(": "); //$NON-NLS-1$
  82. while (s.length() < 25)
  83. s.append(' ');
  84. String endStr = String.valueOf(totalWork);
  85. String curStr = String.valueOf(cmp);
  86. while (curStr.length() < endStr.length())
  87. curStr = " " + curStr; //$NON-NLS-1$
  88. if (pcnt < 100)
  89. s.append(' ');
  90. if (pcnt < 10)
  91. s.append(' ');
  92. s.append(pcnt);
  93. s.append("% ("); //$NON-NLS-1$
  94. s.append(curStr);
  95. s.append("/"); //$NON-NLS-1$
  96. s.append(endStr);
  97. s.append(")"); //$NON-NLS-1$
  98. }
  99. private void send(StringBuilder s) {
  100. if (write) {
  101. try {
  102. out.write(s.toString());
  103. out.flush();
  104. } catch (IOException err) {
  105. write = false;
  106. }
  107. }
  108. }
  109. }