• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.io.PrintWriter
20 
21 enum class TerminalColor(val value: Int) {
22     BLACK(0),
23     RED(1),
24     GREEN(2),
25     YELLOW(3),
26     BLUE(4),
27     MAGENTA(5),
28     CYAN(6),
29     WHITE(7)
30 }
31 
terminalAttributesnull32 fun terminalAttributes(
33     bold: Boolean = false,
34     underline: Boolean = false,
35     reverse: Boolean = false,
36     foreground: TerminalColor? = null,
37     background: TerminalColor? = null
38 ): String {
39     val sb = StringBuilder()
40     sb.append("\u001B[")
41     if (foreground != null) {
42         sb.append('3').append('0' + foreground.value)
43     }
44     if (background != null) {
45         if (sb.last().isDigit())
46             sb.append(';')
47         sb.append('4').append('0' + background.value)
48     }
49 
50     if (bold) {
51         if (sb.last().isDigit())
52             sb.append(';')
53         sb.append('1')
54     }
55     if (underline) {
56         if (sb.last().isDigit())
57             sb.append(';')
58         sb.append('4')
59     }
60     if (reverse) {
61         if (sb.last().isDigit())
62             sb.append(';')
63         sb.append('7')
64     }
65     if (sb.last() == '[') {
66         // Nothing: Reset
67         sb.append('0')
68     }
69     sb.append("m")
70     return sb.toString()
71 }
72 
resetTerminalnull73 fun resetTerminal(): String {
74     return "\u001b[0m"
75 }
76 
colorizednull77 fun colorized(string: String, color: TerminalColor): String {
78     return "${terminalAttributes(foreground = color)}$string${resetTerminal()}"
79 }
80 
boldnull81 fun bold(string: String): String {
82     return "${terminalAttributes(bold = true)}$string${resetTerminal()}"
83 }
84 
terminalPrintnull85 fun PrintWriter.terminalPrint(
86     string: String,
87     bold: Boolean = false,
88     underline: Boolean = false,
89     reverse: Boolean = false,
90     foreground: TerminalColor? = null,
91     background: TerminalColor? = null
92 ) {
93     print(
94         terminalAttributes(
95             bold = bold, underline = underline, reverse = reverse, foreground = foreground,
96             background = background
97         )
98     )
99     print(string)
100     print(resetTerminal())
101 }
102