/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2014 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package net.imagej.updater.util;

/**
 * A default implementation of the {@link Progress} interface.
 * 
 * @author Johannes Schindelin
 */
public class StderrProgress implements Progress {

	protected final static boolean redirected = System.console() == null;
	protected final static String end = redirected ? "\n" : "\033[K\r";
	protected String label;
	protected Object item;
	protected long lastShown, minShowDelay = 500;
	protected int lineWidth = -1;

	public StderrProgress() {}

	public StderrProgress(final int lineWidth) {
		this.lineWidth = lineWidth;
	}

	protected void print(String label, final String rest) {
		if (lineWidth < 0) System.err.print(label + " " + rest + end);
		else {
			if (label.length() >= lineWidth - 3) label =
				label.substring(0, lineWidth - 3) + "...";
			else {
				final int diff = label.length() + 1 + rest.length() - lineWidth;
				if (diff < 0) label += " " + rest;
				else label +=
					(" " + rest).substring(0, rest.length() - diff - 3) + "...";
			}
			System.err.print(label + end);
		}
	}

	protected boolean skipShow() {
		final long now = System.currentTimeMillis();
		if (now - lastShown < minShowDelay) return true;
		lastShown = now;
		return false;
	}

	@Override
	public void setTitle(final String title) {
		label = title;
	}

	@Override
	public void setCount(final int count, final int total) {
		if (skipShow()) return;
		print(label, "" + count + "/" + total);
	}

	@Override
	public void addItem(final Object item) {
		this.item = item;
		print(label, "(" + item + ") ");
	}

	@Override
	public void setItemCount(final int count, final int total) {
		if (redirected || skipShow()) return;
		print(label, "(" + item + ") [" + count + "/" + total + "]");
	}

	@Override
	public void itemDone(final Object item) {
		print(item.toString(), "done");
	}

	@Override
	public void done() {
		print("Done:", label);
		System.err.println("");
	}
}

@@ -2052,16 +2052,63 @@
 dth = -1
+, dotCounter = 0;%0A%09private boolean skipProgress
 ;%0A%0A%09publ
@@ -3006,16 +3006,32 @@
 %7B%0A%09%09if (
+skipProgress %7C%7C 
 skipShow
@@ -3279,16 +3279,32 @@
 %7B%0A%09%09if (
+skipProgress %7C%7C 
 redirect
@@ -3449,24 +3449,167 @@
 t item) %7B%0A%09%09
+if (skipProgress) %7B%0A%09%09%09if ((++dotCounter %25 80) == 1 && dotCounter %3E 1) %7B%0A%09%09%09%09System.err.print(%22%5Cn%22);%0A%09%09%09%7D%0A%09%09%09System.err.print(%22.%22);%0A%09%09%7D%0A%09%09else 
 print(item.t
@@ -3665,16 +3665,60 @@
 one() %7B%0A
+%09%09if (skipProgress) System.err.print(%22%5Cn%22);%0A
 %09%09print(

add a flag to suppress showing the progress