File size: 507 Bytes
26ff0b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from __future__ import annotations

__all__ = ["limit_chars"]


def limit_chars(text: str, limit: int = 10_000) -> str:
    """Return at most ``limit`` characters from ``text``.

    Earlier characters are stripped when the text exceeds the limit.
    A short notice is prepended indicating the amount removed.
    """
    text = text.strip()
    if len(text) <= limit:
        return text

    truncated = len(text) - limit
    return f"(output truncated, {truncated} characters hidden)\n{text[-limit:]}"