示例#1
0
def choose_build_type_with_screen(
        screen: Screen, build_type_options: List[BuildTypeOption]
) -> Optional[BuildTypeDecision]:
    '''
	TODOCUMENT

	:param screen             : TODOCUMENT
	:param build_type_options : TODOCUMENT
	'''
    # Seem to need to do this to prevent the first printed line always using white foreground color
    screen.print_at(
        '.',
        0,
        0,
    )
    screen.refresh()
    screen.print_at(
        ' ',
        0,
        0,
    )

    build_types = [x.build_type for x in build_type_options]

    active_index = 0
    cmake_it = False
    if 'BUILDTYPE' in os.environ and os.environ['BUILDTYPE'] in build_types:
        active_index = build_types.index(os.environ['BUILDTYPE'])

    # Run the event loop
    while True:

        # Print the build_type_options
        for option_idx, build_type_option in enumerate(build_type_options):
            max_build_type_len = max(len(x) for x in build_types)
            is_active = option_idx == active_index
            screen.print_at(
                (f' { build_type_option.build_type:{max_build_type_len}}      '
                 + (' ' if build_type_option.presence == BuildPresence.ABSENT
                    else '?' if build_type_option.presence
                    == BuildPresence.HAS_DIR else u'\u2713') +
                 (' ' if build_type_option.is_standard else
                  '   [not-in-standard-list] ')),
                x=4,
                y=option_idx,
                bg=(COLOUR_BRIGHT_RED if cmake_it else Screen.COLOUR_WHITE)
                if is_active else Screen.COLOUR_BLACK,
                colour=Screen.COLOUR_BLACK
                if is_active else Screen.COLOUR_WHITE,
            )

        for y_val, text in enumerate(
            ('', '[up/down]  : change selection', '[enter]    : select',
             '[spacebar] : toggle whether to also (re)run conan/cmake (indicated by red)',
             '[q]        : quit with no change', '', u'\u2713' +
             ' : ninja file exists in directory', '? : directory exists'),
                len(build_type_options)):
            screen.print_at(
                text,
                x=0,
                y=y_val,
            )

        # Get any event and respond to relevant keys
        event = screen.get_event()
        if isinstance(event, KeyboardEvent):
            if event.key_code == ord("\n"):
                return BuildTypeDecision(
                    build_type=build_type_options[active_index].build_type,
                    cmake_it=cmake_it)
            if event.key_code in (Screen.KEY_ESCAPE, ord('Q'), ord('q')):
                return None
            if event.key_code == Screen.KEY_DOWN:
                active_index = active_index + 1 if active_index + 1 < len(
                    build_type_options) else 0
            elif event.key_code == Screen.KEY_UP:
                active_index = active_index - 1 if active_index > 0 else len(
                    build_type_options) - 1
            elif event.key_code == ord(' '):
                cmake_it = not cmake_it

        # Refresh screen
        screen.refresh()