MCPcopy Create free account
hub / github.com/aws/aws-cli / center_text

Function center_text

awscli/table.py:60–80  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

58
59
60def 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
83def align_left(

Callers 2

_render_titleMethod · 0.85
_render_column_titlesMethod · 0.85

Calls 1

get_text_lengthFunction · 0.85

Tested by

no test coverage detected