-
Notifications
You must be signed in to change notification settings - Fork 7
/
StyledText.scala
46 lines (39 loc) · 1.25 KB
/
StyledText.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.scalapenos.sbt.prompt
/**
* A StyledText instance represents the combination of some
* String of text and a Style that can be rendered together.
*/
sealed trait StyledText {
def text: String
def style: Style
def isEmpty: Boolean
/**
* Creates a new instance of StyledText withe same Style as
* the original and the text tranformed using the specified
* function.
*/
def mapText(f: String ⇒ String): StyledText
lazy val rendered = style.render(text)
override def toString = rendered
}
object StyledText {
def apply(text: String, style: Style = Styles.NoStyle): StyledText = {
if (text.isEmpty) Empty else NonEmpty(text, style)
}
case class NonEmpty(text: String, style: Style = Styles.NoStyle) extends StyledText {
val isEmpty = false
def mapText(f: String ⇒ String) = copy(text = f(text))
}
/**
* The Empty StyledText has no Style and no text, so mapText will
* not work on it. Handy for when you only want to add
* prefixes/suffixes when the text is actually non-empty, like for
* instance with the current Git branch name.
*/
case object Empty extends StyledText {
val text = ""
val style = Styles.NoStyle
val isEmpty = true
def mapText(f: String ⇒ String) = this
}
}