Beispiel #1
0
    def _on_command(self, instance: AdvancedTextInput) -> bool:
        """"Privated function fired when self.commandText.on_text_validate is
        called, in other words, when users hit the 'enter' key. This property
        is established by res/sira.kv.

        [ensures]:  instance.last_row > 0
                    instance.protected_len <= len(instance._lines[instance.last_row])
                    instance.password_mode = False
                    instance.history_stack.traversal =
                        instance.history_stack.traversal_dummy
        [calls]:    instance.history_stack.reset_traversal
                    self.controller.processInput
                    instance.on_cursor
        """
        string = instance.text[instance.last_row_start +
                               instance.protected_len:]
        if instance.password_mode:
            string = instance.password_cache
            instance.password_mode = False
        elif instance.command_mode and string != "":
            instance.history_stack.push(string)
        instance.history_stack.reset_traversal()
        self.controller.processInput(string)
        instance.on_cursor(instance, instance.cursor)
        return True
    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
Beispiel #3
0
    def _stop_interaction(self, instance: AdvancedTextInput) -> None:
        """Private function to interrupt interactive mode. This function will
        be fired when the user hit control-C.

        [ensures]:  instance.password_mode = False
                    instance.command_mode = True
        [calls]:    self.controller.closeinteractive
        """
        if not instance.command_mode:
            if instance.completion_mode:
                if "_stop_completion" not in dir(self):
                    self._stop_completion = lambda x: x
                self._stop_completion(instance)
            self.controller.closeinteractive()
            instance.password_mode = False
            instance.command_mode = True
    def _stop_completion(self, instance: AdvancedTextInput) -> None:
        """Private function to stop completion mode.

        [requires]: instance.completion_mode
        [ensures]:  not instance.completion_mode
                    self.tab_index = -1
                    self.option = []
                    self.start_indices = []
        [calls]:    self._clear_options
        """
        if not asserts(instance.completion_mode, "Not in completion mode"):
            return

        self._clear_options(instance)
        instance.completion_mode = False
        instance._reset_last_line()
        instance.on_cursor(instance, instance.cursor)
        self.tab_index = -1
        self.option = []
        self.start_indices = []
    def _on_reduce_option(self, instance: AdvancedTextInput) -> bool:
        """Private function to reduce options based on user input. This function
        is fired when self.commandText.on_reduce_option is called.

        [requires]: instance.completion_mode
        [ensures]:  self.option = [opt for all opt in $self.option if
                    opt.lower().startswith(
                        instance.text[self.completion_start:end].lower()
                    )]
        [calls]:    self._stop_completion
        """
        if not asserts(instance.completion_mode, "Not in completion mode"):
            return True

        copy = list()
        instance.do_cursor_movement("cursor_end", control=False)
        end = instance.cursor_index(instance.cursor)
        word_truc = instance.text[self.completion_start:end]
        copy = [
            opt for opt in self.option
            if opt.lower().startswith(word_truc.lower())
        ]
        self._stop_completion(instance)
        self.option = copy
    def _clear_options(self, instance: AdvancedTextInput) -> None:
        """Private function to clear all displayed options in self.commandText.

        [requires]: instance.completion_mode
        [ensures]:  len(self.commandText._lines) - 1 = self.commandText.last_row
                    [erase all text below self.commandText.last_row]
        """
        if not asserts(instance.completion_mode, "Not in completion mode"):
            return

        instance.cancel_selection()
        start = instance.text.rindex('\n')
        end = len(instance.text)
        if end < start:
            import pdb
            pdb.set_trace()
        instance.select_text(start, end)
        instance.delete_selection()
    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