diff --git a/core/shared/src/main/scala/zio/console/package.scala b/core/shared/src/main/scala/zio/console/package.scala index b47f320f88c5..25bf98d9f500 100644 --- a/core/shared/src/main/scala/zio/console/package.scala +++ b/core/shared/src/main/scala/zio/console/package.scala @@ -16,7 +16,7 @@ package zio -import java.io.{ EOFException, IOException, PrintStream, Reader } +import java.io.{ EOFException, IOException, PrintStream } import scala.io.StdIn import scala.{ Console => SConsole } @@ -38,61 +38,28 @@ package object console { } object Service { + private def putStr(stream: PrintStream)(line: String): UIO[Unit] = + IO.effectTotal(SConsole.withOut(stream)(SConsole.print(line))) + + private def putStrLn(stream: PrintStream)(line: String): UIO[Unit] = + IO.effectTotal(SConsole.withOut(stream)(SConsole.println(line))) + val live: Service = new Service { + def putStr(line: String): UIO[Unit] = Service.putStr(SConsole.out)(line) + + def putStrErr(line: String): UIO[Unit] = Service.putStr(SConsole.err)(line) + + def putStrLnErr(line: String): UIO[Unit] = Service.putStrLn(SConsole.err)(line) + + def putStrLn(line: String): UIO[Unit] = Service.putStrLn(SConsole.out)(line) - /** - * Prints text to the console. - */ - final def putStr(line: String): UIO[Unit] = - putStr(SConsole.out)(line) - - /** - * Prints text to the standard error console. - */ - override def putStrErr(line: String): UIO[Unit] = - putStrLn(SConsole.err)(line) - - final def putStr(stream: PrintStream)(line: String): UIO[Unit] = - IO.effectTotal(SConsole.withOut(stream) { - SConsole.print(line) - }) - - /** - * Prints a line of text to the standard error console, including a newline character. - */ - override def putStrLnErr(line: String): UIO[Unit] = - putStrLn(SConsole.err)(line) - - /** - * Prints a line of text to the console, including a newline character. - */ - final def putStrLn(line: String): UIO[Unit] = - putStrLn(SConsole.out)(line) - - final def putStrLn(stream: PrintStream)(line: String): UIO[Unit] = - IO.effectTotal(SConsole.withOut(stream) { - SConsole.println(line) - }) - - /** - * Retrieves a line of input from the console. - */ - final val getStrLn: IO[IOException, String] = - getStrLn(SConsole.in) - - /** - * Retrieves a line of input from the console. - * Fails with an [[java.io.EOFException]] when the underlying [[java.io.Reader]] - * returns null. - */ - final def getStrLn(reader: Reader): IO[IOException, String] = - IO.effect(SConsole.withIn(reader) { + val getStrLn: IO[IOException, String] = + IO.effect { val line = StdIn.readLine() - if (line == null) { - throw new EOFException("There is no more input left to read") - } else line - }).refineToOrDie[IOException] + if (line ne null) line + else throw new EOFException("There is no more input left to read") + }.refineToOrDie[IOException] } } @@ -129,6 +96,8 @@ package object console { /** * Retrieves a line of input from the console. + * Fails with an [[java.io.EOFException]] when the underlying [[java.io.Reader]] + * returns null. */ val getStrLn: ZIO[Console, IOException, String] = ZIO.accessM(_.get.getStrLn)