If a string is long enough, and has at least 3 dots, replace the middle part with ellipses. If a string naming a file is long enough, and has at least 3 slashes, replace the middle part with ellipses. If three consecutive dots, or two consecutive dots are encountered these are
(string: str, *, min_elide)
| 29 | |
| 30 | |
| 31 | def _elide_point(string: str, *, min_elide) -> str: |
| 32 | """ |
| 33 | If a string is long enough, and has at least 3 dots, |
| 34 | replace the middle part with ellipses. |
| 35 | |
| 36 | If a string naming a file is long enough, and has at least 3 slashes, |
| 37 | replace the middle part with ellipses. |
| 38 | |
| 39 | If three consecutive dots, or two consecutive dots are encountered these are |
| 40 | replaced by the equivalents HORIZONTAL ELLIPSIS or TWO DOT LEADER unicode |
| 41 | equivalents |
| 42 | """ |
| 43 | string = string.replace('...','\N{HORIZONTAL ELLIPSIS}') |
| 44 | string = string.replace('..','\N{TWO DOT LEADER}') |
| 45 | if len(string) < min_elide: |
| 46 | return string |
| 47 | |
| 48 | object_parts = string.split('.') |
| 49 | file_parts = string.split(os.sep) |
| 50 | if file_parts[-1] == '': |
| 51 | file_parts.pop() |
| 52 | |
| 53 | if len(object_parts) > 3: |
| 54 | return "{}.{}\N{HORIZONTAL ELLIPSIS}{}.{}".format( |
| 55 | object_parts[0], |
| 56 | object_parts[1][:1], |
| 57 | object_parts[-2][-1:], |
| 58 | object_parts[-1], |
| 59 | ) |
| 60 | |
| 61 | elif len(file_parts) > 3: |
| 62 | return ("{}" + os.sep + "{}\N{HORIZONTAL ELLIPSIS}{}" + os.sep + "{}").format( |
| 63 | file_parts[0], file_parts[1][:1], file_parts[-2][-1:], file_parts[-1] |
| 64 | ) |
| 65 | |
| 66 | return string |
| 67 | |
| 68 | |
| 69 | def _elide_typed(string: str, typed: str, *, min_elide: int) -> str: |