SideBandProgressMonitor.java

  1. /*
  2.  * Copyright (C) 2008-2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.transport;

  11. import java.io.IOException;
  12. import java.io.OutputStream;

  13. import org.eclipse.jgit.lib.BatchingProgressMonitor;
  14. import org.eclipse.jgit.lib.Constants;

  15. /** Write progress messages out to the sideband channel. */
  16. class SideBandProgressMonitor extends BatchingProgressMonitor {
  17.     private final OutputStream out;

  18.     private boolean write;

  19.     SideBandProgressMonitor(OutputStream os) {
  20.         out = os;
  21.         write = true;
  22.     }

  23.     /** {@inheritDoc} */
  24.     @Override
  25.     protected void onUpdate(String taskName, int workCurr) {
  26.         StringBuilder s = new StringBuilder();
  27.         format(s, taskName, workCurr);
  28.         s.append("   \r"); //$NON-NLS-1$
  29.         send(s);
  30.     }

  31.     /** {@inheritDoc} */
  32.     @Override
  33.     protected void onEndTask(String taskName, int workCurr) {
  34.         StringBuilder s = new StringBuilder();
  35.         format(s, taskName, workCurr);
  36.         s.append(", done\n"); //$NON-NLS-1$
  37.         send(s);
  38.     }

  39.     private void format(StringBuilder s, String taskName, int workCurr) {
  40.         s.append(taskName);
  41.         s.append(": "); //$NON-NLS-1$
  42.         s.append(workCurr);
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt) {
  47.         StringBuilder s = new StringBuilder();
  48.         format(s, taskName, cmp, totalWork, pcnt);
  49.         s.append("   \r"); //$NON-NLS-1$
  50.         send(s);
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     protected void onEndTask(String taskName, int cmp, int totalWork, int pcnt) {
  55.         StringBuilder s = new StringBuilder();
  56.         format(s, taskName, cmp, totalWork, pcnt);
  57.         s.append("\n"); //$NON-NLS-1$
  58.         send(s);
  59.     }

  60.     private void format(StringBuilder s, String taskName, int cmp,
  61.             int totalWork, int pcnt) {
  62.         s.append(taskName);
  63.         s.append(": "); //$NON-NLS-1$
  64.         if (pcnt < 100)
  65.             s.append(' ');
  66.         if (pcnt < 10)
  67.             s.append(' ');
  68.         s.append(pcnt);
  69.         s.append("% ("); //$NON-NLS-1$
  70.         s.append(cmp);
  71.         s.append("/"); //$NON-NLS-1$
  72.         s.append(totalWork);
  73.         s.append(")"); //$NON-NLS-1$
  74.     }

  75.     private void send(StringBuilder s) {
  76.         if (write) {
  77.             try {
  78.                 out.write(Constants.encode(s.toString()));
  79.                 out.flush();
  80.             } catch (IOException err) {
  81.                 write = false;
  82.             }
  83.         }
  84.     }
  85. }