Skip to content

Commit

Permalink
Added ability to colorize all aspects of BorderedTables and Alternati…
Browse files Browse the repository at this point in the history
…ngTables.

Refactored utils.align_text() to print less fill_char style characters.
  • Loading branch information
kmvanbrunt committed Oct 20, 2021
1 parent 3517951 commit 369cd7a
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 147 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Fixed bug where using choices on a Settable didn't verify that a valid choice had been entered.
* Enhancements
* Added settings to Column class which prevent a table from overriding existing styles in header
and/or data text. These were added to support nesting an AlternatingTable within an AlternatingTable,
but other custom table classes can also use these settings.
and/or data text. This allows for things like nesting an AlternatingTable in another AlternatingTable.
* AlternatingTable no longer applies background color to outer borders. This was done to improve appearance
since the background color extended beyond the borders of the table.
* Added ability to colorize all aspects of `BorderedTables` and `AlternatingTables`.
* Added support for 8-bit/256-colors with the `cmd2.EightBitFg` and `cmd2.EightBitBg` classes.
* Added support for 24-bit/RGB colors with the `cmd2.RgbFg` and `cmd2.RgbBg` classes.
* Removed dependency on colorama.
Expand Down
11 changes: 4 additions & 7 deletions cmd2/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ def __str__(self) -> str:
# TODO: Remove this PyShadowingNames usage when deprecated fg and bg classes are removed.
# noinspection PyShadowingNames
def style(
text: Any,
value: Any,
*,
fg: Optional[FgColor] = None,
bg: Optional[BgColor] = None,
Expand All @@ -959,7 +959,7 @@ def style(
The styling is self contained which means that at the end of the string reset code(s) are issued
to undo whatever styling was done at the beginning.
:param text: text to format (anything convertible to a str)
:param value: object whose text is to be styled
:param fg: foreground color provided as any subclass of FgColor (e.g. Fg, EightBitFg, RgbFg)
Defaults to no color.
:param bg: foreground color provided as any subclass of BgColor (e.g. Bg, EightBitBg, RgbBg)
Expand All @@ -978,9 +978,6 @@ def style(
# List of strings that remove style
removals: List[AnsiSequence] = []

# Convert the text object into a string if it isn't already one
text_formatted = str(text)

# Process the style settings
if fg is not None:
additions.append(fg)
Expand Down Expand Up @@ -1014,8 +1011,8 @@ def style(
additions.append(TextStyle.UNDERLINE_ENABLE)
removals.append(TextStyle.UNDERLINE_DISABLE)

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


# Default styles for printing strings of various types.
Expand Down
10 changes: 5 additions & 5 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
been piped to another process and that process terminates before the
cmd2 command is finished executing.
:param msg: message to print (anything convertible to a str)
:param msg: object to print
:param end: string appended after the end of the message, default a newline
"""
try:
Expand All @@ -1080,7 +1080,7 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
"""Print message to sys.stderr
:param msg: message to print (anything convertible to a str)
:param msg: object to print
:param end: string appended after the end of the message, default a newline
:param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
where the message text already has the desired style. Defaults to True.
Expand All @@ -1094,7 +1094,7 @@ def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) ->
def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
"""Wraps perror, but applies ansi.style_warning by default
:param msg: message to print (anything convertible to a str)
:param msg: object to print
:param end: string appended after the end of the message, default a newline
:param apply_style: If True, then ansi.style_warning will be applied to the message text. Set to False in cases
where the message text already has the desired style. Defaults to True.
Expand Down Expand Up @@ -1134,7 +1134,7 @@ def pfeedback(self, msg: Any, *, end: str = '\n') -> None:
"""For printing nonessential feedback. Can be silenced with `quiet`.
Inclusion in redirected output is controlled by `feedback_to_output`.
:param msg: message to print (anything convertible to a str)
:param msg: object to print
:param end: string appended after the end of the message, default a newline
"""
if not self.quiet:
Expand All @@ -1149,7 +1149,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
stdout or stdin are not a fully functional terminal.
:param msg: message to print to current stdout (anything convertible to a str)
:param msg: object to print
:param end: string appended after the end of the message, default a newline
:param chop: True -> causes lines longer than the screen width to be chopped (truncated) rather than wrapped
- truncated text is still accessible by scrolling with the right & left arrow keys
Expand Down

0 comments on commit 369cd7a

Please sign in to comment.