Skip to content

Commit 5a34460

Browse files
committed
Rolled back undocumented changes to printing functions introduced in 2.5.0.
1 parent 20c893d commit 5a34460

File tree

7 files changed

+41
-128
lines changed

7 files changed

+41
-128
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.8 (TBD)
2+
* Bug Fixes
3+
* Rolled back undocumented changes to printing functions introduced in 2.5.0.
4+
15
## 2.5.7 (November 22, 2024)
26
* Bug Fixes
37
* Fixed issue where argument parsers for overridden commands were not being created.

Diff for: cmd2/ansi.py

-3
Original file line numberDiff line numberDiff line change
@@ -1042,9 +1042,6 @@ def style(
10421042
# Default styles for printing strings of various types.
10431043
# These can be altered to suit an application's needs and only need to be a
10441044
# function with the following structure: func(str) -> str
1045-
style_output = functools.partial(style)
1046-
"""Partial function supplying arguments to :meth:`cmd2.ansi.style()` which colors text for normal output"""
1047-
10481045
style_success = functools.partial(style, fg=Fg.GREEN)
10491046
"""Partial function supplying arguments to :meth:`cmd2.ansi.style()` which colors text to signify success"""
10501047

Diff for: cmd2/cmd2.py

+32-120
Original file line numberDiff line numberDiff line change
@@ -1211,124 +1211,55 @@ def visible_prompt(self) -> str:
12111211
return ansi.strip_style(self.prompt)
12121212

12131213
def print_to(
1214-
self,
1215-
dest: Union[TextIO, IO[str]],
1216-
msg: Any,
1217-
*,
1218-
end: str = '\n',
1219-
style: Optional[Callable[[str], str]] = None,
1220-
paged: bool = False,
1221-
chop: bool = False,
1214+
self, dest: Union[TextIO, IO[str]], msg: Any, *, end: str = '\n', style: Optional[Callable[[str], str]] = None
12221215
) -> None:
12231216
final_msg = style(msg) if style is not None else msg
1224-
if paged:
1225-
self.ppaged(final_msg, end=end, chop=chop, dest=dest)
1226-
else:
1227-
try:
1228-
ansi.style_aware_write(dest, f'{final_msg}{end}')
1229-
except BrokenPipeError:
1230-
# This occurs if a command's output is being piped to another
1231-
# process and that process closes before the command is
1232-
# finished. If you would like your application to print a
1233-
# warning message, then set the broken_pipe_warning attribute
1234-
# to the message you want printed.
1235-
if self.broken_pipe_warning:
1236-
sys.stderr.write(self.broken_pipe_warning)
1237-
1238-
def poutput(
1239-
self,
1240-
msg: Any = '',
1241-
*,
1242-
end: str = '\n',
1243-
apply_style: bool = True,
1244-
paged: bool = False,
1245-
chop: bool = False,
1246-
) -> None:
1217+
try:
1218+
ansi.style_aware_write(dest, f'{final_msg}{end}')
1219+
except BrokenPipeError:
1220+
# This occurs if a command's output is being piped to another
1221+
# process and that process closes before the command is
1222+
# finished. If you would like your application to print a
1223+
# warning message, then set the broken_pipe_warning attribute
1224+
# to the message you want printed.
1225+
if self.broken_pipe_warning:
1226+
sys.stderr.write(self.broken_pipe_warning)
1227+
1228+
def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
12471229
"""Print message to self.stdout and appends a newline by default
12481230
1249-
Also handles BrokenPipeError exceptions for when a command's output has
1250-
been piped to another process and that process terminates before the
1251-
cmd2 command is finished executing.
1252-
12531231
:param msg: object to print
12541232
:param end: string appended after the end of the message, default a newline
1255-
:param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1256-
where the message text already has the desired style. Defaults to True.
1257-
:param paged: If True, pass the output through the configured pager.
1258-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
12591233
"""
1260-
self.print_to(self.stdout, msg, end=end, style=ansi.style_output if apply_style else None, paged=paged, chop=chop)
1234+
self.print_to(self.stdout, msg, end=end)
12611235

1262-
def perror(
1263-
self,
1264-
msg: Any = '',
1265-
*,
1266-
end: str = '\n',
1267-
apply_style: bool = True,
1268-
paged: bool = False,
1269-
chop: bool = False,
1270-
) -> None:
1236+
def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
12711237
"""Print message to sys.stderr
12721238
12731239
:param msg: object to print
12741240
:param end: string appended after the end of the message, default a newline
12751241
:param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
12761242
where the message text already has the desired style. Defaults to True.
1277-
:param paged: If True, pass the output through the configured pager.
1278-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
12791243
"""
1280-
self.print_to(sys.stderr, msg, end=end, style=ansi.style_error if apply_style else None, paged=paged, chop=chop)
1244+
self.print_to(sys.stderr, msg, end=end, style=ansi.style_error if apply_style else None)
12811245

1282-
def psuccess(
1283-
self,
1284-
msg: Any = '',
1285-
*,
1286-
end: str = '\n',
1287-
paged: bool = False,
1288-
chop: bool = False,
1289-
) -> None:
1290-
"""Writes to stdout applying ansi.style_success by default
1246+
def psuccess(self, msg: Any = '', *, end: str = '\n') -> None:
1247+
"""Wraps poutput, but applies ansi.style_success by default
12911248
12921249
:param msg: object to print
12931250
:param end: string appended after the end of the message, default a newline
1294-
:param paged: If True, pass the output through the configured pager.
1295-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
12961251
"""
1297-
self.print_to(self.stdout, msg, end=end, style=ansi.style_success, paged=paged, chop=chop)
1252+
msg = ansi.style_success(msg)
1253+
self.poutput(msg, end=end)
12981254

1299-
def pwarning(
1300-
self,
1301-
msg: Any = '',
1302-
*,
1303-
end: str = '\n',
1304-
paged: bool = False,
1305-
chop: bool = False,
1306-
) -> None:
1255+
def pwarning(self, msg: Any = '', *, end: str = '\n') -> None:
13071256
"""Wraps perror, but applies ansi.style_warning by default
13081257
13091258
:param msg: object to print
13101259
:param end: string appended after the end of the message, default a newline
1311-
:param paged: If True, pass the output through the configured pager.
1312-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
13131260
"""
1314-
self.print_to(sys.stderr, msg, end=end, style=ansi.style_warning, paged=paged, chop=chop)
1315-
1316-
def pfailure(
1317-
self,
1318-
msg: Any = '',
1319-
*,
1320-
end: str = '\n',
1321-
paged: bool = False,
1322-
chop: bool = False,
1323-
) -> None:
1324-
"""Writes to stderr applying ansi.style_error by default
1325-
1326-
:param msg: object to print
1327-
:param end: string appended after the end of the message, default a newline
1328-
:param paged: If True, pass the output through the configured pager.
1329-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1330-
"""
1331-
self.print_to(sys.stderr, msg, end=end, style=ansi.style_error, paged=paged, chop=chop)
1261+
msg = ansi.style_warning(msg)
1262+
self.perror(msg, end=end, apply_style=False)
13321263

13331264
def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
13341265
"""Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists.
@@ -1357,36 +1288,20 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non
13571288

13581289
self.perror(final_msg, end=end, apply_style=False)
13591290

1360-
def pfeedback(
1361-
self,
1362-
msg: Any,
1363-
*,
1364-
end: str = '\n',
1365-
apply_style: bool = True,
1366-
paged: bool = False,
1367-
chop: bool = False,
1368-
) -> None:
1291+
def pfeedback(self, msg: Any, *, end: str = '\n') -> None:
13691292
"""For printing nonessential feedback. Can be silenced with `quiet`.
13701293
Inclusion in redirected output is controlled by `feedback_to_output`.
13711294
13721295
:param msg: object to print
13731296
:param end: string appended after the end of the message, default a newline
1374-
:param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1375-
where the message text already has the desired style. Defaults to True.
1376-
:param paged: If True, pass the output through the configured pager.
1377-
:param chop: If paged is True, True to truncate long lines or False to wrap long lines.
13781297
"""
13791298
if not self.quiet:
1380-
self.print_to(
1381-
self.stdout if self.feedback_to_output else sys.stderr,
1382-
msg,
1383-
end=end,
1384-
style=ansi.style_output if apply_style else None,
1385-
paged=paged,
1386-
chop=chop,
1387-
)
1299+
if self.feedback_to_output:
1300+
self.poutput(msg, end=end)
1301+
else:
1302+
self.perror(msg, end=end, apply_style=False)
13881303

1389-
def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optional[Union[TextIO, IO[str]]] = None) -> None:
1304+
def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
13901305
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
13911306
13921307
Never uses a pager inside a script (Python or text) or when output is being redirected or piped or when
@@ -1399,17 +1314,14 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
13991314
- chopping is ideal for displaying wide tabular data as is done in utilities like pgcli
14001315
False -> causes lines longer than the screen width to wrap to the next line
14011316
- wrapping is ideal when you want to keep users from having to use horizontal scrolling
1402-
:param dest: Optionally specify the destination stream to write to. If unspecified, defaults to self.stdout
14031317
14041318
WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
14051319
"""
1406-
dest = self.stdout if dest is None else dest
1407-
14081320
# Attempt to detect if we are not running within a fully functional terminal.
14091321
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
14101322
functional_terminal = False
14111323

1412-
if self.stdin.isatty() and dest.isatty():
1324+
if self.stdin.isatty() and self.stdout.isatty():
14131325
if sys.platform.startswith('win') or os.environ.get('TERM') is not None:
14141326
functional_terminal = True
14151327

@@ -1430,7 +1342,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
14301342
with self.sigint_protection:
14311343
import subprocess
14321344

1433-
pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE, stdout=dest)
1345+
pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE, stdout=self.stdout)
14341346
pipe_proc.communicate(final_msg.encode('utf-8', 'replace'))
14351347
except BrokenPipeError:
14361348
# This occurs if a command's output is being piped to another process and that process closes before the
@@ -1439,7 +1351,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
14391351
if self.broken_pipe_warning:
14401352
sys.stderr.write(self.broken_pipe_warning)
14411353
else:
1442-
self.print_to(dest, msg, end=end, paged=False)
1354+
self.poutput(msg, end=end)
14431355

14441356
# ----- Methods related to tab completion -----
14451357

Diff for: examples/colors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def do_speak(self, args):
7777

7878
for _ in range(min(repetitions, self.maxrepeats)):
7979
# .poutput handles newlines, and accommodates output redirection too
80-
self.poutput(output_str, apply_style=False)
80+
self.poutput(output_str)
8181

8282
def do_timetravel(self, _):
8383
"""A command which always generates an error message, to demonstrate custom error colors"""

Diff for: examples/initialization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def do_intro(self, _):
6565
def do_echo(self, arg):
6666
"""Example of a multiline command"""
6767
fg_color = Fg[self.foreground_color.upper()]
68-
self.poutput(style(arg, fg=fg_color), apply_style=False)
68+
self.poutput(style(arg, fg=fg_color))
6969

7070

7171
if __name__ == '__main__':

Diff for: examples/pirate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def do_quit(self, arg):
7575

7676
def do_sing(self, arg):
7777
"""Sing a colorful song."""
78-
self.poutput(cmd2.ansi.style(arg, fg=Fg[self.songcolor.upper()]), apply_style=False)
78+
self.poutput(cmd2.ansi.style(arg, fg=Fg[self.songcolor.upper()]))
7979

8080
yo_parser = cmd2.Cmd2ArgumentParser()
8181
yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")

Diff for: tests/test_cmd2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ def test_poutput_none(outsim_app):
19921992
def test_poutput_ansi_always(outsim_app):
19931993
msg = 'Hello World'
19941994
colored_msg = ansi.style(msg, fg=ansi.Fg.CYAN)
1995-
outsim_app.poutput(colored_msg, apply_style=False)
1995+
outsim_app.poutput(colored_msg)
19961996
out = outsim_app.stdout.getvalue()
19971997
expected = colored_msg + '\n'
19981998
assert colored_msg != msg
@@ -2003,7 +2003,7 @@ def test_poutput_ansi_always(outsim_app):
20032003
def test_poutput_ansi_never(outsim_app):
20042004
msg = 'Hello World'
20052005
colored_msg = ansi.style(msg, fg=ansi.Fg.CYAN)
2006-
outsim_app.poutput(colored_msg, apply_style=False)
2006+
outsim_app.poutput(colored_msg)
20072007
out = outsim_app.stdout.getvalue()
20082008
expected = msg + '\n'
20092009
assert colored_msg != msg

0 commit comments

Comments
 (0)