Features
snick provides a comprehensive set of text manipulation functions and a powerful Conjoiner class for constructing multi-line strings. All functions accept additional keyword arguments to customize their behavior.
Core Functions
dedent()
Unindents a block of text by aligning all lines with the leftmost non-whitespace character. This is especially useful for triple-quoted strings in your code:
| dedent() example | |
|---|---|
Output:
Here is some text
here is some other text
we don't want this indented
when it's printed
(to the console)
Parameters:
should_strip(bool): IfFalse, preserves leading and trailing newlines. Default isTrue.
| dedent() with should_strip=False | |
|---|---|
Output (with blank lines preserved):
indent()
Indents a block of text. This is a thin wrapper around textwrap.indent() with sensible defaults:
| indent() example | |
|---|---|
Output:
Parameters:
prefix(str): The string to prepend to each line. Default is" "(4 spaces).predicate(callable): A function to determine which lines to indent.skip_first_line(bool): IfTrue, the first line is not indented. Default isFalse.
| indent() with skip_first_line | |
|---|---|
Output:
dedent_all()
Applies dedent() to each argument separately and joins them together:
| dedent_all() example | |
|---|---|
Output:
Here is a long bit of text
as an introduction to the
following dynamic items:
--------------------------
* Item #1
* Item #2
* Item #3
unwrap()
Unwraps a block of text by joining all lines into a single string. Useful for long strings in deeply indented code:
| unwrap() example | |
|---|---|
Output:
I need to have a very long string here, but it would go way outside of the line length limit and cause me all sorts of grief with the style checker. So, unwrap can help me here
conjoin()
Like Python's built-in join(), but accepts items as arguments instead of requiring an iterable:
| conjoin() example | |
|---|---|
Output:
Here are some lines
that I would like to join
and it would be silly
to have to wrap them in a
list instead of just passing
them as plain old arguments
Parameters:
join_str(str): The string used to join the arguments. Default is"\n".
strip_whitespace()
Removes all whitespace from a string, including newlines, tabs, and spaces. Handy for tests:
| strip_whitespace() example | |
|---|---|
Output:
strip_trailing_whitespace()
Removes trailing whitespace from each line in a multi-line string:
| strip_trailing_whitespace() example | |
|---|---|
Output:
strip_ansi_escape_sequences()
Removes ANSI escape sequences from text. These are commonly added by terminal applications for color and formatting:
| strip_ansi_escape_sequences() example | |
|---|---|
Output:
indent_wrap()
Wraps a long string and indents each wrapped line:
| indent_wrap() example | |
|---|---|
Output:
Here's some filler text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
Parameters:
width(int): The maximum width of each line. Default is80.indent(int): Number of spaces to indent. Default is4.
pretty_format()
Formats Python data structures (dict, list, tuple, or combinations) with clean indentation and trailing commas:
| pretty_format() example | |
|---|---|
Output:
Works with exotic types like datetime, Decimal, and custom classes:
| pretty_format() with datetime and Decimal | |
|---|---|
Output:
{
'timestamp': datetime.datetime(2026, 1, 22, 20, 15, 30),
'amount': Decimal('123.45'),
'active': True,
'notes': None,
}
Parameters:
indent(int): Number of spaces per indentation level. Default is2.
pretty_print()
Same as pretty_format() but prints to a stream instead of returning a string:
| pretty_print() example | |
|---|---|
Parameters:
stream(file-like): The output stream. Default issys.stdout.indent(int): Number of spaces per indentation level. Default is2.
enboxify()
Draws a box around text. Great for making log messages stand out:
| enboxify() example | |
|---|---|
Output:
****************************
* here's some text that we *
* want to put into a box. *
* That will make it look *
* so very nice *
****************************
Parameters:
width(int): The width of the box. If not specified, auto-calculates based on content.box_char(str): The character to use for the box. Default is"*".
Conjoiner Class
The Conjoiner class provides a convenient way to incrementally construct multi-line strings. It's especially useful for complex text output that's generated programmatically.
Basic Usage
| Conjoiner basic usage | |
|---|---|
Output:
Adding Multiple Parts
Add multiple parts in a single call:
| Conjoiner adding multiple parts | |
|---|---|
Automatic Dedenting
By default, Conjoiner dedents each part:
| Conjoiner with automatic dedenting | |
|---|---|
Output:
This text is indented in the code
but will be dedented in the output
Same with this text
it looks nice in the code
To preserve indentation, use should_dedent=False:
| Conjoiner preserving indentation | |
|---|---|
Adding Blank Lines
Using blanks_between
Insert blank lines between parts within a single add() call:
| Conjoiner with blanks_between | |
|---|---|
Output:
Note: blanks_between only applies within a single add() call. Multiple add() calls won't insert blanks by default.
Using blanks_before and blanks_after
| Conjoiner with blanks_before and blanks_after | |
|---|---|
Output:
Using add_blank() and add_blanks()
For explicit control:
| Conjoiner with add_blanks() | |
|---|---|
Output:
Customizing Join String
| Conjoiner with custom join string | |
|---|---|
Output:
Customizing Blank Lines
| Conjoiner with custom blank character | |
|---|---|
Output:
Practical Example
Building a formatted report:
Output:
API Reference
For detailed API documentation including all parameters and return types, see the Reference page.