From 1b1d57d2dcd37a6858d77f26265f5f483e747afb Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:45:44 -0700 Subject: [PATCH] CliOptions: write info to console instead of err Currently, info is written to out if out is not used for result output, and to err otherwise. Let's enhance this approach as follows: if out is not used, then we'll write into to out, as before; otherwise, before resorting to err, check if the console is available and use it then. --- .../scala/org/scalafmt/cli/CliOptions.scala | 4 +++- .../main/scala/org/scalafmt/cli/Output.scala | 20 +++++++++++++++++++ .../org/scalafmt/cli/CliOptionsTest.scala | 7 ++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala index 9945f80fb3..3fd4a0e119 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala @@ -40,7 +40,9 @@ object CliOptions { if (parsed.quiet) Output.NoopStream else { val usesOut = parsed.stdIn || parsed.writeMode.usesOut - new Output.FromStream( + val cons = if (usesOut) System.console() else null + if (cons ne null) new Output.FromWriter(cons.writer()) + else new Output.FromStream( if (parsed.noStdErr || !usesOut) parsed.common.out else parsed.common.err, ) diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/Output.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/Output.scala index 23db641492..ef2a740c2d 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/Output.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/Output.scala @@ -1,6 +1,7 @@ package org.scalafmt.cli import java.io._ +import java.nio.charset._ object Output { @@ -29,4 +30,23 @@ object Output { override def printWriter: PrintWriter = new PrintWriter(obj) } + class FromWriter(val obj: Writer, charset: Charset = StandardCharsets.UTF_8) + extends OutputStream with StreamOrWriter { + + override def write(b: Int): Unit = obj.write(b & 0xf) + override def write(b: Array[Byte]): Unit = obj.write(new String(b, charset)) + override def write(b: Array[Byte], off: Int, len: Int): Unit = obj + .write(new String(b, off, len, charset)) + + override def flush(): Unit = obj.flush() + override def close(): Unit = obj.close() + + def outputStream: OutputStream = this + override def printStream: PrintStream = new PrintStream(this) + override def printWriter: PrintWriter = obj match { + case x: PrintWriter => x + case _ => new PrintWriter(obj) + } + } + } diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala index db8400011f..a1f0423671 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliOptionsTest.scala @@ -114,7 +114,12 @@ class CliOptionsTest extends FunSuite { Seq("--stdin", "--stdout").foreach { arg => test(s"don't write info when using $arg") { val options = Cli.getConfig(Array(arg), baseCliOptionsWithOut).get - options.common.info match { + val cons = System.console() + if (cons ne null) options.common.info match { + case x: Output.FromWriter if x.obj eq cons.writer() => + case x => fail(s"info should be writing to console: $x") + } + else options.common.info match { case x: Output.FromStream if x.obj eq Output.NoopStream.printStream => case x => fail(s"info should be writing to NoopStream: $x") }