Skip to content

Commit 6b8b636

Browse files
committed
Made constants for color values
1 parent bc24624 commit 6b8b636

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

cmd2/argparse_completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def my_completer(text: str, line: str, begidx: int, endidx:int, extra_param: str
6666
from argparse import ZERO_OR_MORE, ONE_OR_MORE, ArgumentError, _, _get_action_name, SUPPRESS
6767
from typing import List, Dict, Tuple, Callable, Union
6868

69-
from colorama import Fore
69+
from . import constants
7070

7171
from .rl_utils import rl_force_redisplay
7272
from .utils import ansi_safe_wcswidth
@@ -996,7 +996,7 @@ def error(self, message: str) -> None:
996996
linum += 1
997997

998998
self.print_usage(sys.stderr)
999-
self.exit(2, Fore.LIGHTRED_EX + '{}\n\n'.format(formatted_message) + Fore.RESET)
999+
self.exit(2, constants.ERROR_COLOR + '{}\n\n'.format(formatted_message) + constants.RESET_COLOR)
10001000

10011001
def format_help(self) -> str:
10021002
"""Copy of format_help() from argparse.ArgumentParser with tweaks to separately display required parameters"""

cmd2/cmd2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Type, Union, IO
4343

4444
import colorama
45-
from colorama import Fore
4645

4746
from . import constants
4847
from . import plugin
@@ -59,7 +58,7 @@
5958
rl_warning = "Readline features including tab completion have been disabled since no \n" \
6059
"supported version of readline was found. To resolve this, install \n" \
6160
"pyreadline on Windows or gnureadline on Mac.\n\n"
62-
sys.stderr.write(Fore.LIGHTYELLOW_EX + rl_warning + Fore.RESET)
61+
sys.stderr.write(constants.WARNING_COLOR + rl_warning + constants.RESET_COLOR)
6362
else:
6463
from .rl_utils import rl_force_redisplay, readline
6564

@@ -616,7 +615,7 @@ def poutput(self, msg: Any, end: str = '\n', color: str = '') -> None:
616615
if not msg_str.endswith(end):
617616
msg_str += end
618617
if color:
619-
msg_str = color + msg_str + Fore.RESET
618+
msg_str = color + msg_str + constants.RESET_COLOR
620619
self.decolorized_write(self.stdout, msg_str)
621620
except BrokenPipeError:
622621
# This occurs if a command's output is being piped to another
@@ -627,8 +626,8 @@ def poutput(self, msg: Any, end: str = '\n', color: str = '') -> None:
627626
if self.broken_pipe_warning:
628627
sys.stderr.write(self.broken_pipe_warning)
629628

630-
def perror(self, err: Union[str, Exception], traceback_war: bool = True, err_color: str = Fore.LIGHTRED_EX,
631-
war_color: str = Fore.LIGHTYELLOW_EX) -> None:
629+
def perror(self, err: Union[str, Exception], traceback_war: bool = True, err_color: str = constants.ERROR_COLOR,
630+
war_color: str = constants.WARNING_COLOR) -> None:
632631
""" Print error message to sys.stderr and if debug is true, print an exception Traceback if one exists.
633632
634633
:param err: an Exception or error message to print out
@@ -644,12 +643,12 @@ def perror(self, err: Union[str, Exception], traceback_war: bool = True, err_col
644643
err_msg = "EXCEPTION of type '{}' occurred with message: '{}'\n".format(type(err).__name__, err)
645644
else:
646645
err_msg = "{}\n".format(err)
647-
err_msg = err_color + err_msg + Fore.RESET
646+
err_msg = err_color + err_msg + constants.RESET_COLOR
648647
self.decolorized_write(sys.stderr, err_msg)
649648

650649
if traceback_war and not self.debug:
651650
war = "To enable full traceback, run the following command: 'set debug true'\n"
652-
war = war_color + war + Fore.RESET
651+
war = war_color + war + constants.RESET_COLOR
653652
self.decolorized_write(sys.stderr, war)
654653

655654
def pfeedback(self, msg: str) -> None:
@@ -3745,7 +3744,7 @@ class TestMyAppCase(Cmd2TestCase):
37453744
test_results = runner.run(testcase)
37463745
if test_results.wasSuccessful():
37473746
self.decolorized_write(sys.stderr, stream.read())
3748-
self.poutput('Tests passed', color=Fore.LIGHTGREEN_EX)
3747+
self.poutput('Tests passed', color=constants.SUCCESS_COLOR)
37493748
else:
37503749
# Strip off the initial traceback which isn't particularly useful for end users
37513750
error_str = stream.read()

cmd2/constants.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Constants and definitions"""
44

55
import re
6+
from colorama import Fore
67

78
# Used for command parsing, output redirection, tab completion and word
89
# breaks. Do not change.
@@ -20,7 +21,13 @@
2021

2122
LINE_FEED = '\n'
2223

23-
# values for colors setting
24+
# Values for colors setting
2425
COLORS_NEVER = 'Never'
2526
COLORS_TERMINAL = 'Terminal'
2627
COLORS_ALWAYS = 'Always'
28+
29+
# Text colors
30+
SUCCESS_COLOR = Fore.LIGHTGREEN_EX
31+
WARNING_COLOR = Fore.LIGHTYELLOW_EX
32+
ERROR_COLOR = Fore.LIGHTRED_EX
33+
RESET_COLOR = Fore.RESET

0 commit comments

Comments
 (0)