MCPcopy
hub / github.com/rspeer/python-ftfy / display_rjust

Function display_rjust

ftfy/formatting.py:113–139  ·  view source on GitHub ↗

Return `text` right-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. "Right" here means toward the end of the string

(text: str, width: int, fillchar: str = " ")

Source from the content-addressed store, hash-verified

111
112
113def display_rjust(text: str, width: int, fillchar: str = " ") -> str:
114 """
115 Return `text` right-justified in a Unicode string whose display width,
116 in a monospaced terminal, should be at least `width` character cells.
117 The rest of the string will be padded with `fillchar`, which must be
118 a width-1 character.
119
120 "Right" here means toward the end of the string, which may actually be on
121 the left in an RTL context. This is similar to the use of the word "right"
122 in "right parenthesis".
123
124 >>> lines = ['Table flip', '(╯°□°)╯︵ ┻━┻', 'ちゃぶ台返し']
125 >>> for line in lines:
126 ... print(display_rjust(line, 20, '▒'))
127 ▒▒▒▒▒▒▒▒▒▒Table flip
128 ▒▒▒▒▒▒▒(╯°□°)╯︵ ┻━┻
129 ▒▒▒▒▒▒▒▒ちゃぶ台返し
130 """
131 if character_width(fillchar) != 1:
132 raise ValueError("The padding character must have display width 1")
133
134 text_width = monospaced_width(text)
135 if text_width == -1:
136 return text
137
138 padding = max(0, width - text_width)
139 return fillchar * padding + text
140
141
142def display_center(text: str, width: int, fillchar: str = " ") -> str:

Callers

nothing calls this directly

Calls 2

character_widthFunction · 0.85
monospaced_widthFunction · 0.85

Tested by

no test coverage detected