Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CliOptions: turn info into a stream+writer wrapper #4271

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.scalafmt.sysops.OsSpecific

import java.io.InputStream
import java.io.PrintStream
import java.io.PrintWriter
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
Expand All @@ -35,23 +36,26 @@ object CliOptions {
* directly from main.
*/
def auto(parsed: CliOptions): CliOptions = {
val usesOut = parsed.stdIn || parsed.writeMode.usesOut
val auxOut =
if (parsed.noStdErr || !usesOut) parsed.common.out else parsed.common.err

parsed.copy(common =
parsed.common.copy(
out = guardPrintStream(parsed.quiet && !usesOut)(parsed.common.out),
info = guardPrintStream(parsed.quiet || usesOut)(auxOut),
debug = guardPrintStream(parsed.quiet)(
if (parsed.debug) auxOut else parsed.common.debug,
),
err = guardPrintStream(parsed.quiet)(parsed.common.err),
),
val info: Output.StreamOrWriter =
if (parsed.quiet) Output.NoopStream
else {
val usesOut = parsed.stdIn || parsed.writeMode.usesOut
new Output.FromStream(
if (parsed.noStdErr || !usesOut) parsed.common.out
else parsed.common.err,
)
}
val common = parsed.common.copy(
out = guardPrintStream(parsed.quiet && !parsed.stdIn)(parsed.common.out),
info = info,
debug = (if (parsed.debug) info else Output.NoopStream).printWriter,
err = guardPrintStream(parsed.quiet)(parsed.common.err),
)

parsed.copy(common = common)
}

private def guardPrintStream(p: => Boolean)(
private def guardPrintStream(p: Boolean)(
candidate: PrintStream,
): PrintStream = if (p) Output.NoopStream.printStream else candidate

Expand All @@ -62,8 +66,8 @@ case class CommonOptions(
out: PrintStream = System.out,
in: InputStream = System.in,
err: PrintStream = System.err,
debug: PrintStream = Output.NoopStream.printStream,
info: PrintStream = Output.NoopStream.printStream,
debug: PrintWriter = Output.NoopStream.printWriter,
info: Output.StreamOrWriter = Output.NoopStream,
) {
private[cli] lazy val workingDirectory: AbsoluteFile = cwd
.getOrElse(AbsoluteFile.userDir)
Expand Down
15 changes: 13 additions & 2 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/Output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import java.io._

object Output {

object NoopStream extends OutputStream {
trait StreamOrWriter {
def outputStream: OutputStream
def printStream: PrintStream
def printWriter: PrintWriter
}

object NoopStream extends OutputStream with StreamOrWriter {
self =>
override def write(b: Int): Unit = ()

Expand All @@ -15,7 +21,12 @@ object Output {
def outputStream: OutputStream = self
val printStream = new PrintStream(self)
val printWriter = new PrintWriter(self)
val streamWriter = new OutputStreamWriter(self)
}

class FromStream(val obj: PrintStream) extends StreamOrWriter {
override def outputStream: OutputStream = obj
override def printStream: PrintStream = obj
override def printWriter: PrintWriter = new PrintWriter(obj)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ class ScalafmtCliReporter(options: CliOptions) extends ScalafmtReporter {
override def parsedConfig(config: Path, scalafmtVersion: String): Unit =
options.common.debug.println(s"parsed config (v$scalafmtVersion): $config")

override def downloadWriter(): PrintWriter =
new PrintWriter(options.common.info)
override def downloadWriter(): PrintWriter = options.common.info.printWriter

override def downloadOutputStreamWriter(): OutputStreamWriter =
new OutputStreamWriter(options.common.info)
new OutputStreamWriter(options.common.info.outputStream)
}

private class FailedToFormat(filename: String, cause: Throwable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.scalafmt.Error
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.BatchPathFinder

import java.io.OutputStreamWriter
import java.nio.file.Path

trait ScalafmtRunner {
Expand All @@ -19,7 +18,7 @@ trait ScalafmtRunner {
msg: String,
): TermDisplay = {
val termDisplay = new TermDisplay(
new OutputStreamWriter(options.common.info),
options.common.info.printWriter,
fallbackMode = options.nonInteractive || TermDisplay.defaultFallbackMode,
)
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ class CliOptionsTest extends FunSuite {

test("write info to out if not writing to stdout") {
val options = Cli.getConfig(Array.empty[String], baseCliOptionsWithOut).get
assertEquals(options.common.info, System.out)
assertEquals(options.common.info.printStream, System.out)
}

Seq("--stdin", "--stdout").foreach { arg =>
test(s"don't write info when using $arg") {
val options = Cli.getConfig(Array(arg), baseCliOptionsWithOut).get
assertEquals(options.common.info, Output.NoopStream.printStream)
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")
}
}
}

Expand Down
Loading