diff --git a/core/src/main/scala/replpp/Operators.scala b/core/src/main/scala/replpp/Operators.scala index 6f2839c..8a6db51 100644 --- a/core/src/main/scala/replpp/Operators.scala +++ b/core/src/main/scala/replpp/Operators.scala @@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets import java.nio.file.{Path, Paths} import scala.jdk.CollectionConverters.* import scala.util.{Try, Using} +import scala.util.control.NonFatal /** * Operators to redirect output to files or pipe them into external commands / processes, @@ -22,6 +23,13 @@ import scala.util.{Try, Using} * */ object Operators { + def main(args: Array[String]): Unit = { + given Colors = Colors.BlackWhite + val value = "foo" + val result = value #| "cat" + println(result) + } + /** output from an external command, e.g. when using `#|` */ case class ProcessResults(stdout: String, stderr: String) @@ -135,15 +143,23 @@ object Operators { val chunkSize = 8192 var remaining = bytes.length var pos = 0 - while (remaining > 0) { - val currentWindow = math.min(remaining, chunkSize) - stdin.buffered.write(value, pos, currentWindow) - pos += currentWindow - remaining -= currentWindow + var stopped = false + Using.resource(stdin) { stdin => + while (remaining > 0 && !stopped) { + val currentWindow = math.min(remaining, chunkSize) + try { + stdin.buffered.write(value, pos, currentWindow) + pos += currentWindow + remaining -= currentWindow + } catch { + case t: Throwable => + // most likely the user exited the subprocess + stopped = true + } + } + // flush stdin, but ignore errors + try {stdin.flush()} catch { case NonFatal(_) => /*ignore*/ } } - - stdin.flush() - stdin.close() } } }