Skip to content

Commit e273492

Browse files
authored
Merge pull request #820 from python-cmd2/style_fix
Fixed bug where a redefined ansi.style_error was not always being used
2 parents 711349c + 1694578 commit e273492

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.22 (TBD, 2019)
2+
* Bug Fixes
3+
* Fixed bug where a redefined `ansi.style_error` was not being used in all `cmd2` files
4+
15
## 0.9.21 (November 26, 2019)
26
* Bug Fixes
37
* Fixed bug where pipe processes were not being stopped by Ctrl-C

cmd2/argparse_completer.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from collections import deque
1515
from typing import Dict, List, Optional, Union
1616

17+
from . import ansi
1718
from . import cmd2
1819
from . import utils
19-
from .ansi import ansi_aware_write, ansi_safe_wcswidth, style_error
2020
from .argparse_custom import ATTR_CHOICES_CALLABLE, INFINITY, generate_range_error
2121
from .argparse_custom import ATTR_SUPPRESS_TAB_HINT, ATTR_DESCRIPTIVE_COMPLETION_HEADER, ATTR_NARGS_RANGE
2222
from .argparse_custom import ChoicesCallable, CompletionError, CompletionItem
@@ -193,9 +193,9 @@ def update_mutex_groups(arg_action: argparse.Action) -> bool:
193193
if arg_action == completer_action:
194194
return True
195195

196-
error = style_error("\nError: argument {}: not allowed with argument {}\n".
197-
format(argparse._get_action_name(arg_action),
198-
argparse._get_action_name(completer_action)))
196+
error = ansi.style_error("\nError: argument {}: not allowed with argument {}\n".
197+
format(argparse._get_action_name(arg_action),
198+
argparse._get_action_name(completer_action)))
199199
self._print_message(error)
200200
return False
201201

@@ -444,11 +444,11 @@ def _format_completions(self, action, completions: List[Union[str, CompletionIte
444444
completions.sort(key=self._cmd2_app.default_sort_key)
445445
self._cmd2_app.matches_sorted = True
446446

447-
token_width = ansi_safe_wcswidth(action.dest)
447+
token_width = ansi.ansi_safe_wcswidth(action.dest)
448448
completions_with_desc = []
449449

450450
for item in completions:
451-
item_width = ansi_safe_wcswidth(item)
451+
item_width = ansi.ansi_safe_wcswidth(item)
452452
if item_width > token_width:
453453
token_width = item_width
454454

@@ -585,7 +585,7 @@ def _complete_for_arg(self, arg_action: argparse.Action,
585585
def _print_message(msg: str) -> None:
586586
"""Print a message instead of tab completions and redraw the prompt and input line"""
587587
import sys
588-
ansi_aware_write(sys.stdout, msg + '\n')
588+
ansi.ansi_aware_write(sys.stdout, msg + '\n')
589589
rl_force_redisplay()
590590

591591
def _print_arg_hint(self, arg_action: argparse.Action) -> None:
@@ -615,7 +615,7 @@ def _print_unfinished_flag_error(self, flag_arg_state: _ArgumentState) -> None:
615615
format(argparse._get_action_name(flag_arg_state.action),
616616
generate_range_error(flag_arg_state.min, flag_arg_state.max),
617617
flag_arg_state.count)
618-
self._print_message(style_error('{}'.format(error)))
618+
self._print_message(ansi.style_error('{}'.format(error)))
619619

620620
def _print_completion_error(self, arg_action: argparse.Action, completion_error: CompletionError) -> None:
621621
"""
@@ -628,4 +628,4 @@ def _print_completion_error(self, arg_action: argparse.Action, completion_error:
628628

629629
error = ("\nError tab completing {}:\n"
630630
"{}\n".format(argparse._get_action_name(arg_action), indented_error))
631-
self._print_message(style_error('{}'.format(error)))
631+
self._print_message(ansi.style_error('{}'.format(error)))

cmd2/argparse_custom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def my_completer_method(self, text, line, begidx, endidx, arg_tokens)
185185
from argparse import ZERO_OR_MORE, ONE_OR_MORE, ArgumentError, _
186186
from typing import Callable, Optional, Tuple, Type, Union
187187

188-
from .ansi import ansi_aware_write, style_error
188+
from . import ansi
189189

190190
# Used in nargs ranges to signify there is no maximum
191191
INFINITY = float('inf')
@@ -747,7 +747,7 @@ def error(self, message: str) -> None:
747747
linum += 1
748748

749749
self.print_usage(sys.stderr)
750-
formatted_message = style_error(formatted_message)
750+
formatted_message = ansi.style_error(formatted_message)
751751
self.exit(2, '{}\n\n'.format(formatted_message))
752752

753753
# noinspection PyProtectedMember
@@ -806,7 +806,7 @@ def _print_message(self, message, file=None):
806806
if message:
807807
if file is None:
808808
file = sys.stderr
809-
ansi_aware_write(file, message)
809+
ansi.ansi_aware_write(file, message)
810810

811811

812812
# The default ArgumentParser class for a cmd2 app

examples/custom_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
from cmd2 import Cmd2ArgumentParser, set_default_argument_parser
8-
from cmd2.ansi import style_warning
8+
from cmd2 import ansi
99

1010

1111
# First define the parser
@@ -27,7 +27,7 @@ def error(self, message: str) -> None:
2727
linum += 1
2828

2929
self.print_usage(sys.stderr)
30-
formatted_message = style_warning(formatted_message)
30+
formatted_message = ansi.style_warning(formatted_message)
3131
self.exit(2, '{}\n\n'.format(formatted_message))
3232

3333

0 commit comments

Comments
 (0)