def wrap(text, width=78):
    i = 0
    while i < len(text):
        if i + width + 1 > len(text):
            i = len(text)
        else:
            findnl = text.find('\n', i)
            findspc = text.rfind(' ', i, i+width+1)
            if findspc != -1:
                if findnl != -1 and findnl < findspc:
                    i = findnl + 1
                else:
                    text = text[:findspc] + '\n' + text[findspc+1:]
                    i = findspc + 1
            else:
                findspc = text.find(' ', i)
                if findspc != -1:
                    text = text[:findspc] + '\n' + text[findspc+1:]
                    i = findspc + 1
    return text
Wraps text at a specified width.
dataset/ETHPy150Open jpm/papercut/storage/strutil.py/wrap