Allow custom show for a scope

I am writing a plugin that uses mangled task keys that are very hard to
read. It is helpful to be able to override the show config for these
scopes so that they look reasonable in supershell and in error
reporting.
This commit is contained in:
Ethan Atkins 2019-08-17 13:08:12 -07:00
parent 4030a6434d
commit 8fbf1907ce
2 changed files with 26 additions and 20 deletions

View File

@ -54,12 +54,14 @@ object Def extends Init[Scope] with TaskMacroExtra {
keyNameColor: Option[String] = None, keyNameColor: Option[String] = None,
): Show[ScopedKey[_]] = ): Show[ScopedKey[_]] =
Show[ScopedKey[_]]( Show[ScopedKey[_]](
key => key => {
Scope.display( val color: String => String = withColor(_, keyNameColor)
key.scope, key.scope.extra.toOption
withColor(key.key.label, keyNameColor), .flatMap(_.get(Scope.customShowString).map(color))
ref => displayRelative2(current, ref) .getOrElse {
) Scope.display(key.scope, color(key.key.label), ref => displayRelative2(current, ref))
}
}
) )
private[sbt] def showShortKey( private[sbt] def showShortKey(

View File

@ -178,6 +178,8 @@ object Scope {
): String = ): String =
displayMasked(scope, sep, showProject, mask, false) displayMasked(scope, sep, showProject, mask, false)
val customShowString = AttributeKey[String]("scope-custom-show-string")
/** /**
* unified slash style introduced in sbt 1.1.0. * unified slash style introduced in sbt 1.1.0.
* By default, sbt will no longer display the Zero-config, * By default, sbt will no longer display the Zero-config,
@ -193,6 +195,7 @@ object Scope {
showZeroConfig: Boolean showZeroConfig: Boolean
): String = { ): String = {
import scope.{ project, config, task, extra } import scope.{ project, config, task, extra }
extra.toOption.flatMap(_.get(customShowString)).getOrElse {
val zeroConfig = if (showZeroConfig) "Zero /" else "" val zeroConfig = if (showZeroConfig) "Zero /" else ""
val configPrefix = config.foldStrict(display, zeroConfig, "./") val configPrefix = config.foldStrict(display, zeroConfig, "./")
val taskPrefix = task.foldStrict(_.label + " /", "", "./") val taskPrefix = task.foldStrict(_.label + " /", "", "./")
@ -208,6 +211,7 @@ object Scope {
postfix postfix
) )
} }
}
private[sbt] def appendSpace(s: String): String = private[sbt] def appendSpace(s: String): String =
if (s == "") "" if (s == "") ""