-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed: --no-color option didn't work (#198)
* Refactoring(color usage): new class Color makes it stateful and able to use args. * Refactoring(top-utils): default init and post_optparse defined in basetop. * Refactoring(color usage): "null-object" for --no-color and .enabled field
- Loading branch information
1 parent
8069cb5
commit 797bf32
Showing
9 changed files
with
131 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,73 @@ | ||
from colorama import Fore, Style | ||
|
||
try: | ||
YELLOW = Fore.LIGHTYELLOW_EX | ||
GREY = Fore.LIGHTBLACK_EX | ||
except AttributeError: | ||
YELLOW = Fore.YELLOW | ||
GREY = Fore.CYAN | ||
|
||
COLORS_NODE = { | ||
0: Fore.GREEN, | ||
1: Fore.RED, | ||
2: YELLOW, | ||
3: Fore.BLUE, | ||
-1: Style.RESET_ALL, | ||
} | ||
|
||
COLORS_SOCKET = { | ||
0: Fore.BLUE, | ||
1: YELLOW, | ||
2: Fore.RED, | ||
3: Fore.GREEN, | ||
-1: Style.RESET_ALL, | ||
} | ||
|
||
|
||
def bright(string): | ||
return wrap(string, Style.BRIGHT) | ||
|
||
|
||
def wrap_header(string): | ||
return wrap("# {0}\n".format(string), Style.BRIGHT) | ||
# coding: utf-8 | ||
|
||
|
||
def colorize(value, warning, error): | ||
return wrap(value, Fore.RED if value >= error else YELLOW if value >= warning else Fore.RESET) | ||
|
||
|
||
def wrap(word, color): | ||
""" wrap string in given color """ | ||
return '{0}{1}{2}'.format(color, word, Style.RESET_ALL) | ||
|
||
|
||
def __choose_color_scheme(topology): | ||
return COLORS_NODE if topology.layout_kind == 'NUMA' else COLORS_SOCKET | ||
|
||
|
||
def cpu_color(cpu, topology, color_scheme=None): | ||
if not color_scheme: | ||
color_scheme = __choose_color_scheme(topology) | ||
if isinstance(cpu, str): | ||
cpu = int(cpu[3:]) | ||
return color_scheme.get(topology.layout.get(cpu)) | ||
from colorama import Fore, Style | ||
|
||
|
||
def colorize_cpu_list(cpu_list, topology): | ||
""" return list of highlighted strings with CPU names regarding to NUMA """ | ||
color_scheme = __choose_color_scheme(topology) | ||
return [wrap(cpu, cpu_color(cpu, topology, color_scheme)) for cpu in cpu_list] | ||
class Color(object): | ||
""" Helper for highlighting a console tools """ | ||
try: | ||
YELLOW = Fore.LIGHTYELLOW_EX | ||
GREY = Fore.LIGHTBLACK_EX | ||
except AttributeError: | ||
YELLOW = Fore.YELLOW | ||
GREY = Fore.CYAN | ||
|
||
COLORS_NODE = { | ||
0: Fore.GREEN, | ||
1: Fore.RED, | ||
2: YELLOW, | ||
3: Fore.BLUE, | ||
-1: Style.RESET_ALL, | ||
} | ||
|
||
COLORS_SOCKET = { | ||
0: Fore.BLUE, | ||
1: YELLOW, | ||
2: Fore.RED, | ||
3: Fore.GREEN, | ||
-1: Style.RESET_ALL, | ||
} | ||
|
||
COLOR_NONE = dict((key, "") for key in range(-1, 4)) | ||
|
||
def __init__(self, topology, enabled=True): | ||
self.enabled = enabled | ||
self.topology = topology | ||
if topology is not None: | ||
self.color_scheme = self.__choose_color_scheme() | ||
|
||
def __choose_color_scheme(self): | ||
if not self.enabled: | ||
Style.BRIGHT = Style.RESET_ALL = Fore.RED = Fore.RESET = self.GREY = self.YELLOW = "" | ||
return self.COLOR_NONE | ||
if self.topology.layout_kind == 'NUMA': | ||
return self.COLORS_NODE | ||
return self.COLORS_SOCKET | ||
|
||
@staticmethod | ||
def wrap(word, color): | ||
""" wrap string in given color """ | ||
return '{0}{1}{2}'.format(color, word, Style.RESET_ALL) | ||
|
||
@staticmethod | ||
def colorize(value, warning, error): | ||
return Color.wrap(value, Fore.RED if value >= error else Color.YELLOW if value >= warning else Fore.RESET) | ||
|
||
@staticmethod | ||
def bright(string): | ||
return Color.wrap(string, Style.BRIGHT) | ||
|
||
@staticmethod | ||
def wrap_header(string): | ||
return Color.wrap("# {0}\n".format(string), Style.BRIGHT) | ||
|
||
def colorize_cpu(self, cpu): | ||
if not self.color_scheme: | ||
self.color_scheme = self.__choose_color_scheme() | ||
if isinstance(cpu, str): | ||
cpu = int(cpu[3:]) | ||
return self.color_scheme.get(self.topology.layout.get(cpu)) | ||
|
||
def colorize_cpu_list(self, cpu_list): | ||
""" return list of highlighted strings with CPU names regarding to NUMA """ | ||
return [self.wrap(cpu, self.colorize_cpu(cpu)) for cpu in cpu_list] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.