Skip to main content

Kotlin-style trimMargin utilities for Python, with a tiny CLI.

Project description

trimmargin Logo

trimmargin ✂️📏

Kotlin-style trimMargin and friends for Python — with a tiny CLI

PyPI Python Versions PyPI Status License: MIT Downloads


Features

  • 🔄 Normalizes line endings to \n
  • 🧼 Drops first/last blank lines (edges only, not interior)
  • 🧲 trim_margin() strips leading whitespace + a margin prefix (default |)
  • 🔧 replace_indent_by_margin() like Kotlin’s, with a custom replacement indent
  • 🪄 trim_indent() / replace_indent() built on textwrap.dedent/indent
  • prepend_indent() behaves like Kotlin (blank-line quirk covered)

Installation

# uv (recommended)
uv pip install trimmargin

# or pip
pip install trimmargin

Quick Start

from trimmargin import trim_margin, replace_indent_by_margin

text = """
    |hello
    |world
"""

print(trim_margin(text))
# hello
# world

print(replace_indent_by_margin(text, new_indent=">>> "))
# >>> hello
# >>> world

Behavior Notes

  • If a line’s first non-whitespace char starts the margin prefix, both the leading whitespace and the prefix are removed; other lines are unchanged.
  • Only the first and last blank lines are dropped.
  • Input may use \r\n, \n, or \r; output uses \n.

CLI Usage 🚀

The trimmargin command reads a file or stdin and writes to stdout.

# default: trim-margin on stdin using prefix "|"
trimmargin < input.txt

# file input
trimmargin input.txt

# modes
trimmargin --mode replace-by-margin --new-indent ">>> " input.txt
trimmargin --mode trim-indent < input.txt
trimmargin --mode replace-indent --new-indent "  " < input.txt
trimmargin --mode prepend --indent ">> "

# change margin prefix
trimmargin --prefix "§" < input.txt

# version
trimmargin --version

Modes

  • trim-margin (default): remove leading whitespace + prefix

  • replace-by-margin: same detection as above, then add new-indent

  • trim-indent: remove common indent via textwrap.dedent

  • replace-indent: dedent then indent non-blank lines with new-indent

  • prepend: prepend indent to non-blank lines; blank-line quirk matches Kotlin:

    • if blank and len(line) < len(indent): line becomes exactly indent
    • else: leave blank line unchanged

Development (uv) 🧑‍💻

# setup
uv sync --extra dev

# tests
uv run pytest

# lint & types
uv run ruff check .
uv run mypy src

# format
uv run ruff format .

# build & publish
uv build
uv publish

Why This Exists ❓

textwrap.dedent/indent are great, but Kotlin’s margin-aware trimming is handy for multiline literals embedded in code and docs.

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

trimmargin-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trimmargin-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file trimmargin-1.0.0.tar.gz.

File metadata

  • Download URL: trimmargin-1.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for trimmargin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 55c6cf2a6de1f4bfc347a0d1ed781cdea805fca6a8d7c14e1c4699876a68d2b3
MD5 5e019649c39ca37df6aa72fe8f99310b
BLAKE2b-256 694d263ed38995448d89f3a7d655fa032216b043229c8c313afef132df0bf61d

See more details on using hashes here.

File details

Details for the file trimmargin-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trimmargin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b16c9243e015b0347edddd2b414fd02ba28cf7c07341923bd2035708e64f28f
MD5 0e776e225daa7f410cc05a3379ae7c55
BLAKE2b-256 26caeff650b3b19b3bb595e277717d0043bd89a85205d6362d87a9bc22d6deac

See more details on using hashes here.

Supported by

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