From 42a1e2a2912cf2fd51134f1786b116ae25f0ac24 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Thu, 13 Jul 2017 14:58:10 -0400 Subject: [PATCH] Improve the default prompt Fixes #3313 This changes the default prompt to include "sbt:" + current project name. See https://twitter.com/eed3si9n/status/884914670351659009 --- main-settings/src/main/scala/sbt/Def.scala | 17 ++++++++++------- main/src/main/scala/sbt/Defaults.scala | 11 ++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/main-settings/src/main/scala/sbt/Def.scala b/main-settings/src/main/scala/sbt/Def.scala index ab7fbc52f..68f78d360 100644 --- a/main-settings/src/main/scala/sbt/Def.scala +++ b/main-settings/src/main/scala/sbt/Def.scala @@ -1,7 +1,7 @@ package sbt import sbt.internal.util.Types.const -import sbt.internal.util.{ Attributed, AttributeKey, Init } +import sbt.internal.util.{ Attributed, AttributeKey, Init, ConsoleAppender } import sbt.util.Show import sbt.internal.util.complete.Parser import java.io.File @@ -36,7 +36,7 @@ object Def extends Init[Scope] with TaskMacroExtra { Show[ScopedKey[_]]( (key: ScopedKey[_]) => Scope.display(key.scope, - colored(key.key.label, keyNameColor), + withColor(key.key.label, keyNameColor), ref => displayRelative(current, multi, ref))) def showBuildRelativeKey(currentBuild: URI, @@ -45,7 +45,7 @@ object Def extends Init[Scope] with TaskMacroExtra { Show[ScopedKey[_]]( (key: ScopedKey[_]) => Scope.display(key.scope, - colored(key.key.label, keyNameColor), + withColor(key.key.label, keyNameColor), ref => displayBuildRelative(currentBuild, multi, ref))) def displayRelative(current: ProjectRef, multi: Boolean, project: Reference): String = @@ -63,13 +63,16 @@ object Def extends Init[Scope] with TaskMacroExtra { } def displayFull(scoped: ScopedKey[_]): String = displayFull(scoped, None) def displayFull(scoped: ScopedKey[_], keyNameColor: Option[String]): String = - Scope.display(scoped.scope, colored(scoped.key.label, keyNameColor)) + Scope.display(scoped.scope, withColor(scoped.key.label, keyNameColor)) def displayMasked(scoped: ScopedKey[_], mask: ScopeMask): String = Scope.displayMasked(scoped.scope, scoped.key.label, mask) - def colored(s: String, color: Option[String]): String = color match { - case Some(c) => c + s + scala.Console.RESET - case None => s + def withColor(s: String, color: Option[String]): String = { + val useColor = ConsoleAppender.formatEnabled + color match { + case Some(c) if useColor => c + s + scala.Console.RESET + case _ => s + } } override def deriveAllowed[T](s: Setting[T], allowDynamic: Boolean): Option[String] = diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index d29dd3858..31cd4d650 100755 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -1746,7 +1746,8 @@ object Classpaths { } val base = ModuleID(id.groupID, id.name, sbtVersion.value).withCrossVersion(cross) CrossVersion(scalaVersion, binVersion)(base).withCrossVersion(Disabled()) - } + }, + shellPrompt := shellPromptFromState )) val ivyBaseSettings: Seq[Setting[_]] = baseGlobalDefaults ++ sbtClassifiersTasks ++ Seq( @@ -2873,6 +2874,14 @@ object Classpaths { } } } + + def shellPromptFromState: State => String = { s: State => + val extracted = Project.extract(s) + (name in extracted.currentRef).get(extracted.structure.data) match { + case Some(name) => s"sbt:$name" + Def.withColor("> ", Option(scala.Console.CYAN)) + case _ => "> " + } + } } trait BuildExtra extends BuildCommon with DefExtra {