Skip to content

Commit 2f9aab5

Browse files
committed
Renamed colors setting to allow_ansi
1 parent 92f8e3d commit 2f9aab5

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

cmd2/ansi.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
"""Support for ANSI escape codes which are used for things like applying style to text"""
2+
"""Support for ANSI escape sequences which are used for things like applying style to text"""
33
import functools
44
import re
55
from typing import Any
@@ -8,7 +8,7 @@
88
from colorama import Fore, Back, Style
99
from wcwidth import wcswidth
1010

11-
# Regular expression to match ANSI escape codes
11+
# Regular expression to match ANSI escape sequences
1212
ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m')
1313

1414
# Foreground color presets
@@ -55,10 +55,10 @@
5555

5656

5757
def strip_ansi(text: str) -> str:
58-
"""Strip ANSI escape codes from a string.
58+
"""Strip ANSI escape sequences from a string.
5959
60-
:param text: string which may contain ANSI escape codes
61-
:return: the same string with any ANSI escape codes removed
60+
:param text: string which may contain ANSI escape sequences
61+
:return: the same string with any ANSI escape sequences removed
6262
"""
6363
return ANSI_ESCAPE_RE.sub('', text)
6464

@@ -69,11 +69,11 @@ def ansi_safe_wcswidth(text: str) -> int:
6969
7070
:param text: the string being measured
7171
"""
72-
# Strip ANSI escape codes since they cause wcswidth to return -1
72+
# Strip ANSI escape sequences since they cause wcswidth to return -1
7373
return wcswidth(strip_ansi(text))
7474

7575

76-
# ANSI escape strings not provided by colorama
76+
# ANSI escape sequences not provided by colorama
7777
UNDERLINE_ENABLE = colorama.ansi.code_to_chars(4)
7878
UNDERLINE_DISABLE = colorama.ansi.code_to_chars(24)
7979

@@ -122,7 +122,7 @@ def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underlin
122122
additions.append(UNDERLINE_ENABLE)
123123
removals.append(UNDERLINE_DISABLE)
124124

125-
# Combine the ANSI escape strings with the text
125+
# Combine the ANSI escape sequences with the text
126126
return "".join(additions) + text + "".join(removals)
127127

128128

cmd2/cmd2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
374374
self.quit_on_sigint = False # Quit the loop on interrupt instead of just resetting prompt
375375

376376
# Attributes which ARE dynamically settable at runtime
377-
self.colors = constants.ANSI_TERMINAL
377+
self.allow_ansi = constants.ANSI_TERMINAL
378378
self.continuation_prompt = '> '
379379
self.debug = False
380380
self.echo = False
@@ -385,7 +385,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
385385
self.timing = False # Prints elapsed time for each command
386386

