
# Alert Class

The Alert class is responsible for displaying colored alerts in the Terminal.

## Instance

To use the Alert class, it is necessary to instantiate an object of the same class passing as parameter a reference to the Output object of the Terminal class:

```php
use const Bootgly\CLI;
use Bootgly\CLI\UI\Components\Alert;

$Output = CLI->Terminal->Output;

$Alert = new Alert($Output);
```

## Settings

The Alert class can be configured with the following options:

### Style

The style of alert to be displayed. Can be "Default" or "Fullcolor".

Example:

```php
// Setting the alert style to Fullcolor
$Alert->Style::Fullcolor->set();
```

### Type

The type of alert to be displayed. Can be "Default", "Success", "Attention" or "Failure".

Example:

```php
// Setting the alert type to Success
$Alert->Type::Success->set();
```

### width

The width in characters of the alert to be displayed.

Example:

```php
// Setting the alert width to 100 characters
$Alert->width = 100;
```

## Usage

### Setting the alert message

The `message` property is used to set the message that should be displayed in the alert.

Example:

```php
$Alert->message = 'This is a success alert!';
```

### Long messages are cropped

The alert message always occupies a single row. A message wider than the terminal is cropped with an ellipsis (`…`) before it is written, with the badge (` SUCCESS `, ` ATTENTION `, ` FAIL `, ` ALERT `) discounted from the available columns:

```php
$Alert->message = str_repeat('A very long validation message. ', 10);

// @ Rendered as: FAIL  A very long validation message. A very long validati…
$Alert->render();
```

A wrapped alert would spill into a second row, and that breaks both the block repaints the surrounding components rely on and the row accounting of any [Region](/manual/CLI/Terminal/Output/Region/overview) hosting it — a region counts the rows it emits by the line breaks passing through it, and a wrapped row is one it never sees.

The crop needs `Terminal::$width` to be known; when it is not (piped output, for instance), the message is written whole.

## See it live

The official Alert demo runs in the [live showcase](/manual/CLI/UI/Components/Alert/showcase) — real framework code on PHP 8.4 WebAssembly, in your browser, straight from this page.
