Example #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
Example #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
Example #3
0
    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 = []