<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>High Fibre Programming &#187; cursor</title>
	<atom:link href="http://www.4pmp.com/tag/cursor/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.4pmp.com</link>
	<description>PHP, MySQL, C, Java, Linux and other great after dinner speech topics</description>
	<lastBuildDate>Tue, 17 Jan 2012 09:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Spinning command line cursor in Java</title>
		<link>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/</link>
		<comments>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 12:48:31 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[ctrl+c]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[indicator]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[multithreaded]]></category>
		<category><![CDATA[spin]]></category>
		<category><![CDATA[spinning]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=238</guid>
		<description><![CDATA[As a follow-up to my previous article on displaying a spinning cursor to display program activity, I have created a more useful example that could be adapted for use. The original problem was this &#8211; I was writing a program that performed a particular task and I needed some way of showing the program was [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow-up to my<a href="http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/"> previous article</a> on displaying a spinning cursor to display program activity, I have created a more useful example that could be adapted for use.</p>
<p>The original problem was this &#8211; I was writing a program that performed a particular task and I needed some way of showing the program was alive and functioning correctly whilst doing the task.</p>
<p>In Windows this is done by changing the mouse cursor into an hourglass, unfortunately on the command line we don&#8217;t have such GUI luxuries.   One classic option is to create a spinning line by displaying | / &#8211; \ and so on.</p>
<p>The example below creates two child threads; one to do a particular job (in this case slowly count to 10) and another thread to display a spinning cursor.</p>
<pre class="brush: java;">
/**
 * Spinning cursor test class
 *
 * @author Nick Giles
 */
public class SpinTest
{
    /**
     * Thread which does the stuff we're interested in
     *
     */
    private Thread stuff;

    /**
     * Thread class which does stuff in the background
     *
     */
    private class Stuff implements Runnable
    {
        public void run()
        {
            int result = 0;

            try
            {
                // Slowly count to 10, although you can of course put whatever
                // you want here, presumably something a bit more useful
                while (result &lt; 10)
                {
                    result++;
                    Thread.sleep(1000);
                }
            }
            catch (InterruptedException e)
            {
                // If interrupted then quietly end the thread, you may well
                // want to handle this in some way
            }
        }
    }

    /**
     * Spinner thread
     *
     */
    private class Spinner implements Runnable
    {
        public void run()
        {
            String[] phases = {&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;};

            try
            {
                while (true)
                {
                    for (String phase : phases)
                    {
                        System.out.printf(&quot;\b&quot;+phase);

                        Thread.sleep(100);
                    }
                }
            }
            catch (InterruptedException e)
            {
                // No need to do anything if interrupted
            }
        }
    }

    /**
     * Handles all shutdown functions
     *
     */
    public class ShutdownHandler extends Thread
    {
        public void run()
        {
            // On interrupt, stop doing stuff
            stuff.interrupt();
        }
    }

    public void doStuff()
    {
        try
        {
            // Attach an object to handle shutdown signals (i.e. ctrl+c)
            Runtime.getRuntime().addShutdownHook(new ShutdownHandler()); 

            // Create a new thread for spinning the cursor
            Thread spinner = new Thread(new Spinner());

            // Create a new thread for doing stuff
            this.stuff = new Thread(new Stuff());

            // Nice message to user
            System.out.printf(&quot;Doing stuff...  &quot;);

            // Start doing stuff
            this.stuff.start();

            // Start spinning the cursor
            spinner.start();

            // Check the thread doing stuff is still doing stuff
            while (stuff.isAlive())
            {
                stuff.join(1000);
            }

            System.out.printf(&quot;\bDone.\n&quot;);

            // The thread doing stuff has finished, so stop spinning
            spinner.interrupt();

            // Wait for the spinning thread to terminate
            spinner.join();

            System.out.println(&quot;The End.&quot;);
        }
        catch (InterruptedException e)
        {
            System.out.println(&quot;Interrupted&quot;);
        }
    }

    /**
     * Main method
     *
     */
    public static void main(String[] args)
    {
        SpinTest test = new SpinTest();

        test.doStuff();
    }
}
</pre>
<p>It&#8217;s a fairly basic implementation but it should be fairly simple to extend it for a real-world application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spinning command line cursor in Java and PHP</title>
		<link>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/</link>
		<comments>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 09:46:03 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[spin]]></category>
		<category><![CDATA[spinning]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=231</guid>
		<description><![CDATA[Update: I have created an updated article which describes a multithreaded approach in Java Spinning command line cursor in Java I think I must be really bored this morning, I can&#8217;t believe I&#8217;m actually blogging this, but it might be useful for someone, who knows.   Anyway, I am currently writing a program that sits and [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update:</strong>   I have created an updated article which describes a multithreaded approach in Java <a href="http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/">Spinning command line cursor in Java</a></em></p>
<p>I think I must be really bored this morning, I can&#8217;t believe I&#8217;m actually blogging this, but it might be useful for someone, who knows.   Anyway, I am currently writing a program that sits and does stuff for a very long time, and I need a way to nicely indicate the program is still running and doing its stuff.   So I have created a little method that prints a spinning cursor on the command line.   The implementation would of course have to be multithreaded; one thread to do stuff, the other to spin the cursor &#8211; if there&#8217;s time I&#8217;ll update it to a more complete solution, but for now here&#8217;s the spin implementation:</p>
<pre class="brush: java;">

public class Spin
{
    public static void main(String[] args) throws InterruptedException
    {
        String[] phases = {&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;};

        System.out.printf(&quot;Spinning... |&quot;);

        while (true)
        {
            for (String phase : phases)
            {
                System.out.printf(&quot;\b&quot;+phase);
                Thread.sleep(100);
            }
        }
    }
}
</pre>
<p>And here it is in PHP.</p>
<pre class="brush: php;">
&lt;?php

    $phases = array(&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;);

    printf(&quot;Spinning... |&quot;);

    while (1)
    {
        foreach ($phases AS $phase)
        {
            printf('%s%s', chr(8), $phase);
            usleep(100000); // Replace this with one iteration of doing stuff
        }
    }
</pre>
<p>Of course PHP doesn&#8217;t support threading so you&#8217;d have to call each iteration of the &#8220;doing stuff&#8221; loop inside the spin loop which would mean you&#8217;d get a bit of a jumpy spinning cursor but I think most people could live with that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

