Skip to content

Commit 369cd7a

Browse files
committed
Added ability to colorize all aspects of BorderedTables and AlternatingTables.
Refactored utils.align_text() to print less fill_char style characters.
1 parent 3517951 commit 369cd7a

File tree

7 files changed

+239
-147
lines changed

7 files changed

+239
-147
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* Fixed bug where using choices on a Settable didn't verify that a valid choice had been entered.
55
* Enhancements
66
* Added settings to Column class which prevent a table from overriding existing styles in header
7-
and/or data text. These were added to support nesting an AlternatingTable within an AlternatingTable,
8-
but other custom table classes can also use these settings.
7+
and/or data text. This allows for things like nesting an AlternatingTable in another AlternatingTable.
98
* AlternatingTable no longer applies background color to outer borders. This was done to improve appearance
109
since the background color extended beyond the borders of the table.
10+
* Added ability to colorize all aspects of `BorderedTables` and `AlternatingTables`.
1111
* Added support for 8-bit/256-colors with the `cmd2.EightBitFg` and `cmd2.EightBitBg` classes.
1212
* Added support for 24-bit/RGB colors with the `cmd2.RgbFg` and `cmd2.RgbBg` classes.
1313
* Removed dependency on colorama.

cmd2/ansi.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ def __str__(self) -> str:
943943
# TODO: Remove this PyShadowingNames usage when deprecated fg and bg classes are removed.
944944
# noinspection PyShadowingNames
945945
def style(
946-
text: Any,
946+
value: Any,
947947
*,
948948
fg: Optional[FgColor] = None,
949949
bg: Optional[BgColor] = None,
@@ -959,7 +959,7 @@ def style(
959959
The styling is self contained which means that at the end of the string reset code(s) are issued
960960
to undo whatever styling was done at the beginning.
961961
962-
:param text: text to format (anything convertible to a str)
962+
:param value: object whose text is to be styled
963963
:param fg: foreground color provided as any subclass of FgColor (e.g. Fg, EightBitFg, RgbFg)
964964
Defaults to no color.
965965
:param bg: foreground color provided as any subclass of BgColor (e.g. Bg, EightBitBg, RgbBg)
@@ -978,9 +978,6 @@ def style(
978978
# List of strings that remove style
979979
removals: List[AnsiSequence] = []
980980

981-
# Convert the text object into a string if it isn't already one
982-
text_formatted = str(text)
983-
984981
# Process the style settings
985982
if fg is not None:
986983
additions.append(fg)
@@ -1014,8 +1011,8 @@ def style(
10141011
additions.append(TextStyle.UNDERLINE_ENABLE)
10151012
removals.append(TextStyle.UNDERLINE_DISABLE)
10161013

1017-
# Combine the ANSI style sequences with the text
1018-
return "".join(map(str, additions)) + text_formatted + "".join(map(str, removals))
1014+
# Combine the ANSI style sequences with the value's text
1015+
return "".join(map(str, additions)) + str(value) + "".join(map(str, removals))
10191016

10201017

10211018
# Default styles for printing strings of various types.

cmd2/cmd2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
10621062
been piped to another process and that process terminates before the
10631063
cmd2 command is finished executing.
10641064
1065-
:param msg: message to print (anything convertible to a str)
1065+
:param msg: object to print
10661066
:param end: string appended after the end of the message, default a newline
10671067
"""
10681068
try:
@@ -1080,7 +1080,7 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
10801080
def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
10811081
"""Print message to sys.stderr
10821082
1083-
:param msg: message to print (anything convertible to a str)
1083+
:param msg: object to print
10841084
:param end: string appended after the end of the message, default a newline
10851085
:param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
10861086
where the message text already has the desired style. Defaults to True.
@@ -1094,7 +1094,7 @@ def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) ->
10941094
def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
10951095
"""Wraps perror, but applies ansi.style_warning by default
10961096
1097-
:param msg: message to print (anything convertible to a str)
1097+
:param msg: object to print
10981098
:param end: string appended after the end of the message, default a newline
10991099
:param apply_style: If True, then ansi.style_warning will be applied to the message text. Set to False in cases
11001100
where the message text already has the desired style. Defaults to True.
@@ -1134,7 +1134,7 @@ def pfeedback(self, msg: Any, *, end: str = '\n') -> None:
11341134
"""For printing nonessential feedback. Can be silenced with `quiet`.
11351135
Inclusion in redirected output is controlled by `feedback_to_output`.
11361136
1137-
:param msg: message to print (anything convertible to a str)
1137+
:param msg: object to print
11381138
:param end: string appended after the end of the message, default a newline
11391139
"""
11401140
if not self.quiet:
@@ -1149,7 +1149,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
11491149
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
11501150
stdout or stdin are not a fully functional terminal.
11511151
1152-
:param msg: message to print to current stdout (anything convertible to a str)
1152+
:param msg: object to print
11531153
:param end: string appended after the end of the message, default a newline
11541154
:param chop: True -> causes lines longer than the screen width to be chopped (truncated) rather than wrapped
11551155
- truncated text is still accessible by scrolling with the right & left arrow keys

0 commit comments

Comments
 (0)