cmd2.string_utils
cmd2.string_utils
Provides string utility functions.
This module offers a collection of string utility functions built on the Rich library. These utilities are designed to correctly handle strings with ANSI style sequences and full-width characters (like those used in CJK languages).
align
Align string to a given width.
There are convenience wrappers around this function: align_left(), align_center(), and align_right()
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to align
TYPE:
|
align
|
one of "left", "center", or "right".
TYPE:
|
width
|
Desired width. Defaults to width of the terminal.
TYPE:
|
character
|
Character to pad with. Defaults to " ".
TYPE:
|
Source code in cmd2/string_utils.py
align_left
Left-align string to a given width.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to align
TYPE:
|
width
|
Desired width. Defaults to width of the terminal.
TYPE:
|
character
|
Character to pad with. Defaults to " ".
TYPE:
|
Source code in cmd2/string_utils.py
align_center
Center-align string to a given width.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to align
TYPE:
|
width
|
Desired width. Defaults to width of the terminal.
TYPE:
|
character
|
Character to pad with. Defaults to " ".
TYPE:
|
Source code in cmd2/string_utils.py
align_right
Right-align string to a given width.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to align
TYPE:
|
width
|
Desired width. Defaults to width of the terminal.
TYPE:
|
character
|
Character to pad with. Defaults to " ".
TYPE:
|
Source code in cmd2/string_utils.py
stylize
Apply an ANSI style to a string, preserving any existing styles.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to be styled
TYPE:
|
style
|
style instance or style definition to apply.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
the stylized string |
Source code in cmd2/string_utils.py
strip_style
Strip all ANSI style sequences (colors, bold, etc.) from a string.
This targets SGR sequences specifically and leaves other terminal control codes intact.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to be stripped
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
the stripped string |
Source code in cmd2/string_utils.py
str_width
Return the display width of a string.
This is intended for single-line strings. Replace tabs with spaces before calling this.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
the string being measured
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
width of the string when printed to the terminal |
Source code in cmd2/string_utils.py
is_quoted
Check if a string is quoted.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
the string being checked for quotes
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if a string is quoted |
Source code in cmd2/string_utils.py
quote
quote_if_needed
strip_quotes
Strip outer quotes from a string.
Applies to both single and double quotes.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
string to strip outer quotes from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
same string with potentially outer quotes stripped |
Source code in cmd2/string_utils.py
norm_fold
Normalize and casefold Unicode strings for saner comparisons.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
input unicode string
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
a normalized and case-folded version of the input string |