# 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'
Welcome to Termwind
Give your CLI apps a unique look
HTML); // Using various Tailwind classes render(<<<'HTML'

Status Report

Processing: 75%
✓ Complete
HTML); // Responsive design with breakpoints render(<<<'HTML'
Background changes color based on terminal width
HTML); ``` ### style() - Create custom styles and colors The `style()` function allows you to define custom CSS classes or override existing color definitions. It enables extending Termwind's styling capabilities beyond the default Tailwind classes. ```php color('#bada55'); // Create a custom button style style('btn')->apply('p-4 bg-green-300 text-white font-bold'); // Create a custom alert style style('alert')->apply('px-2 py-1 bg-red-500 text-white'); // Use custom styles render('
Click me
'); render('
Warning: Operation failed!
'); // Create a style with custom behavior style('highlight')->color('#ffff00')->apply('font-bold underline'); render('Important text'); ``` ### ask() - Prompt user for input The `ask()` function displays a styled prompt and waits for user input, returning the user's response. It supports HTML markup with Tailwind classes for styled prompts and optional autocomplete functionality. ```php What is your name?'); render("

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('
LeftRight
'); } else { render('
Left
Right
'); } // Center content based on terminal width $contentWidth = 50; $padding = (int) (($width - $contentWidth) / 2); render(<<
Centered Content
HTML); ``` ### parse() - Convert HTML to terminal string The `parse()` function converts HTML markup with Tailwind classes into a formatted string that can be rendered in the terminal, without immediately displaying it. Useful for preparing output that will be used later. ```php Status: OK'); // Store parsed outputs $header = parse('

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('
Buffered content
'); $content = $buffer->fetch(); assert(str_contains($content, 'Buffered content')); // Reset to default renderUsing(null); // Use in Symfony Console Command class MyCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output): int { renderUsing($output); render('
✓ Command executed
'); return Command::SUCCESS; } } ``` ## HTML Elements ### Lists - Unordered and ordered lists Create bullet and numbered lists using ` HTML); // Ordered list with numbers render(<<<'HTML'
  1. Initialize project
  2. Install dependencies
  3. Run tests
HTML); // Custom styled lists render(<<<'HTML'

Features:

HTML); // Nested lists render(<<<'HTML'
  1. Backend Tasks
  2. Frontend Tasks
HTML); ``` ### Description Lists - Definition lists Description lists use `
`, `
`, and `
` tags to create term-definition pairs, ideal for displaying configuration options, glossaries, or key-value information. ```php
Termwind
A PHP library for terminal styling
Composer
Dependency manager for PHP
HTML); // Styled configuration display render(<<<'HTML'

Application Configuration

Environment
production
Debug Mode
disabled
Cache Driver
redis
HTML); // Feature showcase render(<<<'HTML'
🎨 Styling
Tailwind CSS utility classes
📱 Responsive
Breakpoint-based layouts
⚡ Fast
Optimized rendering engine
HTML); ``` ### Tables - Structured data display Tables support ``, ``, ``, `
`, and `` tags for displaying tabular data with automatic formatting and optional styling. ```php
Name Status
Termwind ✓ Active
Parser ✓ Active
HTML); // Styled table with classes render(<<<'HTML'
Task Progress Status
Build 100% ✓ Done
Test 75% ⧗ Running
Deploy 0% ○ Pending
HTML); ``` ### Text Formatting - Bold, italic, underline, strikethrough Use semantic HTML tags like ``, ``, ``, ``, ``, and `` for text formatting, or apply Tailwind classes for custom styling. ```php

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

HTML); // Tailwind class formatting render(<<<'HTML'
BOLD & STYLED Italic with underline Deprecated feature
HTML); ``` ### Links - Clickable hyperlinks The `` tag with `href` attribute creates clickable links in terminals that support them, allowing users to open URLs by clicking. ```php Visit Termwind on GitHub

'); // Styled links render(<<<'HTML'

Documentation: termwind.com

Issues: Report a bug

HTML); // Links in lists render(<<<'HTML' HTML); ``` ### Code Blocks - Syntax highlighting The `` 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
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.