def truncate(s, n):
    """Return s truncated to n characters."""
    if len(s) <= n:
        return s
    s2 = s[:n - 3] + f'...({len(s)})'
    if s.endswith('\n'):
        return s2 + '\n'
    return s2
