summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-01-08 17:11:17 -0500
committerGitHub <noreply@github.com>2020-01-08 17:11:17 -0500
commitdb093b630ed487838337a04a28af79475d37d1e0 (patch)
treed78cd5fcbb4946206377a162bbcb3b7c01576d15 /cmd2/cmd2.py
parent97dd6f37482510cfc424049ff0b0369b79e34335 (diff)
parent383853ee92f69c3e4ccadacd34e0f22e1e896345 (diff)
downloadcmd2-git-db093b630ed487838337a04a28af79475d37d1e0.tar.gz
Merge pull request #843 from python-cmd2/ansi_to_style
ansi to style
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py58
1 files changed, 29 insertions, 29 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 9120c6af..434ade5f 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -206,11 +206,11 @@ class Cmd(cmd.Cmd):
# To make an attribute settable with the "do_set" command, add it to this ...
self.settable = \
{
- # allow_ansi is a special case in which it's an application-wide setting defined in ansi.py
- 'allow_ansi': ('Allow ANSI escape sequences in output '
- '(valid values: {}, {}, {})'.format(ansi.ANSI_TERMINAL,
- ansi.ANSI_ALWAYS,
- ansi.ANSI_NEVER)),
+ # allow_style is a special case in which it's an application-wide setting defined in ansi.py
+ 'allow_style': ('Allow ANSI text style sequences in output '
+ '(valid values: {}, {}, {})'.format(ansi.STYLE_TERMINAL,
+ ansi.STYLE_ALWAYS,
+ ansi.STYLE_NEVER)),
'continuation_prompt': 'On 2nd+ line of input',
'debug': 'Show full error stack on error',
'echo': 'Echo command issued into output',
@@ -366,7 +366,7 @@ class Cmd(cmd.Cmd):
else:
# Here is the meaning of the various flags we are using with the less command:
# -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
- # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
+ # -R causes ANSI "style" escape sequences to be output in raw form (i.e. colors are displayed)
# -X disables sending the termcap initialization and deinitialization strings to the terminal
# -F causes less to automatically exit if the entire file can be displayed on the first screen
self.pager = 'less -RXF'
@@ -395,23 +395,23 @@ class Cmd(cmd.Cmd):
# ----- Methods related to presenting output to the user -----
@property
- def allow_ansi(self) -> str:
- """Read-only property needed to support do_set when it reads allow_ansi"""
- return ansi.allow_ansi
+ def allow_style(self) -> str:
+ """Read-only property needed to support do_set when it reads allow_style"""
+ return ansi.allow_style
- @allow_ansi.setter
- def allow_ansi(self, new_val: str) -> None:
- """Setter property needed to support do_set when it updates allow_ansi"""
+ @allow_style.setter
+ def allow_style(self, new_val: str) -> None:
+ """Setter property needed to support do_set when it updates allow_style"""
new_val = new_val.lower()
- if new_val == ansi.ANSI_TERMINAL.lower():
- ansi.allow_ansi = ansi.ANSI_TERMINAL
- elif new_val == ansi.ANSI_ALWAYS.lower():
- ansi.allow_ansi = ansi.ANSI_ALWAYS
- elif new_val == ansi.ANSI_NEVER.lower():
- ansi.allow_ansi = ansi.ANSI_NEVER
+ if new_val == ansi.STYLE_TERMINAL.lower():
+ ansi.allow_style = ansi.STYLE_TERMINAL
+ elif new_val == ansi.STYLE_ALWAYS.lower():
+ ansi.allow_style = ansi.STYLE_ALWAYS
+ elif new_val == ansi.STYLE_NEVER.lower():
+ ansi.allow_style = ansi.STYLE_NEVER
else:
- self.perror('Invalid value: {} (valid values: {}, {}, {})'.format(new_val, ansi.ANSI_TERMINAL,
- ansi.ANSI_ALWAYS, ansi.ANSI_NEVER))
+ self.perror('Invalid value: {} (valid values: {}, {}, {})'.format(new_val, ansi.STYLE_TERMINAL,
+ ansi.STYLE_ALWAYS, ansi.STYLE_NEVER))
def _completion_supported(self) -> bool:
"""Return whether tab completion is supported"""
@@ -419,14 +419,14 @@ class Cmd(cmd.Cmd):
@property
def visible_prompt(self) -> str:
- """Read-only property to get the visible prompt with any ANSI escape codes stripped.
+ """Read-only property to get the visible prompt with any ANSI style escape codes stripped.
Used by transcript testing to make it easier and more reliable when users are doing things like coloring the
prompt using ANSI color codes.
:return: prompt stripped of any ANSI escape codes
"""
- return ansi.strip_ansi(self.prompt)
+ return ansi.strip_style(self.prompt)
def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
"""Print message to self.stdout and appends a newline by default
@@ -439,7 +439,7 @@ class Cmd(cmd.Cmd):
:param end: string appended after the end of the message, default a newline
"""
try:
- ansi.ansi_aware_write(self.stdout, "{}{}".format(msg, end))
+ ansi.style_aware_write(self.stdout, "{}{}".format(msg, end))
except BrokenPipeError:
# This occurs if a command's output is being piped to another
# process and that process closes before the command is
@@ -462,7 +462,7 @@ class Cmd(cmd.Cmd):
final_msg = ansi.style_error(msg)
else:
final_msg = "{}".format(msg)
- ansi.ansi_aware_write(sys.stderr, final_msg + end)
+ ansi.style_aware_write(sys.stderr, final_msg + end)
def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
"""Wraps perror, but applies ansi.style_warning by default
@@ -551,8 +551,8 @@ class Cmd(cmd.Cmd):
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
# Also only attempt to use a pager if actually running in a real fully functional terminal
if functional_terminal and not self._redirecting and not self.in_pyscript() and not self.in_script():
- if ansi.allow_ansi.lower() == ansi.ANSI_NEVER.lower():
- msg_str = ansi.strip_ansi(msg_str)
+ if ansi.allow_style.lower() == ansi.STYLE_NEVER.lower():
+ msg_str = ansi.strip_style(msg_str)
msg_str += end
pager = self.pager
@@ -1096,7 +1096,7 @@ class Cmd(cmd.Cmd):
longest_match_length = 0
for cur_match in matches_to_display:
- cur_length = ansi.ansi_safe_wcswidth(cur_match)
+ cur_length = ansi.style_aware_wcswidth(cur_match)
if cur_length > longest_match_length:
longest_match_length = cur_length
else:
@@ -2653,7 +2653,7 @@ class Cmd(cmd.Cmd):
widest = 0
# measure the commands
for command in cmds:
- width = ansi.ansi_safe_wcswidth(command)
+ width = ansi.style_aware_wcswidth(command)
if width > widest:
widest = width
# add a 4-space pad
@@ -3737,7 +3737,7 @@ class Cmd(cmd.Cmd):
test_results = runner.run(testcase)
execution_time = time.time() - start_time
if test_results.wasSuccessful():
- ansi.ansi_aware_write(sys.stderr, stream.read())
+ ansi.style_aware_write(sys.stderr, stream.read())
finish_msg = ' {0} transcript{1} passed in {2:.3f} seconds '.format(num_transcripts, plural, execution_time)
finish_msg = ansi.style_success(utils.align_center(finish_msg, fill_char='='))
self.poutput(finish_msg)