1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava
18 
19 enum class TerminalColor(val value: Int) {
20     BLACK(0),
21     RED(1),
22     GREEN(2),
23     YELLOW(3),
24     BLUE(4),
25     MAGENTA(5),
26     CYAN(6),
27     WHITE(7)
28 }
29 
terminalAttributesnull30 fun terminalAttributes(
31     bold: Boolean = false,
32     underline: Boolean = false,
33     reverse: Boolean = false,
34     foreground: TerminalColor? = null,
35     background: TerminalColor? = null
36 ): String {
37     val sb = StringBuilder()
38     sb.append("\u001B[")
39     if (foreground != null) {
40         sb.append('3').append('0' + foreground.value)
41     }
42     if (background != null) {
43         if (sb.last().isDigit())
44             sb.append(';')
45         sb.append('4').append('0' + background.value)
46     }
47 
48     if (bold) {
49         if (sb.last().isDigit())
50             sb.append(';')
51         sb.append('1')
52     }
53     if (underline) {
54         if (sb.last().isDigit())
55             sb.append(';')
56         sb.append('4')
57     }
58     if (reverse) {
59         if (sb.last().isDigit())
60             sb.append(';')
61         sb.append('7')
62     }
63     if (sb.last() == '[') {
64         // Nothing: Reset
65         sb.append('0')
66     }
67     sb.append("m")
68     return sb.toString()
69 }
70 
resetTerminalnull71 fun resetTerminal(): String {
72     return "\u001b[0m"
73 }
74 
colorizednull75 fun colorized(string: String, color: TerminalColor): String {
76     return "${terminalAttributes(foreground = color)}$string${resetTerminal()}"
77 }
78 
boldnull79 fun bold(string: String): String {
80     return "${terminalAttributes(bold = true)}$string${resetTerminal()}"
81 }
82