Skip to content

Commit 70dfe44

Browse files
ref: Improve maintainability of colors.py (#598)
1 parent 47535fd commit 70dfe44

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

linodecli/baked/colors.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""
2-
Applies shell color escapes for pretty printing
2+
Applies shell color escapes for pretty printing.
33
"""
44

55
import os
66
import platform
77

8+
CLEAR_COLOR = "\x1b[0m"
9+
COLOR_CODE_MAP = {
10+
"red": "\x1b[31m",
11+
"green": "\x1b[32m",
12+
"yellow": "\x1b[33m",
13+
"black": "\x1b[30m",
14+
"white": "\x1b[40m",
15+
}
16+
17+
818
DO_COLORS = True
19+
920
# !! Windows compatibility for ANSI color codes !!
1021
#
1122
# If we're running on windows, we need to run the "color" command to enable
@@ -30,21 +41,20 @@
3041
DO_COLORS = False
3142

3243

33-
CLEAR_COLOR = "\x1b[0m"
34-
COLOR_CODE_MAP = {
35-
"red": "\x1b[31m",
36-
"green": "\x1b[32m",
37-
"yellow": "\x1b[33m",
38-
"black": "\x1b[30m",
39-
"white": "\x1b[40m",
40-
}
41-
42-
43-
def colorize_string(string, color):
44+
def colorize_string(string: str, color: str) -> str:
4445
"""
4546
Returns the requested string, wrapped in ANSI color codes to colorize it as
4647
requested. On platforms where colors are not supported, this just returns
4748
the string passed into it.
49+
50+
:param string: The content to colorize.
51+
:type string: str
52+
:param color: The color to colorize the string with.
53+
(one of red, green, yellow, black, white)
54+
:type color: str
55+
56+
:returns: The colorized string.
57+
:rtype: string
4858
"""
4959
if not DO_COLORS:
5060
return string

0 commit comments

Comments
 (0)