Exemplo n.º 1
0
 def get_completions(self, document, complete_event):
     text = document.text
     res = re.search(r'(\b\w+)(\.)?(\b\w+)?$', text)
     if res:
         word1, dot, word2 = res.group(1,2,3)
         if word1 and dot and not word2 and word1 in valid_types:
             yield Completion('*', start_position=0)
         elif word1 and not word2:
             possible_completions = [
                 tup[1]
                 for tup in get_best_matches(word1, valid_types, top_n=15, case_sensitive=False, include_percentage=True)
             ]
             possible_completions = sorted(possible_completions
                 , key=lambda x: (
                     0 if x.lower().startswith(word1.lower()) else 
                     1 if x.lower() in text
                     else 2
             ))[:7]
             for item in possible_completions:
                 yield Completion(item, start_position=-len(word1))
         elif word1 and dot and word2:
             folder = valid_types.get(word1, None)
             if folder:
                 files = self.files_by_folder(f'/Users/daniel.hicks_1/Documents/Tower/liveNationSFDC PSDEV/src/' + folder)
                 possible_completions = [
                     self.remove_file_extension(tup[1])
                     for tup in get_best_matches(word2, files, top_n=7, case_sensitive=False, include_percentage=True)
                 ]
                 for item in possible_completions:
                     yield Completion(item, start_position=-len(word2))
Exemplo n.º 2
0
def _possible_completions(text, picklist):
    possible_completions = [
        s for s in get_best_matches(
            text, picklist, top_n=15, case_sensitive=False)
    ]
    possible_completions = sorted(
        possible_completions,
        key=lambda x: ((1 if x.lower().startswith(text.lower()) else 0) +
                       (1 if x in text else 0)),
        reverse=True)[:7]
    return possible_completions
Exemplo n.º 3
0
 def get_completions(self, document, complete_event):
     text = document.text
     possible_completions = [
         tup[1] for tup in get_best_matches(text,
                                            self.expected_values,
                                            top_n=15,
                                            case_sensitive=False,
                                            include_percentage=True)
     ]
     possible_completions = sorted(
         possible_completions,
         key=lambda x: ((1 if x.lower().startswith(text.lower()) else 0) +
                        (1 if x in text else 0)),
         reverse=True)[:7]
     for s in possible_completions:
         yield Completion(s, start_position=-len(text))
Exemplo n.º 4
0
def prompt(text,
           options=None,
           multiselect=False,
           boolean=False,
           expected=None,
           expected_type=None,
           call_options=False,
           coded_input=None,
           validator: Validator = None):
    assert not (
        options and expected
    ), 'Cannot specify both the "options" and "expected" parameters in the same function call'
    assert not (options is None and expected is None and multiselect
                ), 'Cannot spectify "multiselect" without providing options'
    if expected_type and options:
        uncastable = _get_uncastable(options, expected_type)
        assert not uncastable, f'If specifying an expected type, all provided options must be castable to that type.\nInvalid values: {uncastable}'
    assert expected is None or callable(expected) or isinstance(
        expected,
        (dict, list, tuple, set
         )), 'The "expected" value passed must be a dict, list, tuple, or set'

    if boolean: expected_type = bool

    while 1:
        try:
            response = None
            if coded_input is not None:
                response = coded_input
            elif options is not None and len(options) == 1:
                printc('cyan', f'Auto-selecting: "{list(options)[0]}"')
                response = '1'

            if response is None:
                msg = ANSI(_print_message(text, boolean, options))
                # print(msg)
                # response = input()
                kwargs = {}

                completer = ExpectedCompleter(
                    expected) if expected and not callable(expected) else None
                validate_while_typing, validatorinner = _prompt_validator(
                    response, options, multiselect, boolean, expected,
                    expected_type, call_options, coded_input, validator)
                response = prompt_toolkit_prompt(
                    msg,
                    validator=validatorinner,
                    validate_while_typing=validate_while_typing,
                    mouse_support=False,
                    key_bindings=cancellation_binding,
                    completer=completer,
                    **kwargs)
                if isinstance(response, Exception):
                    raise response
            return _get_return_value(response, options, multiselect, boolean,
                                     expected, expected_type, call_options,
                                     coded_input)
        except (ResponseInvalidDatatype, ValueError) as e:
            printc('red', 'Could not parse input, please try again.')
            printc('red', f'Expected type: {expected_type}')
        except ResponseNotMatchesExpectedLambda as e:
            printc('red',
                   f'Invalid input, please try again. Input: {response}')
            printc('red', f'Lambda function: \n{inspect.getsource(expected)}')
        except (ResponseNotInRange, ResponseNotInOptions) as e:
            printc('red', 'Response not in expected range, please try again.')
        except CalledFunctionError as e:
            printc('red', f'A function call failed within the options:\n{e}')
        except ResponseNotInExpectedList as e:
            if get_best_matches and expected:
                best_matches = get_best_matches(
                    response, expected, top_n=4,
                    case_sensitive=False) + ['None of the above']
                response2 = prompt('Did you mean one of the following?',
                                   options=best_matches)
                if response2 != 'None of the above':
                    return prompt('',
                                  options=options,
                                  multiselect=multiselect,
                                  boolean=boolean,
                                  expected=expected,
                                  expected_type=expected_type,
                                  call_options=call_options,
                                  coded_input=response2)
            printc('red', f'"{response}" not a valid input. Expected inputs:')
            print(f'{", ".join([str(s) for s in expected])}')
        except PromptCancelled as e:
            raise PromptCancelled()