Skip to content

Commit 6232792

Browse files
committed
Updated Sphinx documentation and README.md
1 parent 307c53e commit 6232792

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Main Features
4343
- Built-in regression testing framework for your applications (transcript-based testing)
4444
- Transcripts for use with built-in regression can be automatically generated from `history -t` or `run_script -t`
4545
- Alerts that seamlessly print while user enters text at prompt
46+
- Colored and stylized output using `ansi.style()`
4647

4748
Python 2.7 support is EOL
4849
-------------------------
@@ -89,7 +90,7 @@ Instructions for implementing each feature follow.
8990
class MyApp(cmd2.Cmd):
9091
def do_foo(self, args):
9192
"""This docstring is the built-in help for the foo command."""
92-
print('foo bar baz')
93+
print(cmd2.ansi.style('foo bar baz', fg='red'))
9394
```
9495
- By default the docstring for your **do_foo** method is the help for the **foo** command
9596
- NOTE: This doesn't apply if you use one of the `argparse` decorators mentioned below
@@ -314,11 +315,10 @@ example/transcript_regex.txt:
314315

315316
```text
316317
# Run this transcript with "python example.py -t transcript_regex.txt"
317-
# The regex for colors is because no color on Windows.
318318
# The regex for editor will match whatever program you use.
319319
# regexes on prompts just make the trailing space obvious
320320
(Cmd) set
321-
colors: /(True|False)/
321+
allow_ansi: Terminal
322322
continuation_prompt: >/ /
323323
debug: False
324324
echo: False
@@ -331,9 +331,7 @@ quiet: False
331331
timing: False
332332
```
333333

334-
Note how a regular expression `/(True|False)/` is used for output of the **show color** command since
335-
colored text is currently not available for cmd2 on Windows. Regular expressions can be used anywhere within a
336-
transcript file simply by enclosing them within forward slashes, `/`.
334+
Regular expressions can be used anywhere within a transcript file simply by enclosing them within forward slashes, `/`.
337335

338336

339337
Found a bug?

cmd2/ansi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ def bg_lookup(bg_name: str) -> str:
132132

133133

134134
def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str:
135-
"""
136-
Applies styles to text
135+
"""Styles a string with ANSI colors and/or styles and returns the new string.
136+
137+
The styling is self contained which means that at the end of the string reset code(s) are issued
138+
to undo whatever styling was done at the beginning.
137139
138140
:param text: Any object compatible with str.format()
139141
:param fg: foreground color. Expects color names in FG_COLORS (e.g. 'red'). Defaults to no color.
140142
:param bg: background color. Expects color names in BG_COLORS (e.g. 'black'). Defaults to no color.
141143
:param bold: apply the bold style if True. Defaults to False.
142144
:param underline: apply the underline style if True. Defaults to False.
143-
144145
:return: the stylized string
145-
:raises ValueError if fg or bg color does cannot be found
146146
"""
147147
# List of strings that add style
148148
additions = []

docs/unfreefeatures.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ instead. These methods have these advantages:
141141
Colored Output
142142
==============
143143

144-
The output methods in the previous section all honor the ``colors`` setting,
144+
The output methods in the previous section all honor the ``allow_ansi`` setting,
145145
which has three possible values:
146146

147147
Never
@@ -152,14 +152,17 @@ Terminal
152152
(the default value) poutput(), pfeedback(), and ppaged() do not strip any
153153
ANSI escape sequences when the output is a terminal, but if the output is
154154
a pipe or a file the escape sequences are stripped. If you want colorized
155-
output you must add ANSI escape sequences, preferably using some python
156-
color library like `plumbum.colors`, `colorama`, `blessings`, or
157-
`termcolor`.
155+
output you must add ANSI escape sequences using either cmd2's internal ansi
156+
module or another color library such as `plumbum.colors`, `colorama`, or `colored`.
158157

159158
Always
160159
poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences,
161160
regardless of the output destination
162161

162+
Colored and otherwise styled output can be generated using the `ansi.style()` function:
163+
164+
.. automethod:: cmd2.ansi.style
165+
163166

164167
.. _quiet:
165168

0 commit comments

Comments
 (0)