Return `text` left-justified in a Unicode string whose display width, in a monospaced terminal, should be at least `width` character cells. The rest of the string will be padded with `fillchar`, which must be a width-1 character. "Left" here means toward the beginning of the st
(text: str, width: int, fillchar: str = " ")
| 77 | |
| 78 | |
| 79 | def display_ljust(text: str, width: int, fillchar: str = " ") -> str: |
| 80 | """ |
| 81 | Return `text` left-justified in a Unicode string whose display width, |
| 82 | in a monospaced terminal, should be at least `width` character cells. |
| 83 | The rest of the string will be padded with `fillchar`, which must be |
| 84 | a width-1 character. |
| 85 | |
| 86 | "Left" here means toward the beginning of the string, which may actually |
| 87 | appear on the right in an RTL context. This is similar to the use of the |
| 88 | word "left" in "left parenthesis". |
| 89 | |
| 90 | >>> lines = ['Table flip', '(╯°□°)╯︵ ┻━┻', 'ちゃぶ台返し'] |
| 91 | >>> for line in lines: |
| 92 | ... print(display_ljust(line, 20, '▒')) |
| 93 | Table flip▒▒▒▒▒▒▒▒▒▒ |
| 94 | (╯°□°)╯︵ ┻━┻▒▒▒▒▒▒▒ |
| 95 | ちゃぶ台返し▒▒▒▒▒▒▒▒ |
| 96 | |
| 97 | This example, and the similar ones that follow, should come out justified |
| 98 | correctly when viewed in a monospaced terminal. It will probably not look |
| 99 | correct if you're viewing this code or documentation in a Web browser. |
| 100 | """ |
| 101 | if character_width(fillchar) != 1: |
| 102 | raise ValueError("The padding character must have display width 1") |
| 103 | |
| 104 | text_width = monospaced_width(text) |
| 105 | if text_width == -1: |
| 106 | # There's a control character here, so just don't add padding |
| 107 | return text |
| 108 | |
| 109 | padding = max(0, width - text_width) |
| 110 | return text + fillchar * padding |
| 111 | |
| 112 | |
| 113 | def display_rjust(text: str, width: int, fillchar: str = " ") -> str: |
no test coverage detected