import time


def wait_release(text_func, button_func, menu):
    """Calls button_func repeatedly waiting for it to return a false value
       and goes through menu list as time passes.

       The menu is a list of menu entries where each entry is a
       two element list of time passed in seconds and text to display
       for that period. Text is displayed by calling text_func(text).
       The entries must be in ascending time order."""

    start_t_ns = time.monotonic_ns()
    menu_option = None
    selected = False

    for menu_option, menu_entry in enumerate(menu):
        menu_time_ns = start_t_ns + int(menu_entry[0] * 1e9)
        menu_text = menu_entry[1]
        if menu_text:
            text_func(menu_text)
        while time.monotonic_ns() < menu_time_ns:
            if not button_func():
                selected = True
                break
        if menu_text:
            text_func("")
        if selected:
            break

    return (menu_option, (time.monotonic_ns() - start_t_ns) * 1e-9)