387387
# To make an attribute settable with the "do_set" command, add it to this ...
388-
self.settable = {'colors': 'Allow colorized output (valid values: Terminal, Always, Never)',
388+
self.settable = {'allow_ansi': 'Allow ANSI escape sequences in output (valid values: Terminal, Always, Never)',
389389
'continuation_prompt': 'On 2nd+ line of input',
390390
'debug': 'Show full error stack on error',
391391
'echo': 'Echo command issued into output',
@@ -581,8 +581,8 @@ def _decolorized_write(self, fileobj: IO, msg: str) -> None:
581581
582582
Honor the current colors setting, which requires us to check whether the fileobject is a tty.
583583
"""
584-
if self.colors.lower() == constants.ANSI_NEVER.lower() or \
585-
(self.colors.lower() == constants.ANSI_TERMINAL.lower() and not fileobj.isatty()):
584+
if self.allow_ansi.lower() == constants.ANSI_NEVER.lower() or \
585+
(self.allow_ansi.lower() == constants.ANSI_TERMINAL.lower() and not fileobj.isatty()):
586586
msg = ansi.strip_ansi(msg)
587587
fileobj.write(msg)
588588

@@ -691,7 +691,7 @@ def ppaged(self, msg: str, end: str = '\n', chop: bool = False) -> None:
691691
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
692692
# Also only attempt to use a pager if actually running in a real fully functional terminal
693693
if functional_terminal and not self._redirecting and not self._in_py and not self._script_dir:
694-
if self.colors.lower() == constants.ANSI_NEVER.lower():
694+
if self.allow_ansi.lower() == constants.ANSI_NEVER.lower():
695695
msg_str = ansi.strip_ansi(msg_str)
696696

697697
pager = self.pager

cmd2/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'run_script', '@@': '_relative_run_script'}
1919

20-
# Values for cmd2 setting that determines when to allow ANSI escape codes
20+
# Values for cmd2's allow_ansi setting
2121
ANSI_NEVER = 'Never'
2222
ANSI_TERMINAL = 'Terminal'
2323
ANSI_ALWAYS = 'Always'

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def verify_help_text(cmd2_app: cmd2.Cmd, help_output: Union[str, List[str]]) ->
8888
"""
8989

9090
# Output from the show command with default settings
91-
SHOW_TXT = """colors: Terminal
91+
SHOW_TXT = """allow_ansi: Terminal
9292
continuation_prompt: >
9393
debug: False
9494
echo: False
@@ -101,7 +101,7 @@ def verify_help_text(cmd2_app: cmd2.Cmd, help_output: Union[str, List[str]]) ->
101101
"""
102102

103103
SHOW_LONG = """
104-
colors: Terminal # Allow colorized output (valid values: Terminal, Always, Never)
104+
allow_ansi: Terminal # Allow ANSI escape sequences in output (valid values: Terminal, Always, Never)
105105
continuation_prompt: > # On 2nd+ line of input
106106
debug: False # Show full error stack on error
107107
echo: False # Echo command issued into output

tests/test_cmd2.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,15 +1356,15 @@ def test_pseudo_raw_input_piped_rawinput_true_echo_true(capsys):
13561356
app, out = piped_rawinput_true(capsys, True, command)
13571357
out = out.splitlines()
13581358
assert out[0] == '{}{}'.format(app.prompt, command)
1359-
assert out[1].startswith('colors:')
1359+
assert out[1].startswith('allow_ansi:')
13601360

13611361
# using the decorator puts the original input function back when this unit test returns
13621362
@mock.patch('builtins.input', mock.MagicMock(name='input', side_effect=['set', EOFError]))
13631363
def test_pseudo_raw_input_piped_rawinput_true_echo_false(capsys):
13641364
command = 'set'
13651365
app, out = piped_rawinput_true(capsys, False, command)
13661366
firstline = out.splitlines()[0]
1367-
assert firstline.startswith('colors:')
1367+
assert firstline.startswith('allow_ansi:')
13681368
assert not '{}{}'.format(app.prompt, command) in out
13691369

13701370
# the next helper function and two tests check for piped
@@ -1383,13 +1383,13 @@ def test_pseudo_raw_input_piped_rawinput_false_echo_true(capsys):
13831383
app, out = piped_rawinput_false(capsys, True, command)
13841384
out = out.splitlines()
13851385
assert out[0] == '{}{}'.format(app.prompt, command)
1386-
assert out[1].startswith('colors:')
1386+
assert out[1].startswith('allow_ansi:')
13871387

13881388
def test_pseudo_raw_input_piped_rawinput_false_echo_false(capsys):
13891389
command = 'set'
13901390
app, out = piped_rawinput_false(capsys, False, command)
13911391
firstline = out.splitlines()[0]
1392-
assert firstline.startswith('colors:')
1392+
assert firstline.startswith('allow_ansi:')
13931393
assert not '{}{}'.format(app.prompt, command) in out
13941394

13951395

@@ -1459,15 +1459,15 @@ def test_poutput_none(outsim_app):
14591459

14601460
def test_poutput_color_always(outsim_app):
14611461
msg = 'Hello World'
1462-
outsim_app.colors = 'Always'
1462+
outsim_app.allow_ansi = 'Always'
14631463
outsim_app.poutput(ansi.style(msg, fg='cyan'))
14641464
out = outsim_app.stdout.getvalue()
14651465
expected = Fore.CYAN + msg + Fore.RESET + '\n'
14661466
assert out == expected
14671467

14681468
def test_poutput_color_never(outsim_app):
14691469
msg = 'Hello World'
1470-
outsim_app.colors = 'Never'
1470+
outsim_app.allow_ansi = 'Never'
14711471
outsim_app.poutput(ansi.style(msg, fg='cyan'))
14721472
out = outsim_app.stdout.getvalue()
14731473
expected = msg + '\n'
@@ -1764,19 +1764,19 @@ def test_ppaged(outsim_app):
17641764
out = outsim_app.stdout.getvalue()
17651765
assert out == msg + end
17661766

1767-
def test_ppaged_strips_color_when_redirecting(outsim_app):
1767+
def test_ppaged_strips_ansi_when_redirecting(outsim_app):
17681768
msg = 'testing...'
17691769
end = '\n'
1770-
outsim_app.colors = cmd2.constants.ANSI_TERMINAL
1770+
outsim_app.allow_ansi = cmd2.constants.ANSI_TERMINAL
17711771
outsim_app._redirecting = True
17721772
outsim_app.ppaged(Fore.RED + msg)
17731773
out = outsim_app.stdout.getvalue()
17741774
assert out == msg + end
17751775

1776-
def test_ppaged_strips_color_when_redirecting_if_always(outsim_app):
1776+
def test_ppaged_strips_ansi_when_redirecting_if_always(outsim_app):
17771777
msg = 'testing...'
17781778
end = '\n'
1779-
outsim_app.colors = cmd2.constants.ANSI_ALWAYS
1779+
outsim_app.allow_ansi = cmd2.constants.ANSI_ALWAYS
17801780
outsim_app._redirecting = True
17811781
outsim_app.ppaged(Fore.RED + msg)
17821782
out = outsim_app.stdout.getvalue()
@@ -1895,7 +1895,7 @@ def test_exit_code_nonzero(exit_code_repl):
18951895
assert out == expected
18961896

18971897

1898-
class ColorsApp(cmd2.Cmd):
1898+
class AnsiApp(cmd2.Cmd):
18991899
def __init__(self, *args, **kwargs):
19001900
super().__init__(*args, **kwargs)
19011901

@@ -1910,13 +1910,13 @@ def do_echo_error(self, args):
19101910
# perror uses colors by default
19111911
self.perror(args)
19121912

1913-
def test_colors_default():
1914-
app = ColorsApp()
1915-
assert app.colors == cmd2.constants.ANSI_TERMINAL
1913+
def test_ansi_default():
1914+
app = AnsiApp()
1915+
assert app.allow_ansi == cmd2.constants.ANSI_TERMINAL
19161916

1917-
def test_colors_pouterr_always_tty(mocker, capsys):
1918-
app = ColorsApp()
1919-
app.colors = cmd2.constants.ANSI_ALWAYS
1917+
def test_ansi_pouterr_always_tty(mocker, capsys):
1918+
app = AnsiApp()
1919+
app.allow_ansi = cmd2.constants.ANSI_ALWAYS
19201920
mocker.patch.object(app.stdout, 'isatty', return_value=True)
19211921
mocker.patch.object(sys.stderr, 'isatty', return_value=True)
19221922

@@ -1936,9 +1936,9 @@ def test_colors_pouterr_always_tty(mocker, capsys):
19361936
assert len(err) > len('oopsie\n')
19371937
assert 'oopsie' in err
19381938

1939-
def test_colors_pouterr_always_notty(mocker, capsys):
1940-
app = ColorsApp()
1941-
app.colors = cmd2.constants.ANSI_ALWAYS
1939+
def test_ansi_pouterr_always_notty(mocker, capsys):
1940+
app = AnsiApp()
1941+
app.allow_ansi = cmd2.constants.ANSI_ALWAYS
19421942
mocker.patch.object(app.stdout, 'isatty', return_value=False)
19431943
mocker.patch.object(sys.stderr, 'isatty', return_value=False)
19441944

@@ -1958,9 +1958,9 @@ def test_colors_pouterr_always_notty(mocker, capsys):
19581958
assert len(err) > len('oopsie\n')
19591959
assert 'oopsie' in err
19601960

1961-
def test_colors_terminal_tty(mocker, capsys):
1962-
app = ColorsApp()
1963-
app.colors = cmd2.constants.ANSI_TERMINAL
1961+
def test_ansi_terminal_tty(mocker, capsys):
1962+
app = AnsiApp()
1963+
app.allow_ansi = cmd2.constants.ANSI_TERMINAL
19641964
mocker.patch.object(app.stdout, 'isatty', return_value=True)
19651965
mocker.patch.object(sys.stderr, 'isatty', return_value=True)
19661966

@@ -1979,9 +1979,9 @@ def test_colors_terminal_tty(mocker, capsys):
19791979
assert len(err) > len('oopsie\n')
19801980
assert 'oopsie' in err
19811981

1982-
def test_colors_terminal_notty(mocker, capsys):
1983-
app = ColorsApp()
1984-
app.colors = cmd2.constants.ANSI_TERMINAL
1982+
def test_ansi_terminal_notty(mocker, capsys):
1983+
app = AnsiApp()
1984+
app.allow_ansi = cmd2.constants.ANSI_TERMINAL
19851985
mocker.patch.object(app.stdout, 'isatty', return_value=False)
19861986
mocker.patch.object(sys.stderr, 'isatty', return_value=False)
19871987

@@ -1993,9 +1993,9 @@ def test_colors_terminal_notty(mocker, capsys):
19931993
out, err = capsys.readouterr()
19941994
assert out == err == 'oopsie\n'
19951995

1996-
def test_colors_never_tty(mocker, capsys):
1997-
app = ColorsApp()
1998-
app.colors = cmd2.constants.ANSI_NEVER
1996+
def test_ansi_never_tty(mocker, capsys):
1997+
app = AnsiApp()
1998+
app.allow_ansi = cmd2.constants.ANSI_NEVER
19991999
mocker.patch.object(app.stdout, 'isatty', return_value=True)
20002000
mocker.patch.object(sys.stderr, 'isatty', return_value=True)
20012001

@@ -2007,9 +2007,9 @@ def test_colors_never_tty(mocker, capsys):
20072007
out, err = capsys.readouterr()
20082008
assert out == err == 'oopsie\n'
20092009

2010-
def test_colors_never_notty(mocker, capsys):
2011-
app = ColorsApp()
2012-
app.colors = cmd2.constants.ANSI_NEVER
2010+
def test_ansi_never_notty(mocker, capsys):
2011+
app = AnsiApp()
2012+
app.allow_ansi = cmd2.constants.ANSI_NEVER
20132013
mocker.patch.object(app.stdout, 'isatty', return_value=False)
20142014
mocker.patch.object(sys.stderr, 'isatty', return_value=False)
20152015

tests/transcripts/regex_set.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Regexes on prompts just make the trailing space obvious
55

66
(Cmd) set
7-
colors: /(Terminal|Always|Never)/
7+
allow_ansi: /(Terminal|Always|Never)/
88
continuation_prompt: >/ /
99
debug: False
1010
echo: False

0 commit comments

Comments
 (0)