Skip to content

Commit 0b1588d

Browse files
authored
Merge pull request #253 from linode/ref/no-more-colorclass
Remove dependency on colorclass
2 parents 775b5e3 + 3776ec8 commit 0b1588d

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ added to Linode's OpenAPI spec:
389389
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
390390
|x-linode-cli-action | method | The action name for operations under this path. If not present, operationId is used. |
391391
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
392-
|x-linode-cli-color   | property | If present, defines key-value pairs of property value: color. Colors must be understood |
393-
|                     |         | by colorclass.Color. Must include a default.                         |
392+
|x-linode-cli-color   | property | If present, defines key-value pairs of property value: color. Colors must be one of |
393+
|                     |         | "red", "green", "yellow", "white", and "black". Must include a default. |
394394
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+
395395
|x-linode-cli-command | path | The command name for operations under this path. If not present, "default" is used. |
396396
+-----------------------------+-------------+-------------------------------------------------------------------------------------------+

linodecli/response.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,58 @@
44
"""
55
from __future__ import print_function
66

7-
from colorclass import Color
7+
import os
8+
import platform
9+
10+
DO_COLORS = True
11+
# !! Windows compatibility for ANSI color codes !!
12+
#
13+
# If we're running on windows, we need to run the "color" command to enable
14+
# ANSI color code support.
15+
if platform.system() == "Windows":
16+
ver = platform.version()
17+
18+
if '.' in ver:
19+
ver = ver.split('.', 1)[0]
20+
21+
try:
22+
verNum = int(ver)
23+
except ValueError:
24+
DO_COLORS = False
25+
26+
# windows 10+ supports ANSI color codes after running the 'color' command to
27+
# properly set up the command prompt. Older versions of windows do not, and
28+
# we should not attempt to use them there.
29+
if verNum >= 10:
30+
os.system("color")
31+
else:
32+
DO_COLORS = False
33+
34+
35+
CLEAR_COLOR = "\x1b[0m"
36+
COLOR_CODE_MAP = {
37+
"red": "\x1b[31m",
38+
"green": "\x1b[32m",
39+
"yellow": "\x1b[33m",
40+
"black": "\x1b[30m",
41+
"white": "\x1b[40m",
42+
}
43+
44+
45+
def colorize_string(string, color):
46+
"""
47+
Returns the requested string, wrapped in ANSI color codes to colorize it as
48+
requested. On platforms where colors are not supported, this just returns
49+
the string passed into it.
50+
"""
51+
if not DO_COLORS:
52+
return string
53+
54+
col = COLOR_CODE_MAP.get(color, CLEAR_COLOR)
55+
56+
return "{}{}{}".format(
57+
col, string, CLEAR_COLOR,
58+
)
859

960

1061
class ModelAttr:
@@ -51,7 +102,7 @@ def render_value(self, model, colorize=True):
51102
# apply colors
52103
value = str(value) # just in case
53104
color = self.color_map.get(value) or self.color_map['default_']
54-
value = str(Color('{'+color+'}'+value+'{/'+color+'}'))
105+
value = colorize_string(value, color)
55106

56107
if value is None:
57108
# don't print the word "None"

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
terminaltables
2-
colorclass
32
requests
43
PyYAML
54
enum34; python_version < '3.4'

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def get_version():
4949
license="BSD 3-Clause License",
5050
install_requires=[
5151
"terminaltables",
52-
"colorclass",
5352
"requests",
5453
"PyYAML",
5554
"future; python_version <= '3.0.0'"

0 commit comments

Comments
 (0)