Skip to content

Instantly share code, notes, and snippets.

@kpmeen
Last active April 10, 2017 19:53
Show Gist options
  • Save kpmeen/fdf2b1c81ca8c692b303d03f5ac935c9 to your computer and use it in GitHub Desktop.
Save kpmeen/fdf2b1c81ca8c692b303d03f5ac935c9 to your computer and use it in GitHub Desktop.
My custom SBT Shell prompt
// Requires that sbt-git plugin is enabled. Make sure you have this plugin
// added to your ~/.sbt/0.13/plugins/plugins.sbt file:
// addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
shellPrompt := { state =>
object NoOpSbtLogger extends Logger {
def trace(t: => Throwable): Unit = {}
def success(message: => String): Unit = {}
def log(level: Level.Value, message: => String): Unit = {}
}
def textColor(color: Int) = { s"\033[38;5;${color}m" }
def backgroundColor(color:Int) = { s"\033[48;5;${color}m" }
def textBold = { s"\033[1m" }
def reset = { s"\033[0m" }
def formatText(str: String, bold: Boolean = false)(fg: Int, bg: Int) = {
s"${if (bold) textBold else ""}${textColor(fg)}${backgroundColor(bg)}${str}${reset}"
}
val red = 1
val green = 2
val white = 15
val black = 16
val blue = 39
val orange = 166
val time = java.time.LocalTime.now().toString.takeWhile(_ != '.')
val home = sys.env.getOrElse("HOME", default = "")
val currDir = baseDirectory.value.getAbsolutePath.replaceAll(home, "~")
import com.typesafe.sbt.SbtGit._
val prjExtract = Project.extract(state)
val dirtyGit = formatText("✘")(red, black)
val cleanGit = formatText("✔")(green, black)
val branch = prjExtract.get(GitKeys.gitCurrentBranch)
// The below lines are unsafe and will fail if the git repository hasn't seen any commits yet...
val (_, runner) = prjExtract.runTask(GitKeys.gitRunner, state)
val maybeStatus = scala.util.Try {
runner("diff-index", "HEAD", "--")(prjExtract.get(baseDirectory), NoOpSbtLogger)
}.toOption
val maybeGit = maybeStatus.map(s => !s.trim.isEmpty)
formatText("sbt", true)(white, 88) +
" [" + formatText(time)(blue, black) + "]" +
" [" + formatText(name.value)(green, black) +
maybeGit.map(_ => ":" + formatText(branch)(blue, black)).getOrElse("") + "]" +
maybeGit.map(isDirty => s"${if (isDirty) dirtyGit else cleanGit}").getOrElse("") +
" λ "
}
import com.softwaremill.clippy.ClippySbtPlugin._
clippyColorsEnabled := true
libraryDependencies += "com.lihaoyi" % "ammonite" % "0.8.2" % "test" cross CrossVersion.full
initialCommands in (Test, console) := """ammonite.Main().run()"""
addSbtPlugin("io.get-coursier" %% "sbt-coursier" % "1.0.0-RC1")
addSbtPlugin("com.softwaremill.clippy" % "plugin-sbt" % "0.5.2")
addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "0.1.4")
addSbtPlugin("org.ensime" % "sbt-ensime" % "1.12.8")
addSbtPlugin("com.gilt" % "sbt-dependency-graph-sugar" % "0.8.2")
addSbtPlugin("com.eed3si9n" % "sbt-dirty-money" % "0.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.9.2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment