/*
 * [TestImAlive.java]
 *
 * Summary: Methods to display on the console the fact your app is still alive and busy working away.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import static java.lang.System.*;

/**
 * Methods to display on the console the fact your app is still alive and busy working away.
 * <p/>
 * These are classics from the days of the Apple][ and DOS.
 * These won't work on eclipse which displays \b as a box instead of backspace.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestImAlive
    {
    /**
     * chars to cycle through in the bubble display
     */
    private static final char bubbles[] = { '.', 'o', 'O', 'o' };

    /**
     * chars to cycle through in the spinner display
     */
    private static final char spins[] = { '-', '\\', '|', '/' };

    /**
     * where we are in the bubble cycle 0..4
     */
    private static int bubbleCycle = 0;

    /**
     * where we are in the spinner cycle 0..4
     */
    private static int spinCycle = 0;

    /**
     * display an animated bubble on the console that kicks over each elapsedTime spinner is called.
     */
    private static void bubbler()
        {
        // display a varying char over top of the previous one.
        // If \b does not work, try \r
        out.print( bubbles[ bubbleCycle ] + "\b" );
        // use "\r" instead of '\r' to force + to mean concatenation
        bubbleCycle = ( bubbleCycle + 1 ) & 3;
        // cycles 0, 1, 2, 3, 0, 1...
        }

    /**
     * display an animated spinner on the console that kicks over each elapsedTime spinner is called.
     */
    private static void spinner()
        {
        // display a varying char over top of the previous one.
        out.print( spins[ spinCycle ] + "\b" );
        spinCycle = ( spinCycle + 1 ) & 3;
        // cycles 0, 1, 2, 3, 0, 1 ...
        }

    /**
     * Test harness for bubbler and spinner
     *
     * @param args not used
     */
    @SuppressWarnings( { "EmptyCatchBlock" } )
    public static void main( String[] args )
        {
        for ( int i = 0; i < 20; i++ )
            {
            try
                {
                // simulate the program doing useful work.
                Thread.sleep( 250 );// sleep .25 a second
                }
            catch ( InterruptedException e )
                {
                }
            bubbler();
            } // end for
        for ( int i = 0; i < 20; i++ )
            {
            try
                {
                Thread.sleep( 250 );// sleep .25 a second
                }
            catch ( InterruptedException e )
                {
                }
            spinner();
            } // end for
        }
    }