コード例 #1
0
    def _on_space(self, instance: AdvancedTextInput) -> bool:
        """Private function to start completion mode by entering from space.
        This function is fired when self.commandText._on_space is called.

        [ensures]:  (when this is the first space after a user-input word)
                    self.from_space
                    (otherwise, do nothing)
        [calls]:    (when this is the first space after a user-input word)
                    self.controller.auto_complete
                    (otherwise, do nothing)
        """
        if instance.completion_mode:
            cursor = instance.cursor
            self._stop_completion(instance)
            instance.cursor = cursor
            return True
        if not self.space_completion:
            return True
        c_index = instance.cursor_index(instance.cursor) - 1
        if instance.last_row_start + instance.protected_len != c_index\
                and instance.text[c_index - 1] != " ":
            string = instance.text[instance.last_row_start +
                                   instance.protected_len:]
            self.from_space = True
            self.controller.auto_complete(string)
        return True
コード例 #2
0
    def _display_options(self, instance: AdvancedTextInput, behavior: str,
                         option: list) -> bool:
        """Private function to display options under self.commandText.last_row.
        This function is fired when self.commandText.on_next_options or
        self.commandText.on_prev_options is called. Essentially, this function
        will display options based on behavior and calculate start indices of
        displayed options. According to behavior, there are three scenarios:
            [Scenario 1]: When behavior == "init", the function will display the
                first n (n = max(self.options_per_line, [the number of remaining
                options])) options;
            [Scenario 2]: When behavior == "next", the function will display the
                next n options;
            [Scenario 3]: When behavior == "prev", the function will display the
                previous n options.

        [ensures]:  [display n options below self.commandText.last_row]
                    self.tab_index = -1
                    [convention #3.1]
        """
        # display options based on behavior
        instance.do_cursor_movement("cursor_end", control=True)
        cursor = instance.cursor
        if behavior == "init":
            self.page_index = 0
        else:
            self._clear_options(instance)
            instance.completion_mode = False
            self.tab_index = -1
            next_index = self.page_index + self.options_per_line\
                if behavior == "next"\
                else self.page_index - self.options_per_line
            self.page_index = max(0, next_index)\
                if next_index < len(option)\
                else self.page_index
        end_index = self.page_index + self.options_per_line\
            if self.page_index + self.options_per_line <= len(option)\
            else len(option)
        instance.insert_text("\n" +
                             " ".join(option[self.page_index:end_index]))
        instance.completion_mode = True
        instance.cursor = cursor
        instance._reset_last_line()
        instance.on_cursor(instance, instance.cursor)
        # calc start indices of displayed options
        index = 0
        self.start_indices.clear()
        for s in option[self.page_index:end_index]:
            self.start_indices.append(index)
            index += len(s) + 1
        return True