Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed May 23, 2019
1 parent 7df28ee commit 9706bf0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ object BloopInstall {
private class ProcessHandler() extends NuAbstractProcessHandler {
var response: Option[CompletableFuture[_]] = None
val completeProcess = Promise[BloopInstallResult]()
val stdout = new ProcessOutput(line => scribe.info(line))
val stderr = new ProcessOutput(line => scribe.error(line))
val stdout = new LineListener(line => scribe.info(line))
val stderr = new LineListener(line => scribe.error(line))

override def onStart(nuProcess: NuProcess): Unit = {
nuProcess.closeStdin(false)
}

override def onExit(statusCode: Int): Unit = {
stdout.onProcessExit()
stderr.onProcessExit()
stdout.flush()
stderr.flush()
if (!completeProcess.isCompleted) {
if (statusCode == 0) {
completeProcess.trySuccess(BloopInstallResult.Installed)
Expand All @@ -235,13 +235,13 @@ object BloopInstall {

override def onStdout(buffer: ByteBuffer, closed: Boolean): Unit = {
if (!closed) {
stdout.onByteOutput(buffer)
stdout.appendBytes(buffer)
}
}

override def onStderr(buffer: ByteBuffer, closed: Boolean): Unit = {
if (!closed) {
stderr.onByteOutput(buffer)
stderr.appendBytes(buffer)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import java.nio.charset.StandardCharsets
import fansi.ErrorMode

/**
* Handles streaming console output from a system process with potential ANSI codes.
* Converts a stream of strings (with potential ANSI codes and newlines) into a callback for indvidual lines
* where each individual line has no newline \n characters.
*
* @param onLine The callback handler when the process has printed a single line.
* Guaranteed to have no newline \n characters.
* @param onLine The callback handler when a single line has been consumed from the input.
*/
class ProcessOutput(onLine: String => Unit) {
class LineListener(onLine: String => Unit) {
private var buffer = new StringBuilder()

/** The process has exited, clears out buffered output. */
def onProcessExit(): Unit = {
/** Clear buffered output. */
def flush(): Unit = {
if (buffer.length() > 0) {
onLine(buffer.toString())
reset()
}
}

def onByteOutput(bytes: ByteBuffer): this.type = {
onPlainOutput(toPlainString(bytes))
def appendBytes(bytes: ByteBuffer): this.type = {
appendPlainString(toPlainString(bytes))
}

def onStringOutput(text: String): this.type = {
onPlainOutput(toPlainString(text))
def appendString(text: String): this.type = {
appendPlainString(toPlainString(text))
}

private def onPlainOutput(text: String): this.type = {
private def appendPlainString(text: String): this.type = {
def loop(start: Int): Unit = {
val newline = text.indexOf('\n', start)
if (newline < 0) {
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/src/test/scala/tests/LineListenerSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tests

import scala.meta.internal.metals.LineListener
import scala.collection.mutable

object LineListenerSuite extends BaseSuite {
def check(
name: String,
act: LineListener => Unit,
expected: List[String]
): Unit = {
test(name) {
var buf = mutable.ListBuffer.empty[String]
val output = new LineListener(line => buf += line)
act(output)
output.flush()
assertEquals(buf.toList, expected)
}
}

check(
"flush", { out =>
out.appendString("a")
out.appendString("b")
},
List("ab")
)

check(
"loop", { out =>
out.appendString("a\nb"),
},
List("a", "b")
)

check(
"eol", { out =>
out.appendString("a\n")
},
List("a")
)

check(
"eol2", { out =>
out.appendString("a\n\n")
},
List("a", "")
)

check(
"ansi", { out =>
out.appendString(fansi.Color.Blue("blue").toString()),
},
List("blue")
)

check(
"windows", { out =>
out.appendString("a\r\nb")
},
List("a", "b")
)
}
57 changes: 0 additions & 57 deletions tests/unit/src/test/scala/tests/ProcessOutputSuite.scala

This file was deleted.

0 comments on commit 9706bf0

Please sign in to comment.