Wrap all termui functions with a custom decorator.

def patch_ui_functions(wrapper):
    '''Wrap all termui functions with a custom decorator.'''
    NONE = object()
    import click

    saved = []

    for name, info in sorted(_ui_functions.items()):
        f = getattr(click, name, NONE)
        if f is NONE:
            continue

        new_f = wrapper(_copy_fn(f), info)

        argspec = getargspec(f)
        signature = inspect.formatargspec(*argspec) \
            .lstrip('(') \
            .rstrip(')')
        args = ', '.join(arg.split('=')[0].split(':')[0].strip()
                         for arg in signature.split(','))

        stub_f = eval('lambda {s}: {n}._real_click_fn({a})'
                      .format(n=f.__name__, s=signature, a=args))

        if PY2:
            saved.append((f, f.func_code))
            f.func_code = stub_f.func_code
        else:
            saved.append((f, f.__code__))
            f.__code__ = stub_f.__code__

        f._real_click_fn = new_f

    try:
        yield
    finally:
        for f, code in saved:
            if PY2:
                f.func_code = code
            else:
                f.__code__ = code

            del f._real_click_fn