Skip to content

Commit f6c050e

Browse files
committed
Merge branch 'mar17-colorize'
2 parents 513232f + 438ae47 commit f6c050e

File tree

7 files changed

+1275
-0
lines changed

7 files changed

+1275
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* NSC -- new Scala compiler
2+
* Copyright 2005-2012 LAMP/EPFL
3+
* @author Paul Phillips
4+
*/
5+
6+
package scala.tools.util
7+
package color
8+
9+
import collection.mutable
10+
11+
object Ansi {
12+
final val ESC = '\u001b' // <esc>
13+
final val LBR = '\u005b' // [
14+
final val CSI = new String(Array(ESC, LBR)) // control sequence introducer
15+
final val CSI_FINAL = "m" // control sequence final byte
16+
17+
def colors = List(Black, Red, Green, Yellow, Blue, Magenta, Cyan, White)
18+
def effects = List(Reset, Bright, Faint, Italic, Underline, Blink, Inverse, Hidden, Strikethrough)
19+
20+
// No, that's not the finale of "CSI: Crime Scene Investigation."
21+
22+
def colorizerFor(codes: Seq[Int]): String => String =
23+
s => ansiCodeToString(codes) + s + ansiCodeToString(0)
24+
25+
def ansiCodeToString(code: Int): String = CSI + code + CSI_FINAL
26+
def ansiCodeToString(codes: Seq[Int]): String = codes.mkString(CSI, ";", CSI_FINAL)
27+
}
28+
29+
/** An ansi control sequence. The colorize function prepends
30+
* the control sequence to the given String and appends a
31+
* reset sequence.
32+
*/
33+
class Ansi(atoms0: List[AnsiAtom]) {
34+
val atoms = atoms0 sortBy (x => (!x.isAttr, x.isInstanceOf[AnsiBackground]))
35+
val colorize = Ansi colorizerFor codes
36+
37+
def codes = atoms map (_.code)
38+
def /(that: AnsiAtom) = new Ansi(atoms :+ that)
39+
// This looks redundant with / , but isn't - it is a way
40+
// to ensure that the argument will be a background color,
41+
// even if a foreground color is passed as an argument
42+
// (as it will be implicitly converted.)
43+
def on(that: AnsiBackground) = this / that
44+
45+
// Convenience functions.
46+
def reset = this / Reset
47+
def bright = this / Bright
48+
def faint = this / Faint
49+
def italic = this / Italic
50+
def underline = this / Underline
51+
def blink = this / Blink
52+
def inverse = this / Inverse
53+
def hidden = this / Hidden
54+
def strikethrough = this / Strikethrough
55+
56+
// adjectives first
57+
override def toString = atoms mkString " "
58+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* NSC -- new Scala compiler
2+
* Copyright 2005-2012 LAMP/EPFL
3+
* @author Paul Phillips
4+
*/
5+
6+
package scala.tools.util
7+
package color
8+
9+
case object Reset extends AnsiAttr(0)
10+
case object Bright extends AnsiAttr(1)
11+
case object Faint extends AnsiAttr(2)
12+
case object Italic extends AnsiAttr(3)
13+
case object Underline extends AnsiAttr(4)
14+
case object Blink extends AnsiAttr(5)
15+
case object Inverse extends AnsiAttr(7)
16+
case object Hidden extends AnsiAttr(8)
17+
case object Strikethrough extends AnsiAttr(9)
18+
19+
case object Black extends AnsiForeground(30)
20+
case object Red extends AnsiForeground(31)
21+
case object Green extends AnsiForeground(32)
22+
case object Yellow extends AnsiForeground(33)
23+
case object Blue extends AnsiForeground(34)
24+
case object Magenta extends AnsiForeground(35)
25+
case object Cyan extends AnsiForeground(36)
26+
case object White extends AnsiForeground(37)
27+
case object Default extends AnsiForeground(39)
28+
29+
/** One piece of an ansi control sequence. Either a color
30+
* (foreground or background) or an attribute (e.g. bright, underline.)
31+
* Control sequences are created from AnsiAtoms with the / operator.
32+
*/
33+
trait AnsiAtom {
34+
def code: Int
35+
def isAttr: Boolean
36+
}
37+
sealed abstract class AnsiAttr(val code: Int) extends AnsiAtom {
38+
final def isAttr = true
39+
}
40+
sealed abstract class AnsiColor(val code: Int) extends AnsiAtom {
41+
final def isAttr = false
42+
def flip: AnsiColor
43+
}
44+
sealed abstract class AnsiForeground(code: Int) extends AnsiColor(code) {
45+
require(30 <= code && code <= 39, code)
46+
val flip: AnsiBackground = new AnsiBackground(this)
47+
}
48+
sealed class AnsiBackground(val flip: AnsiForeground) extends AnsiColor(flip.code + 10) {
49+
require(40 <= code && code <= 49, code)
50+
override def toString = "(on " + flip + " background)"
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package scala.tools.util
2+
package color
3+
4+
/** A colorized String. It's difficult to achieve precise
5+
* formatting and selective string colorization simultaneously,
6+
* because all length-based calculations will break down in
7+
* the face of the ansi controls. It doesn't do much yet, but
8+
* this is here to eventually make that transparent.
9+
*/
10+
final class CString(val uncolorized: String, val colorized: String) {
11+
def visibleLength = uncolorized.length
12+
def colorizedLength = colorized.length
13+
def show() = Console println colorized
14+
def bytes() = colorized map (ch => ch.toByte)
15+
def > = show()
16+
17+
def append(x: CString): CString = new CString(uncolorized + x.uncolorized, colorized + x.colorized)
18+
def +(other: CString): CString = this append other
19+
override def toString = colorized
20+
}
21+
22+
class CStringOps(str: String) {
23+
/** Enables for example
24+
* println("foo" in Red)
25+
* println("foo" in Magenta.bright)
26+
*/
27+
def in(ansi: Ansi): CString = new CString(str, ansi colorize str)
28+
}

0 commit comments

Comments
 (0)