Kotlin-style trimMargin utilities for Python, with a tiny CLI.
Project description
trimmargin ✂️📏
Kotlin-style trimMargin
and friends for Python — with a tiny CLI
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 ontextwrap.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 addnew-indent
-
trim-indent
: remove common indent viatextwrap.dedent
-
replace-indent
:dedent
thenindent
non-blank lines withnew-indent
-
prepend
: prependindent
to non-blank lines; blank-line quirk matches Kotlin:- if blank and
len(line) < len(indent)
: line becomes exactlyindent
- else: leave blank line unchanged
- if blank and
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
Algorithm | Hash digest | |
---|---|---|
SHA256 |
55c6cf2a6de1f4bfc347a0d1ed781cdea805fca6a8d7c14e1c4699876a68d2b3
|
|
MD5 |
5e019649c39ca37df6aa72fe8f99310b
|
|
BLAKE2b-256 |
694d263ed38995448d89f3a7d655fa032216b043229c8c313afef132df0bf61d
|
File details
Details for the file trimmargin-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: trimmargin-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
1b16c9243e015b0347edddd2b414fd02ba28cf7c07341923bd2035708e64f28f
|
|
MD5 |
0e776e225daa7f410cc05a3379ae7c55
|
|
BLAKE2b-256 |
26caeff650b3b19b3bb595e277717d0043bd89a85205d6362d87a9bc22d6deac
|