Skip to content

textual_enhanced.commands

Provides code related to 'commands' in a Textual application.

CommandHits module-attribute

CommandHits = Iterator[CommandHit | Command]

The result of looking for commands to make into hits.

ChangeTheme

Bases: Command

Change the application's theme

Command

Bases: Message

Base class for all application command messages.

ACTION class-attribute instance-attribute

ACTION = None

The action to call when the command is executed.

By default the action will be:

action_{snake-case-of-command}_command

There are some commands that will result in unhelpful snake-case names, and also times where you want the action method named in a better way; in such cases use ACTION to override the method name.

BINDING_KEY class-attribute instance-attribute

BINDING_KEY = None

The binding key for the command.

This can either be a string, which is the keys to bind, a tuple of the keys and also an overriding display value, or None.

COMMAND class-attribute instance-attribute

COMMAND = None

The text for the command.

Notes

If no COMMAND is provided the class name will be used.

FOOTER_TEXT class-attribute instance-attribute

FOOTER_TEXT = None

The text to show in the footer.

Notes

If no FOOTER_TEXT is provided the command will be used.

SHOW_IN_FOOTER = False

Should the command be shown in the footer?

By default commands are not shown in the footer, so set this to True to have the command appear there.

context_command property

context_command

The command in context.

context_tooltip property

context_tooltip

The tooltip for the command, in context.

has_binding property

has_binding

Does this command have a binding?

action_name classmethod

action_name()

Get the action name for the command.

Returns:

Type Description
str

The action name.

binding classmethod

binding()

Create a binding object for the command.

Returns:

Type Description
Binding

A Binding for the command's key bindings.

Raises:

Type Description
ValueError

If the command has no key binding.

bindings staticmethod

bindings(*bindings)

Create bindings.

Parameters:

Name Type Description Default

bindings

BindingType | type[Command]

Normal Textual bindings or a command class.

()

Returns:

Type Description
list[BindingType]

A list of bindings that can be used with BINDINGS.

command classmethod

command()

The text for the command.

Returns:

Type Description
str

The command's textual name.

key_binding classmethod

key_binding()

Get the key that is the binding for this command.

Returns:

Type Description
str

The key that is bound, or None if there isn't one.

Notes

If a command has multiple bindings, only the first key is returned.

tooltip classmethod

tooltip()

The tooltip for the command.

CommandHit

Bases: NamedTuple

A command hit for use in building a command palette hit.

command instance-attribute

command

The command.

description instance-attribute

description

The description of the command.

message instance-attribute

message

The message to emit when the command is chosen.

CommandsProvider

Bases: Provider

A base class for command-message-oriented command palette commands.

commands abstractmethod

commands()

Provide the command data for the command palette.

Yields:

Type Description
CommandHits

A tuple of the command, the command description and a command message.

discover async

discover()

Handle a request to discover commands.

Yields:

Type Description
Hits

Command discovery hits for the command palette.

maybe

maybe(command)

Yield a command if it's applicable.

Parameters:

Name Type Description Default

command

type[Command]

The type of the command to maybe yield.

required

Yields:

Type Description
CommandHits

The command if it can be used right now.

This method takes the command, looks at its action_name and uses Textual's check_action to see if the action can be performed right now. If it can it will yield an instance of the command, otherwise it does nothing.

prompt classmethod

prompt()

The prompt for the command provider.

search async

search(query)

Handle a request to search for commands that match the query.

Parameters:

Name Type Description Default

query

str

The query from the user.

required

Yields:

Type Description
Hits

Command hits for the command palette.

CommonCommands

Bases: CommandsProvider

Provides some common top-level commands for the application.

commands

commands()

Provide the main application commands for the command palette.

Yields:

Type Description
CommandHits

The commands for the command palette.

Help

Bases: Command

Show help for and information about the application

Quit

Bases: Command

Quit the application

Provides helper code for dealing with bindings.

all_keys_for

all_keys_for(node, source)

Get all the keys for the given command or binding.

Parameters:

Name Type Description Default

node

DOMNode

The node we're working from.

required

source

type[Command] | Binding

The command or binding to get the keys for.

required

Yields:

Type Description
str

The display names of all the keys for the command/binding.

primary_key_for

primary_key_for(node, source)

Get the primary key for the given command or binding.

Parameters:

Name Type Description Default

node

DOMNode

The node we're working from.

required

source

type[Command] | Binding

The command or binding to get the keys for.

required

Returns:

Type Description
str

The display name of the primary key for the command/binding.