You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I didn't find a way to read from console (stdin of the current process) using os-lib.
I expected to find such a feature in a lib designed to handle OS interfacing (something like os.read.lines.stream(os.Stdin)).
In the Scala API doc, I only found how to manage standard input of subprocesses, not how to access the standard input of the current process and read from it.
Can you help me? Thanks!
The text was updated successfully, but these errors were encountered:
I'm not a maintainer of this library but I stumbled upon your question.
Reading from stdin can be done with just basic Scala, in particular the methods on scala.io.Stdin such as Stdin.readLine().
Now your desire to have a "stream" of input is rather interesting actually. Here are a few options I can think of but I'm not sure any of them are optimal:
while loop with mutable var
LazyList
3rd party libraries like ZStreams
A while loop (not very "functional programming" but easily understood by most programmers):
var in: String = ""
while ({
in = StdIn.readLine()
in != "foo" // terminate loop if user enters "foo"
}) {
// ...do whatever you want to do with `in`....
}
LazyList (standard Scala's concept of a lazy "stream") has various factory methods in its companion object. This is one of many ways you could skin this cat:
val inputStream: LazyList[String] = LazyList.continually(StdIn.readLine())
inputStream
.takeWhile(_ != "foo") // end when user enters "foo"
.map { line =>
println(s"You entered: $line")
line
}
.toList // You need this! Otherwise the LazyList will never start getting evaluated
I like ZIO ZStreams, but that's somewhat out of the scope of this question.
Hello,
I didn't find a way to read from console (stdin of the current process) using os-lib.
I expected to find such a feature in a lib designed to handle OS interfacing (something like
os.read.lines.stream(os.Stdin)
).In the Scala API doc, I only found how to manage standard input of subprocesses, not how to access the standard input of the current process and read from it.
Can you help me? Thanks!
The text was updated successfully, but these errors were encountered: