Skip to content

CancellableLoaderWidget

Edit this page

CancellableLoaderWidget extends LoaderWidget with focus and keyboard support so the user can cancel the operation by pressing Escape or Ctrl+C.

When to Use

Use CancellableLoaderWidget for long-running operations that the user should be able to abort: network requests, file indexing, AI inference, etc. If the operation cannot be cancelled, use LoaderWidget instead.

Basic Usage

1
2
3
4
5
use Symfony\Component\Tui\Widget\CancellableLoaderWidget;

$loader = new CancellableLoaderWidget('Generating response...');
$tui->add($loader);
$tui->setFocus($loader);

Because CancellableLoaderWidget implements FocusableInterface, it must receive focus to capture keyboard input.

Handling Cancellation

Listen for the CancelEvent to react when the user cancels:

1
2
3
4
5
6
use Symfony\Component\Tui\Event\CancelEvent;

$loader->onCancel(function (CancelEvent $event) {
    // abort the background operation
    $loader->stop();
});

You can also check the cancellation state at any time:

1
2
3
if ($loader->isCancelled()) {
    // handle cancellation
}

Call reset() to clear the cancelled flag before reusing the widget:

1
2
$loader->reset();
$loader->start();

Inherited Features

CancellableLoaderWidget inherits all features from LoaderWidget: spinner styles, animation speed, message updates and the finished indicator.

Default Keybindings

Key Action
Escape, Ctrl+C Cancel
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version