'Update segments lengths and return them'

def _render_length(self, theme, segments, divider_widths):

    segments_len = len(segments)
    ret = 0
    divider_spaces = theme.get_spaces()
    prev_segment = theme.EMPTY_SEGMENT
    try:
        first_segment = next(iter((segment for segment in segments if (not segment[u'literal_contents'][1]))))
    except StopIteration:
        first_segment = None
    try:
        last_segment = next(iter((segment for segment in reversed(segments) if (not segment[u'literal_contents'][1]))))
    except StopIteration:
        last_segment = None
    for (index, segment) in enumerate(segments):
        side = segment[u'side']
        segment_len = segment[u'_contents_len']
        if (not segment[u'literal_contents'][1]):
            if (side == u'left'):
                if (segment is not last_segment):
                    compare_segment = next(iter((segment for segment in segments[(index + 1):] if (not segment[u'literal_contents'][1]))))
                else:
                    compare_segment = theme.EMPTY_SEGMENT
            else:
                compare_segment = prev_segment
            divider_type = (u'soft' if (compare_segment[u'highlight'][u'bg'] == segment[u'highlight'][u'bg']) else u'hard')
            outer_padding = (int(bool(((segment is first_segment) if (side == u'left') else (segment is last_segment)))) * theme.outer_padding)
            draw_divider = segment[((u'draw_' + divider_type) + u'_divider')]
            segment_len += outer_padding
            if draw_divider:
                segment_len += (divider_widths[side][divider_type] + divider_spaces)
            prev_segment = segment
        segment[u'_len'] = segment_len
        ret += segment_len
    return ret
