'Internal segment rendering method.
This method loops through the segment array and compares the
foreground/background colors and divider properties and returns the
rendered statusline as a string.
The method always renders the raw segment contents (i.e. without
highlighting strings added), and only renders the highlighted
statusline if render_highlighted is True.'

def _render_segments(self, theme, segments, render_highlighted=True):

    segments_len = len(segments)
    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']
        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
            outer_padding = ((int(bool(((segment is first_segment) if (side == u'left') else (segment is last_segment)))) * theme.outer_padding) * u'    ')
            divider_type = (u'soft' if (compare_segment[u'highlight'][u'bg'] == segment[u'highlight'][u'bg']) else u'hard')
            divider_highlighted = u''
            contents_raw = segment[u'contents']
            contents_highlighted = u''
            draw_divider = segment[((u'draw_' + divider_type) + u'_divider')]
            if draw_divider:
                divider_raw = self.escape(theme.get_divider(side, divider_type))
                if (side == u'left'):
                    contents_raw = ((outer_padding + contents_raw) + (divider_spaces * u'    '))
                else:
                    contents_raw = (((divider_spaces * u'    ') + contents_raw) + outer_padding)
                if (divider_type == u'soft'):
                    divider_highlight_group_key = (u'highlight' if (segment[u'divider_highlight_group'] is None) else u'divider_highlight')
                    divider_fg = segment[divider_highlight_group_key][u'fg']
                    divider_bg = segment[divider_highlight_group_key][u'bg']
                else:
                    divider_fg = segment[u'highlight'][u'bg']
                    divider_bg = compare_segment[u'highlight'][u'bg']
                if (side == u'left'):
                    if render_highlighted:
                        contents_highlighted = self.hl(self.escape(contents_raw), **segment[u'highlight'])
                        divider_highlighted = self.hl(divider_raw, divider_fg, divider_bg, False)
                    segment[u'_rendered_raw'] = (contents_raw + divider_raw)
                    segment[u'_rendered_hl'] = (contents_highlighted + divider_highlighted)
                else:
                    if render_highlighted:
                        divider_highlighted = self.hl(divider_raw, divider_fg, divider_bg, False)
                        contents_highlighted = self.hl(self.escape(contents_raw), **segment[u'highlight'])
                    segment[u'_rendered_raw'] = (divider_raw + contents_raw)
                    segment[u'_rendered_hl'] = (divider_highlighted + contents_highlighted)
            else:
                if (side == u'left'):
                    contents_raw = (outer_padding + contents_raw)
                else:
                    contents_raw = (contents_raw + outer_padding)
                contents_highlighted = self.hl(self.escape(contents_raw), **segment[u'highlight'])
                segment[u'_rendered_raw'] = contents_raw
                segment[u'_rendered_hl'] = contents_highlighted
            prev_segment = segment
        else:
            segment[u'_rendered_raw'] = (u'    ' * segment[u'literal_contents'][0])
            segment[u'_rendered_hl'] = segment[u'literal_contents'][1]
        (yield segment)
