Skip to content

Commit

Permalink
improvement: Strip ANSI coloring from BazelBsp logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Jul 18, 2023
1 parent 9d9e769 commit 716ae39
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,22 @@ final class ForwardingMetalsBuildClient(
def onBuildShowMessage(params: l.MessageParams): Unit =
languageClient.showMessage(params)

def onBuildLogMessage(params: l.MessageParams): Unit =
params.getType match {
case l.MessageType.Error =>
scribe.error(params.getMessage)
case l.MessageType.Warning =>
scribe.warn(params.getMessage)
case l.MessageType.Info =>
scribe.info(params.getMessage)
case l.MessageType.Log =>
scribe.info(params.getMessage)
def onBuildLogMessage(params: l.MessageParams): Unit = {
// NOTE: BazelBsp adds coloring to the log message after `workspaceBuildTargets` request
val noANSICodes = filterANSICodes(params.getMessage).trim()
if (noANSICodes.nonEmpty) {
params.getType match {
case l.MessageType.Error =>
scribe.error(noANSICodes)
case l.MessageType.Warning =>
scribe.warn(noANSICodes)
case l.MessageType.Info =>
scribe.info(noANSICodes)
case l.MessageType.Log =>
scribe.info(noANSICodes)
}
}
}

def onBuildPublishDiagnostics(params: b.PublishDiagnosticsParams): Unit = {
diagnostics.onBuildPublishDiagnostics(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,4 +1082,7 @@ object MetalsEnrichments
def filerANSIColorCodes(str: String): String =
str.replaceAll("\u001B\\[[;\\d]*m", "")

def filterANSICodes(str: String): String =
str.replaceAll("\u001b\\[1A\u001b\\[K|\u001B\\[[;\\d]*m", "")

}
4 changes: 3 additions & 1 deletion tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class BazelLspSuite
test("basic") {
cleanWorkspace()
for {
_ <- initialize(BazelBuildLayout(workspaceLayout, scalaVersion, bazelVersion))
_ <- initialize(
BazelBuildLayout(workspaceLayout, scalaVersion, bazelVersion)
)
_ = assertNoDiff(
client.workspaceMessageRequests,
List(
Expand Down

0 comments on commit 716ae39

Please sign in to comment.