Features
snick provides a set of text manipulation functions and a Conjoiner class for building multi-line strings
incrementally. 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. Useful for triple-quoted strings inside indented code.
Example
| dedent() example | |
|---|---|
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)
If False, should_strip preserves leading and trailing newlines. Default is True.
| dedent() with should_strip=False | |
|---|---|
indent()
Indents a block of text. This is a thin wrapper around textwrap.indent() with sensible defaults.
Example
| indent() example | |
|---|---|
Parameters
prefix (str)
The string to prepend to each line. Default is " " (4 spaces).
| indent() with prefix | |
|---|---|
predicate (callable)
A function that determines which lines to indent. Only lines for which it returns True are prefixed.
| indent() with predicate | |
|---|---|
skip_first_line (bool)
If True, the first line is not indented. Default is False.
| indent() with skip_first_line | |
|---|---|
dedent_all()
Applies dedent() to each argument separately and joins them together (using conjoin().
Example
| dedent_all() example | |
|---|---|
Here is a long bit of text
as an introduction to the
following dynamic items:
--------------------------
* Item #1
* Item #2
* Item #3
Parameters
should_strip (bool)
Passed through to each dedent() call. See dedent().
join_str (str)
The string used to join the dedented parts. See conjoin().
unwrap()
Joins all lines in a block of text into a single string. Useful for long strings in deeply indented code.
Example
| unwrap() example | |
|---|---|
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
Parameters
should_strip (bool)
Passed through to the internal dedent() call. See dedent().
separator (str)
The string used to join lines after dedenting. Default is " " (a single space).
| unwrap() with separator | |
|---|---|
conjoin()
Like Python's built-in join(), but accepts items as arguments instead of requiring an iterable.
Example
| conjoin() example | |
|---|---|
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".
| conjoin() with join_str | |
|---|---|
strip_whitespace()
Removes all whitespace from a string, including newlines, tabs, and spaces. Handy for tests.
Example
| strip_whitespace() example | |
|---|---|
strip_trailing_whitespace()
Removes trailing whitespace from each line in a multi-line string.
Example
| strip_trailing_whitespace() example | |
|---|---|
strip_ansi_escape_sequences()
Removes ANSI escape sequences from text, such as those added by terminal applications for color and formatting.
Example
| strip_ansi_escape_sequences() example | |
|---|---|
here's some text with
ansi control codes added
including a few
different colors
and bold text.
indent_wrap()
Wraps a long string and indents each wrapped line.
Example
| indent_wrap() example | |
|---|---|
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
All keyword arguments are passed through to textwrap.fill(). The most commonly useful ones are:
width (int)
The maximum line width. Default is 80.
indent (int)
Number of spaces to prefix each line with. Passed to textwrap.fill() as initial_indent. Default is 4.
subsequent_indent (str)
The prefix applied to all lines after the first. indent_wrap sets this automatically to match any leading
whitespace on the input text, but you can override it explicitly.
break_long_words (bool)
If True, words longer than width are broken across lines. Default is True.
break_on_hyphens (bool)
If True, wrapping may occur on hyphens in compound words. Default is True.
pretty_format()
Formats Python data structures (dict, list, tuple, or combinations) with consistent indentation and trailing
commas, similar to json.dumps(indent=2) but for arbitrary Python objects.
Example
| pretty_format() example | |
|---|---|
It also works with types like datetime, Decimal, and custom classes:
| pretty_format() with datetime and Decimal | |
|---|---|
{
'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 is 2.
pretty_print()
Same as pretty_format() but prints to a stream instead of returning a string.
Example
| pretty_print() example | |
|---|---|
Parameters
stream (file-like)
The output stream to print to. Default is sys.stdout.
| pretty_print() with stream | |
|---|---|
indent (int)
Number of spaces per indentation level. Default is 2.
| pretty_print() with indent | |
|---|---|
enboxify()
Draws a box around a block of text.
Example
| enboxify() example | |
|---|---|
****************************
* here's some text that we *
* want to put into a box. *
* That will make it look *
* so very nice *
****************************
Parameters
boxchar (str)
The single character used to draw the box. Default is "*".
| enboxify() with boxchar | |
|---|---|
############################
# here's some text that we #
# want to put into a box. #
############################
hspace (int)
Number of spaces between the text and the box on the left and right. Default is 1.
| enboxify() with hspace | |
|---|---|
********************************
* here's some text that we *
* want to put into a box. *
********************************
vspace (int)
Number of blank lines between the text and the box on the top and bottom. Default is 0.
| enboxify() with vspace | |
|---|---|
****************************
* *
* here's some text that we *
* want to put into a box. *
* *
****************************
should_strip (bool)
Passed through to the internal dedent() call. See dedent().
Conjoiner class
Conjoiner builds multi-line strings incrementally. You add lines or blocks one at a time and convert to a
string at the end.
Basic usage
| Conjoiner basic usage | |
|---|---|
Adding multiple parts
Pass multiple arguments to add() in a single call:
| Conjoiner adding multiple parts | |
|---|---|
Automatic dedenting
By default, Conjoiner dedents each part:
| Conjoiner with automatic dedenting | |
|---|---|
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, pass should_dedent=False:
| Conjoiner preserving indentation | |
|---|---|
Adding blank lines
Insert blank lines between parts within a single add() call using blanks_between:
| Conjoiner with blanks_between | |
|---|---|
blanks_between only applies within a single add() call. Use blanks_before and blanks_after to control
spacing across calls:
| Conjoiner with blanks_before and blanks_after | |
|---|---|
For explicit control, use add_blank() or add_blanks():
| Conjoiner with add_blanks() | |
|---|---|
Customizing the join string
| Conjoiner with custom join string | |
|---|---|
Customizing blank lines
| Conjoiner with custom blank character | |
|---|---|
Practical example
Building a formatted report:
API reference
For full parameter and return type documentation, see the Reference page.