Skip to main content

Indent text using `with` blocks

Project description

Indenter

Indenter is a Python package that assists with programmatically indenting text to arbitrary levels using with blocks.

Installation

pip install indenter

Usage

from indenter import Indenter
with Indenter() as ind:
  print(ind + "Text to be indented")

# Output:
#   Text to be indented

You can nest an arbitrary amount of calls:

from indenter import Indenter
ind = Indenter()
with ind:
  print(ind + "I'm one level deep")
  with ind:
    print(ind + "I'm two levels deep")
  print(ind + "I'm one level deep again")

# Output:
#   I'm one level deep
#     I'm two levels deep
#   I'm one level deep again

The default indentation symbol is two spaces. You can override this by passing symbol:

from indenter import Indenter
# Indent with 4 spaces per level
with Indenter(symbol="    ") as ind:
  print(ind + "I'm indented by four spaces")

# Indent with tabs
with Indenter(symbol="\t") as ind:
  print(ind + "I'm indented by one tab")

# Output:
#     I'm indented by four spaces
# 	I'm indented by one tab

When using custom symbols, nested withs will inherit the symbol of their parent. If you need more than one type of indentation at once, you must make and manage multiple Indenters.

The indenter starts at zero levels of indentation by default, and increases by one level for each with block. This can be overridden to start at other levels, such as 1:

from indenter import Indenter
ind = Indenter(start=1)
print(ind + "First-level indentation)
with ind:
  print(ind + "Second-level indentation")

# Output:
#   First-level indentation
#     Second-level indentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

indenter-1.0.0.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

indenter-1.0.0-py3-none-any.whl (3.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page