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
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 = []