Center text with specified edge chars. You can pass in the length of the text as an arg, otherwise it is computed automatically for you. This can allow you to center a string not based on it's literal length (useful if you're using ANSI codes).
(
text, length=80, left_edge='|', right_edge='|', text_length=None
)
| 58 | |
| 59 | |
| 60 | def center_text( |
| 61 | text, length=80, left_edge='|', right_edge='|', text_length=None |
| 62 | ): |
| 63 | """Center text with specified edge chars. |
| 64 | |
| 65 | You can pass in the length of the text as an arg, otherwise it is computed |
| 66 | automatically for you. This can allow you to center a string not based |
| 67 | on it's literal length (useful if you're using ANSI codes). |
| 68 | """ |
| 69 | # postcondition: get_text_length(returned_text) == length |
| 70 | if text_length is None: |
| 71 | text_length = get_text_length(text) |
| 72 | output = [] |
| 73 | char_start = (length // 2) - (text_length // 2) - 1 |
| 74 | output.append(left_edge + ' ' * char_start + text) |
| 75 | length_so_far = get_text_length(left_edge) + char_start + text_length |
| 76 | right_side_spaces = length - get_text_length(right_edge) - length_so_far |
| 77 | output.append(' ' * right_side_spaces) |
| 78 | output.append(right_edge) |
| 79 | final = ''.join(output) |
| 80 | return final |
| 81 | |
| 82 | |
| 83 | def align_left( |
no test coverage detected