Skip to content

Suggestions

Suggestions

Bases: OptionList

Widget for displaying the suggestion for auto-completions as a pop-up.

Source code in src/textual_shell/widgets/shell/suggestions.py
class Suggestions(OptionList):
    """Widget for displaying the suggestion for auto-completions as a pop-up."""

    class FocusChange(Message):
        """
        A message for when the prompt input 
        has either gained or lost focus.
        """
        def __init__(self, is_focused: bool):
            super().__init__()
            self.is_focused = is_focused


    class Cycle(Message):
        """
        Cycle the highlighted suggestion.

        Args:
            next (str): The next suggestion.
        """
        def __init__(self, next: Annotated[str, 'The next suggestion.']):
            super().__init__()
            self.next = next


    class Continue(Message):
        """Select the current highlighted suggestion."""
        pass

    class Hide(Message):
        """Hide the suggestions."""
        pass

    class Cancel(Message):
        """Cancel the suggestion and undo the auto-completion."""
        pass

    class Execute(Message):
        """Append the suggestion and execute the command"""
        pass


    BINDINGS = [
        Binding('backspace', 'cancel_completion', 'Cancel Autocompletion'),
        Binding('tab', 'cycle', 'Cycle autocompletion', priority=True),
        Binding('space', 'continue', 'Select suggestion'),
        Binding('escape', 'hide', 'Hide autosuggestion'),
        Binding('enter', 'enter_command', 'Select the suggestion and execute the command.', show=False)
    ]

    def on_focus(self, event: events.Focus) -> None:
        """The Suggestion widget has gained focus."""
        self.post_message(self.FocusChange(True))

    def on_blur(self, event: events.Blur) -> None:
        """The Suggestion widget has lost focus."""
        self.post_message(self.FocusChange(False))

    def action_cancel_completion(self) -> None:
        """Cancel the auto-completion."""
        self.highlighted = None
        self.post_message(self.Cancel())

    def action_cycle(self) -> None:
        """Cycle to the next completion."""
        if self.option_count == 0:
            return 

        next = self.highlighted + 1
        if next >= self.option_count:
            next = 0

        self.highlighted = next
        suggestion = self.get_option_at_index(next).prompt
        self.post_message(self.Cycle(suggestion))

    def action_continue(self) -> None:
        """Select the autocompletion."""
        self.post_message(self.Continue())

    def action_hide(self) -> None:
        """Hide the Suggestions"""
        self.post_message(self.Hide())

    def action_enter_command(self) -> None:
        """Execute the command"""
        self.post_message(self.Execute())

Cancel

Bases: Message

Cancel the suggestion and undo the auto-completion.

Source code in src/textual_shell/widgets/shell/suggestions.py
class Cancel(Message):
    """Cancel the suggestion and undo the auto-completion."""
    pass

Continue

Bases: Message

Select the current highlighted suggestion.

Source code in src/textual_shell/widgets/shell/suggestions.py
class Continue(Message):
    """Select the current highlighted suggestion."""
    pass

Cycle

Bases: Message

Cycle the highlighted suggestion.

Parameters:

Name Type Description Default
next str

The next suggestion.

required
Source code in src/textual_shell/widgets/shell/suggestions.py
class Cycle(Message):
    """
    Cycle the highlighted suggestion.

    Args:
        next (str): The next suggestion.
    """
    def __init__(self, next: Annotated[str, 'The next suggestion.']):
        super().__init__()
        self.next = next

Execute

Bases: Message

Append the suggestion and execute the command

Source code in src/textual_shell/widgets/shell/suggestions.py
class Execute(Message):
    """Append the suggestion and execute the command"""
    pass

FocusChange

Bases: Message

A message for when the prompt input has either gained or lost focus.

Source code in src/textual_shell/widgets/shell/suggestions.py
class FocusChange(Message):
    """
    A message for when the prompt input 
    has either gained or lost focus.
    """
    def __init__(self, is_focused: bool):
        super().__init__()
        self.is_focused = is_focused

Hide

Bases: Message

Hide the suggestions.

Source code in src/textual_shell/widgets/shell/suggestions.py
class Hide(Message):
    """Hide the suggestions."""
    pass

action_cancel_completion()

Cancel the auto-completion.

Source code in src/textual_shell/widgets/shell/suggestions.py
def action_cancel_completion(self) -> None:
    """Cancel the auto-completion."""
    self.highlighted = None
    self.post_message(self.Cancel())

action_continue()

Select the autocompletion.

Source code in src/textual_shell/widgets/shell/suggestions.py
def action_continue(self) -> None:
    """Select the autocompletion."""
    self.post_message(self.Continue())

action_cycle()

Cycle to the next completion.

Source code in src/textual_shell/widgets/shell/suggestions.py
def action_cycle(self) -> None:
    """Cycle to the next completion."""
    if self.option_count == 0:
        return 

    next = self.highlighted + 1
    if next >= self.option_count:
        next = 0

    self.highlighted = next
    suggestion = self.get_option_at_index(next).prompt
    self.post_message(self.Cycle(suggestion))

action_enter_command()

Execute the command

Source code in src/textual_shell/widgets/shell/suggestions.py
def action_enter_command(self) -> None:
    """Execute the command"""
    self.post_message(self.Execute())

action_hide()

Hide the Suggestions

Source code in src/textual_shell/widgets/shell/suggestions.py
def action_hide(self) -> None:
    """Hide the Suggestions"""
    self.post_message(self.Hide())

on_blur(event)

The Suggestion widget has lost focus.

Source code in src/textual_shell/widgets/shell/suggestions.py
def on_blur(self, event: events.Blur) -> None:
    """The Suggestion widget has lost focus."""
    self.post_message(self.FocusChange(False))

on_focus(event)

The Suggestion widget has gained focus.

Source code in src/textual_shell/widgets/shell/suggestions.py
def on_focus(self, event: events.Focus) -> None:
    """The Suggestion widget has gained focus."""
    self.post_message(self.FocusChange(True))