16 lines
452 B
Python
16 lines
452 B
Python
from __future__ import annotations
|
|
|
|
|
|
def truncate_middle(text: str, limit: int, marker: str = "\n… middle omitted …\n") -> str:
|
|
if limit <= 0:
|
|
return ""
|
|
if len(text) <= limit:
|
|
return text
|
|
if limit <= len(marker):
|
|
return text[:limit]
|
|
remaining = limit - len(marker)
|
|
head = (remaining + 1) // 2
|
|
tail = remaining - head
|
|
suffix = text[-tail:] if tail else ""
|
|
return text[:head] + marker + suffix
|