Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

trimmargin

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trimmargin

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

pipPyPI
Version
1.0.0
Maintainers
1

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.

Keywords

text

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts