This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How to do non-blocking read from console in REPL?

3 replies
bjornr
Joined: 2011-11-28,
User offline. Last seen 35 weeks 4 days ago.

Hi

I'm trying to do some simple console games for my kids using arrow
keys etc. in the scala REPL
I have found how to use jlines and readVirtualKey but I can't make it
not to hang while witing for a key to be pressed.
I would like to do something like this:

scala> :paste

def noBlockReadLoop(timeout:Int) = {
val time = System.currentTimeMillis / 1000
var stop = false
do {
val con = new tools.jline.console.ConsoleReader()
if (con.getInput.available!=0)
print(con.readVirtualKey)
else print (".")
Thread.sleep(10)
stop = (System.currentTimeMillis / 1000) > time + timeout
} while (!stop)
}

scala> noBlockReadLoop(5)

BUt it does not work on my machine (Windows 7) - it only prints .
whatever I press :-(
If I don't test for available it hangs the loop until I press a key,
and I want to avoid the hang and let the loop to continue if no key is
pressed...

Does any body knows what I am doing wrong? Or have an alternative
approach that works better in my Windows cmd window?

/Bjorn

mike.langen
Joined: 2012-01-18,
User offline. Last seen 38 weeks 2 days ago.
Re: How to do non-blocking read from console in REPL?

I haven't tried it in the REPL, but I've had good luck with

https://github.com/robey/naggati

(which is a nice little scala layer on top of Apache Mina / Java NIO)
for non-blocking IO

On Jan 18, 6:06 pm, Bjorn Regnell wrote:
> Hi
>
> I'm trying to do some simple console games for my kids using arrow
> keys etc. in the scala REPL
> I have found how to use jlines and readVirtualKey but I can't make it
> not to hang while witing for a key to be pressed.
> I would like to do something like this:
>
> scala> :paste
>
>   def noBlockReadLoop(timeout:Int) = {
>     val time = System.currentTimeMillis / 1000
>     var stop = false
>     do {
>       val con = new tools.jline.console.ConsoleReader()
>       if (con.getInput.available!=0)
>         print(con.readVirtualKey)
>       else print (".")
>       Thread.sleep(10)
>       stop = (System.currentTimeMillis / 1000) > time + timeout
>     } while (!stop)
>   }
>
> scala> noBlockReadLoop(5)
>
> BUt it does not work on my machine (Windows 7) - it only prints .
> whatever I press :-(
> If I don't test for available it hangs the loop until I press a key,
> and I want to avoid the hang and let the loop to continue if no key is
> pressed...
>
> Does any body knows what I am doing wrong? Or have an alternative
> approach that works better in my Windows cmd window?
>
> /Bjorn

bjornr
Joined: 2011-11-28,
User offline. Last seen 35 weeks 4 days ago.
Re: How to do non-blocking read from console in REPL?

Thanks for the tip, Mike! I will have at deeper look at naggati, but
from a glance it seems as if it might be a bit of an overkill for my
purposes:

I had a friend test the noBlockReadLoop above on a Linux machine and
it worked (sic!), so platform independence of Java (and/or jLine?)
does not seem to be that complete :-(
I would really like to get something similar to work on my Win7
machine.

Maybe someone running the REPL in Windows knows why the above does not
work?
Or is there some other Windows version in which it does?

/Bjorn

On Jan 19, 7:31 pm, Mike Langen wrote:
> I haven't tried it in the REPL, but I've had good luck with
>
> https://github.com/robey/naggati
>
> (which is a nice little scala layer on top of Apache Mina / Java NIO)
> for non-blocking IO
>
> On Jan 18, 6:06 pm, Bjorn Regnell wrote:
>
>
>
> > Hi
>
> > I'm trying to do some simple console games for my kids using arrow
> > keys etc. in the scala REPL
> > I have found how to use jlines and readVirtualKey but I can't make it
> > not to hang while witing for a key to be pressed.
> > I would like to do something like this:
>
> > scala> :paste
>
> >   def noBlockReadLoop(timeout:Int) = {
> >     val time = System.currentTimeMillis / 1000
> >     var stop = false
> >     do {
> >       val con = new tools.jline.console.ConsoleReader()
> >       if (con.getInput.available!=0)
> >         print(con.readVirtualKey)
> >       else print (".")
> >       Thread.sleep(10)
> >       stop = (System.currentTimeMillis / 1000) > time + timeout
> >     } while (!stop)
> >   }
>
> > scala> noBlockReadLoop(5)
>
> > BUt it does not work on my machine (Windows 7) - it only prints .
> > whatever I press :-(
> > If I don't test for available it hangs the loop until I press a key,
> > and I want to avoid the hang and let the loop to continue if no key is
> > pressed...
>
> > Does any body knows what I am doing wrong? Or have an alternative
> > approach that works better in my Windows cmd window?
>
> > /Bjorn- Hide quoted text -
>
> - Show quoted text -

bjornr
Joined: 2011-11-28,
User offline. Last seen 35 weeks 4 days ago.
Re: How to do non-blocking read from console in REPL?

I have now made some more tests and the below piece of code works well
on Linux Ubuntu and on Mac OS X 10.7.2
BUT it does not work in the scala REPL for windows 7 cmd :-(
Thus there seems to be some platformdependent problem with jline here.
Aaargh.
If you try the code below in the REPL, it should print dots for 5 secs
while you can press any key and see its int number.
Anyone knows how to get this to work on a win7 machine?

scala> :paste
def noBlockReadLoop(timeout:Int) = {
val time = System.currentTimeMillis / 1000
var stop = false
do {
val con = new tools.jline.console.ConsoleReader()
if (con.getInput.available!=0)
print(con.readVirtualKey)
else print (".")
Thread.sleep(10)
stop = (System.currentTimeMillis / 1000) > time + timeout
} while (!stop)
}

scala> noBlockReadLoop(5)
....................................................................

Any help or hints appreciated.
/Bjorn

On 22 Jan, 01:48, Bjorn Regnell wrote:
> Thanks for the tip, Mike! I will have at deeper look at naggati, but
> from a glance it seems as if it might be a bit of an overkill for my
> purposes:
>
> I had a friend test the noBlockReadLoop above on a Linux machine and
> it worked (sic!), so platform independence of Java (and/or jLine?)
> does not seem to be that complete :-(
> I would really like to get something similar to work on my Win7
> machine.
>
> Maybe someone running the REPL in Windows knows why the above does not
> work?
> Or is there some other Windows version in which it does?
>
> /Bjorn
>
> On Jan 19, 7:31 pm, Mike Langen wrote:
>
>
>
> > I haven't tried it in the REPL, but I've had good luck with
>
> >https://github.com/robey/naggati
>
> > (which is a nice little scala layer on top of Apache Mina / Java NIO)
> > fornon-blockingIO
>
> > On Jan 18, 6:06 pm, Bjorn Regnell wrote:
>
> > > Hi
>
> > > I'm trying to do some simple console games for my kids using arrow
> > > keys etc. in the scala REPL
> > > I have found how to use jlines and readVirtualKey but I can't make it
> > > not to hang while witing for a key to be pressed.
> > > I would like to do something like this:
>
> > > scala> :paste
>
> > >   def noBlockReadLoop(timeout:Int) = {
> > >     val time = System.currentTimeMillis / 1000
> > >     var stop = false
> > >     do {
> > >       val con = new tools.jline.console.ConsoleReader()
> > >       if (con.getInput.available!=0)
> > >         print(con.readVirtualKey)
> > >       else print (".")
> > >       Thread.sleep(10)
> > >       stop = (System.currentTimeMillis / 1000) > time + timeout
> > >     } while (!stop)
> > >   }
>
> > > scala> noBlockReadLoop(5)
>
> > > BUt it does not work on my machine (Windows 7) - it only prints .
> > > whatever I press :-(
> > > If I don't test for available it hangs the loop until I press a key,
> > > and I want to avoid the hang and let the loop to continue if no key is
> > > pressed...
>
> > > Does any body knows what I am doing wrong? Or have an alternative
> > > approach that works better in my Windows cmd window?
>
> > > /Bjorn- Hide quoted text -
>
> > - Show quoted text -- Dölj citerad text -
>
> - Visa citerad text -

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland