/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.gshell.artifact.monitor;

import org.apache.geronimo.gshell.io.IO;
import static org.apache.maven.wagon.WagonConstants.UNKNOWN_LENGTH;
import org.apache.maven.wagon.events.TransferEvent;
import static org.apache.maven.wagon.events.TransferEvent.REQUEST_PUT;

import java.io.IOException;

/**
 * A download monitor providing a simple spinning progress interface.
 *
 * @version $Rev$ $Date$
 */
public class ProgressSpinnerMonitor
    extends TransferListenerSupport
{
    private static final String CARRIAGE_RETURN = "\r";

    private IO io;

    private ProgressSpinner spinner = new ProgressSpinner();

    private long complete;

    public ProgressSpinnerMonitor(final IO io) throws IOException {
        assert io != null;

        this.io = io;
    }
    
    private void print(final String message) {
        if (!io.isQuiet()) {
            io.out.print(message);
            io.out.print(CARRIAGE_RETURN);
            io.out.flush();
        }
    }

    private void println(final String message) {
        if (!io.isQuiet()) {
            io.out.println(message);
            io.out.flush();
        }
    }

    public void transferInitiated(final TransferEvent event) {
        assert event != null;

        super.transferInitiated(event);

        complete = 0;

        spinner.reset();

        String message = event.getRequestType() == REQUEST_PUT ? "Uploading" : "Downloading";
        String url = event.getWagon().getRepository().getUrl();

        println(message + ": " + url + "/" + event.getResource().getName());
    }
    
    public void transferProgress(final TransferEvent event, final byte[] buffer, final int length) {
        assert event != null;

        super.transferProgress(event, buffer, length);

        long total = event.getResource().getContentLength();
        complete += length;

        String message;

        if (total >= 1024) {
            message = complete / 1024 + "/" + (total == UNKNOWN_LENGTH ? "?" : total / 1024 + "K");
        }
        else {
            message = complete + "/" + (total == UNKNOWN_LENGTH ? "?" : total + "b");
        }

        print(spinner.spin(message));
    }
}
@@ -1366,16 +1366,22 @@
 private 
+final 
 IO io;%0A%0A
@@ -1392,16 +1392,22 @@
 private 
+final 
 Progress
@@ -1960,24 +1960,126 @@
    %7D%0A    %7D%0A%0A
+    /*%0A    NOTE: Seems liked this is not always called, so use transferStarted to display the banner%0A%0A
     public v
@@ -2265,22 +2265,823 @@
  String 
-messag
+type = event.getRequestType() == REQUEST_PUT ? %22Uploading%22 : %22Downloading%22;%0A        String url = event.getWagon().getRepository().getUrl();%0A%0A        String message = type + %22: %22 + url + %22/%22 + event.getResource().getName();%0A%0A        log.debug(message);%0A%0A        println(message);%0A    %7D%0A    */%0A%0A    //%0A    // TODO: May actually want to use a little timmer thread to make this spin more often for better user feedback%0A    //       that we are still working, then use the event to update the message.  Or really, put the timer in%0A    //       the spinner impl, and then just update the status message%0A    //%0A    %0A    public void transferStarted(final TransferEvent event) %7B%0A        assert event != null;%0A%0A        super.transferStarted(event);%0A%0A        complete = 0;%0A%0A        spinner.reset();%0A%0A        String typ
 e = even
@@ -3222,24 +3222,23 @@
     
-println(
+String 
 message 
 + %22:
@@ -3233,16 +3233,23 @@
 message 
+= type 
 + %22: %22 +
@@ -3290,29 +3290,80 @@
 etName()
-);%0A    %7D
+;%0A%0A        log.debug(message);%0A%0A        println(message);
 %0A    
+%7D%0A
 %0A    pub
@@ -3905,24 +3905,61 @@
 %0A        %7D%0A%0A
+        log.debug(message);%0A        %0A
         prin

Use transferStarted to display status, since it seems like transferInitiated is not always called.