@staticmethod
def strip_textpad(text):
    """
        Attempt to intelligently strip excess whitespace from the output of a
        curses textpad.
        """
    if text is None:
        return text
    if '\n' not in text:
        return text.rstrip()
    stack, current_line = [], ''
    for line in text.split('\n'):
        if line.endswith('  ') or not line:
            stack.append(current_line + line.rstrip())
            current_line = ''
        else:
            current_line += line
    stack.append(current_line)
    for item in stack[::-1]:
        if not item:
            stack.pop()
        else:
            break
    out = '\n'.join(stack)
    return out
