-
Notifications
You must be signed in to change notification settings - Fork 39
/
RichBrowserOutputFormat.scala
59 lines (54 loc) · 2.04 KB
/
RichBrowserOutputFormat.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
47
48
49
50
51
52
53
54
55
56
57
58
59
package scribe.output.format
import scribe.output._
import scribe.writer.BrowserConsoleWriter
/**
* Supports rich output to JavaScript console in the browser
*/
object RichBrowserOutputFormat extends OutputFormat {
import BrowserConsoleWriter.args
override def apply(output: LogOutput, stream: String => Unit): Unit = recurse(output, stream)
private def recurse(output: LogOutput, stream: String => Unit): Unit = {
def withArg(key: String, value: String, output: LogOutput): Unit = {
stream("%c")
args.around(key -> value) {
recurse(output, stream)
}
stream("%c")
}
output match {
case o: TextOutput => stream(o.plainText)
case o: CompositeOutput => o.entries.foreach(recurse(_, stream))
case o: ColoredOutput => withArg("color", color2CSS(o.color), o.output)
case o: BackgroundColoredOutput => withArg("background-color", color2CSS(o.color), o.output)
case o: URLOutput =>
stream("%o (")
args.around("::URL" -> o.url) {
recurse(o.output, stream)
}
stream(")")
case o: BoldOutput => withArg("font-weight", "bold", o.output)
case o: ItalicOutput => withArg("font-style", "italic", o.output)
case o: UnderlineOutput => withArg("text-decoration", "underline", o.output)
case o: StrikethroughOutput => withArg("text-decoration", "line-through", o.output)
case _ => stream(output.plainText)
}
}
private def color2CSS(color: Color): String = color match {
case Color.Black => "black"
case Color.Blue => "blue"
case Color.Cyan => "cyan"
case Color.Green => "green"
case Color.Magenta => "magenta"
case Color.Red => "red"
case Color.White => "white"
case Color.Yellow => "yellow"
case Color.Gray => "gray"
case Color.BrightBlue => "lightblue"
case Color.BrightCyan => "lightcyan"
case Color.BrightGreen => "lime"
case Color.BrightMagenta => "violet"
case Color.BrightRed => "crimson"
case Color.BrightWhite => "white"
case Color.BrightYellow => "lightyellow"
}
}