Skip to main content

ellipt

Truncate strings with an ellipsis at the start, middle or end. The limit can count code points, grapheme clusters, terminal columns or UTF-8 bytes, the marker is counted inside the limit, and the cut never splits an accent, a flag or an emoji sequence. Typed, zero dependencies, Unicode 18.0.0 tables embedded.

Install

pip install ellipt

Usage

from ellipt import ELLIPSIS, measure, shorten, shorten_path, truncate

shorten("hello world", 8)  # -> 'hello...'
shorten("hello world", 8, where="start")  # -> '...world'
shorten("hello world", 9, where="middle")  # -> 'hel...rld'
shorten("hello world", 6, marker=ELLIPSIS)  # -> 'hello…'

# Text that already fits is returned unchanged.
shorten("short", 20)  # -> 'short'

Pick the unit of the limit

family = "\U0001f468\u200d\U0001f469\u200d\U0001f467"  # one emoji, five code points
text = "ab" + family + "cd"

measure(text, "chars")  # -> 9
measure(text, "graphemes")  # -> 5
measure(text, "columns")  # -> 6
measure(text, "bytes")  # -> 22

# The family is kept whole or dropped whole, never cut into pieces.
shorten(text, 6, marker=ELLIPSIS)  # -> 'ab…'
shorten(text, 4, unit="graphemes", marker=ELLIPSIS)  # -> 'ab👨\u200d👩\u200d👧…'
shorten("你好世界", 5, unit="columns", marker=ELLIPSIS)  # -> '你好…'
shorten("café crème", 8, unit="bytes", marker="")  # -> 'café cr'

width_aware=True from 0.1.0 still works and means unit="columns".

Know what was removed

r = truncate("The quick brown fox jumps", 14, words=True, marker=ELLIPSIS)
r.text  # -> 'The quick…'
r.removed  # -> ' brown fox jumps'
r.truncated  # -> True
r.size <= r.limit  # -> True

words=True backs the cut off to whitespace; with one long word it cuts hard instead.

Shorten paths the way file browsers do

shorten_path("/home/me/projects/app/src/main.py", 20)  # -> '/.../app/src/main.py'
shorten_path("/home/me/projects/app/src/main.py", 11)  # -> '.../main.py'
shorten_path("photos/an_extremely_long_holiday_name.jpeg", 20)  # -> 'an_ext...y_name.jpeg'

Command line

$ printf 'hello world\n' | ellipt 8
hello...
$ git ls-files | ellipt 28 --path
$ ellipt 30 --where middle --ellipsis notes.txt
$ ellipt 255 --unit bytes --marker "" < titles.txt

The command reads standard input (or the files given) as UTF-8, keeps each LF or CRLF line ending, and counts terminal columns by default, expanding tabs to 8-column stops first (--tabsize). --ellipsis uses U+2026 as the marker without having to type it. ellipt --help lists every option. Errors go to standard error with exit status 1, and a reader that stops early (| head) is not an error.

Why

textwrap.shorten only cuts at the end, collapses whitespace and always cuts on words. Plain slicing (s[:n] + "...") splits emoji and accents, overflows a terminal with wide characters and can exceed a byte limit. ellipt cuts where you ask, in the unit you need, and guarantees the result is never longer than the limit. See GUIDE.md for the exact rules, the Unicode sources and the edge cases.

License

MIT. See LICENSE. The embedded Unicode data is covered by the Unicode License v3, see LICENSE-UNICODE.

Metadata

Release files for ellipt 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ellipt 0.2.1
File Size Uploaded Provenance Actions
ellipt-0.2.1.tar.gz 36.4 kB Without provenance Details for ellipt-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ellipt 0.2.1
File Interpreter ABI Platform Actions
ellipt-0.2.1-py3-none-any.whl Python 3 none any Details for ellipt-0.2.1-py3-none-any.whl

Total release size: 59.7 kB

Release files / ellipt-0.2.1.tar.gz

Download URL ellipt-0.2.1.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3d04fbd674e134e5c13627971e258e879b3475ef5788db7ff96c66fc5a294ea7
BLAKE2b-256 checksum
How to use checksums
7af05bc6f6d162f82cbe6db96987ab4012cd39679bab6ce9bbb10d842dd59c5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / ellipt-0.2.1-py3-none-any.whl

Download URL ellipt-0.2.1-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72fdbaf9c5b2b7af9de2a9ecb320a63d84b8ff4eab425fc507b9f6a49325851d
BLAKE2b-256 checksum
How to use checksums
5661458a3b89226fd0245e58fede9a60266b842bf755b2b5d8926d578ba271b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for ellipt 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page