def get_chars_within_width(self):
        """Return chars for label with max width."""
        ### get the max width minus double the padding (to
        ### compensate for the left and right padding
        max_width = self.max_width - (self.padding * 2)

        ### get the available width for characters by
        ### removing the width taken by the ellipsis
        available_width = max_width - self.ellipsis_width

        ### get the chars and combine with the ellipsis
        ### according to the "ellipsis_at_end" flag

        ## define variables needed to perform calculations

        # an accumulator to store the total width used by
        # the character used as we add them one by one
        width_amount = 0

        # a string to store each character as we add
        # them one by one until we use all the available
        # width
        chars = ""

        # the string representing the ellipsis itself
        ellipsis = "..."

        ## reference the char map locally
        width_map = self.width_map

        ## now iterate over the contents, adding forming
        ## the final string containing the characters and
        ## the ellipsis

        if self.ellipsis_at_end:

            for char in self.contents:

                # calculate the resulting width from
                # adding the character

                char_width = width_map[char]
                resulting_width = width_amount + char_width

                # if the resulting width surpasses the
                # available width, we break out of the
                # loop
                if resulting_width > available_width:
                    break

                # otherwise it is ok to add the character
                # to the text; we also update the
                # amount of width used
                else:
                    chars += char
                    width_amount = resulting_width

            chars = chars + ellipsis

        else:

            for char in self.contents[::-1]:

                # calculate the resulting width from
                # adding the character

                char_width = width_map[char]
                resulting_width = width_amount + char_width

                # if the resulting width surpasses the
                # available width, we break out of the
                # loop
                if resulting_width > available_width:
                    break

                # otherwise it is ok to add the character
                # to the text; we also update the
                # amount of width used
                else:
                    chars = char + chars
                    width_amount = resulting_width

            chars = ellipsis + chars

        ### finally return the characters
        return chars
Return chars for label with max width.
nodezator/textman/label/main.py