# Termwind Termwind is a PHP library that brings the Tailwind CSS API to command-line applications. It allows developers to build beautiful terminal interfaces using familiar CSS utility classes like `bg-green-300`, `px-1`, `text-center`, and more. Instead of manually formatting terminal output with ANSI escape codes, developers can write HTML-like markup with Tailwind-style classes that render as styled console output. The library is built on top of Symfony Console and provides a complete set of HTML elements (div, span, paragraph, tables, lists, etc.) that can be styled with Tailwind utility classes. It supports responsive design with breakpoints, custom color definitions, text formatting, spacing utilities, and interactive features like prompts. Termwind seamlessly integrates with Laravel and Symfony console commands, making it easy to create visually appealing CLI tools with minimal effort. ## Core Functions ### render() - Display HTML in the terminal The `render()` function takes HTML markup with Tailwind CSS classes and outputs styled content to the terminal. It's the primary way to display formatted text in CLI applications. ```php Termwind'); // Multi-line HTML with nested elements render(<<<'HTML'
Status Report
Hello, {$name}!
"); // Question with styling $email = ask(<<<'HTML' 📧 Enter your email address: HTML); // Question with autocomplete options $framework = ask( 'Choose a framework:', ['Laravel', 'Symfony', 'CodeIgniter', 'Yii'] ); // Building an interactive form $username = ask('Username:'); $password = ask('Password:'); render(<<✓ Account created for: {$username}
HTML); ``` ### terminal() - Access terminal properties The `terminal()` function returns a Terminal object that provides methods to interact with the terminal environment, including getting dimensions and clearing the screen. ```php width(); $height = $term->height(); render(<<Terminal dimensions: {$width}x{$height}
HTML); // Clear the terminal screen $term->clear(); // Create responsive layouts based on width if ($width > 100) { render('Application Report
'); $footer = parse('Generated at ' . date('Y-m-d H:i:s') . '
'); // Combine and render later $fullReport = $header . "\n" . $output . "\n" . $footer; echo $fullReport; // Prepare dynamic content $messages = []; foreach (['info', 'warning', 'error'] as $type) { $messages[$type] = parse("{$type} message"); } // Conditionally display based on logic $severity = 'error'; echo $messages[$severity]; ``` ### renderUsing() - Set custom output handler The `renderUsing()` function allows you to specify a custom OutputInterface implementation for rendering, enabling integration with Symfony Console OutputInterface or custom output handlers. ```php Standard output'); // Use buffered output for testing $buffer = new BufferedOutput(); renderUsing($buffer); render('Features:
Application Configuration
| `, and ` | ` tags for displaying tabular data with automatic formatting and optional styling.
```php
| Name |
Status |
|
|---|---|---|---|
| Termwind | ✓ Active | ||
| Parser | ✓ Active |
| Task | Progress | Status |
|---|---|---|
| Build | 100% | ✓ Done |
| Test | 75% | ⧗ Running |
| Deploy | 0% | ○ Pending |
This is bold text and this is italic text
This is underlined and this is strikethrough
Use strong for important text and emphasis for emphasis
HTML); // Combining formatting with colors render(<<<'HTML'✓ Success: Operation completed
✗ Error: Invalid input
Note: Configuration updated
Documentation: termwind.com
Issues: Report a bug
Resources:
` tag displays code with syntax highlighting and line numbers. Use `line` and `start-line` attributes to highlight specific lines.
```php
function hello() {
echo "Hello, World!";
}
HTML);
// Highlight specific line
render(<<<'HTML'
try {
throw new \Exception('Something went wrong');
} catch (\Throwable $e) {
report($e);
}
HTML);
// Preformatted text with
render(<<<'HTML'
Text in a pre element
it preserves
both spaces and
line breaks
HTML);
```
## Tailwind CSS Classes
### Spacing - Margin and padding utilities
Termwind supports all Tailwind spacing utilities including margin (`m-*`), padding (`p-*`), and their directional variants (`ml-*`, `px-*`, `my-*`, etc.).
```php
Top margin
Bottom margin
Left margin (indent)
Horizontal margins
Vertical margins
HTML);
// Padding utilities
render(<<<'HTML'
Padding all sides
Horizontal padding
Vertical padding
Individual padding
HTML);
// Combining spacing with other styles
render(<<<'HTML'
Title
Indented content with spacing
HTML);
```
### Colors - Background and text colors
Apply background colors with `bg-{color}-{variant}` and text colors with `text-{color}-{variant}`. Supports all standard Tailwind color palettes.
```php
Red background
Green background
Blue background
Yellow background
HTML);
// Text colors
render(<<<'HTML'
Error message
Success message
Information
Muted text
HTML);
// Color variants
render(<<<'HTML'
Light blue
Medium blue
Dark blue
HTML);
```
### Typography - Font styles and text transforms
Control text appearance with font weight, style, decoration, transformation, and alignment utilities.
```php
Bold text
Normal text
Italic text
HTML);
// Text decoration
render(<<<'HTML'
Underlined text
Strikethrough text
HTML);
// Text transformation
render(<<<'HTML'
uppercase text
LOWERCASE TEXT
capitalize each word
HTML);
// Text alignment
render(<<<'HTML'
Left aligned
Center aligned
Right aligned
HTML);
// Text overflow
render('This is a very long text that will be truncated
');
```
### Layout - Width, display, and flex utilities
Control element dimensions, visibility, and flexbox layouts with Tailwind's layout utilities.
```php
Width 20
Full width
Auto width
Minimum width
Maximum width content here
HTML);
// Display utilities
render(<<<'HTML'
Block element
Inline
Inline
This is hidden
This is invisible but takes space
HTML);
// Flexbox utilities
render(<<<'HTML'
Left
Right
Centered
A
B
C
Even
Distribution
HTML);
```
## Summary
Termwind is primarily used for building visually appealing command-line interfaces in PHP applications. It excels in creating interactive CLI tools, progress indicators, status dashboards, and formatted output for console commands. The library is particularly valuable in Laravel Artisan commands, Symfony Console applications, build tools, deployment scripts, and developer utilities where presenting information clearly in the terminal enhances user experience. Its Tailwind CSS-based approach makes it intuitive for web developers to apply familiar styling patterns to CLI applications.
Integration with existing PHP applications is straightforward through Composer installation and namespace imports. The library works seamlessly with Laravel's service provider auto-discovery, integrates naturally with Symfony Console commands via the `renderUsing()` function, and can be used standalone in any PHP 8.2+ CLI script. Common patterns include wrapping Termwind rendering in reusable command classes, creating custom style libraries for consistent branding across CLI tools, and combining `ask()` with `render()` for interactive wizards and configuration tools. The library's flexibility allows it to be adopted incrementally, replacing existing echo statements with styled output without requiring architectural changes.