CLImate
A terminal output library for PHP. Makes it easy to create beautiful CLI output with colors, padding, formatting, and more.
Framework
CLImate
Overview
CLImate is a terminal output library for PHP. It makes it easy to create beautiful CLI output with colors, padding, formatting, and more. It's popular among developers who care about terminal output appearance and is often used in combination with other CLI frameworks as a specialized output library.
Details
CLImate is a library specialized in improving the visual experience of command-line output. It provides rich functionality including text styling, tables, progress bars, animations, and interactive input to achieve professional-looking CLI applications.
Key Features
- Rich Colors and Styles: Supports 16-color, 256-color, and TrueColor
- Table Display: Customizable beautiful tables
- Progress Bars: Various styles of progress display
- Interactive Input: Prompts, choices, confirmation dialogs
- Text Decoration: Padding, alignment, borders
- Animation: Spinners, blinking effects
- JSON/CSV Output: Formatted output of structured data
- Terminal Detection: Automatic detection of terminal capabilities
Pros and Cons
Pros
- Beautiful Output: Professional and visually appealing display
- Rich Features: Comprehensive output and formatting capabilities
- Easy-to-use API: Intuitive and learnable interface
- Flexibility: Easy combination with other CLI libraries
- Cross-platform: Works on Windows, Linux, macOS
- Terminal Support: Proper display in various terminal environments
Cons
- Specialization: Specialized for CLI output, not a complete CLI framework
- Learning Curve: Time needed to utilize rich features
- Performance: Heavy decorative output may affect processing speed
- Dependencies: Need to add external library just for output
Key Links
Usage Examples
Installation and Basic Setup
composer require league/climate
Basic Text Output
<?php
require_once 'vendor/autoload.php';
use League\CLImate\CLImate;
$climate = new CLImate;
// Basic text output
$climate->out('Normal text');
// Colored text
$climate->red('Red text');
$climate->green('Green text');
$climate->blue('Blue text');
$climate->yellow('Yellow text');
// Background colors
$climate->backgroundRed('Red background');
$climate->backgroundGreen('Green background');
// Combined styles
$climate->bold('Bold text');
$climate->underline('Underlined text');
$climate->bold()->red('Bold red text');
$climate->underline()->blue()->backgroundYellow('Combined styles');
// Method chaining
$climate
->bold()
->green()
->out('Chained styles');
Table Display Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Basic table
$data = [
[
'name' => 'John Doe',
'age' => 30,
'city' => 'Tokyo',
'email' => '[email protected]'
],
[
'name' => 'Jane Smith',
'age' => 25,
'city' => 'Osaka',
'email' => '[email protected]'
],
[
'name' => 'Bob Johnson',
'age' => 35,
'city' => 'Nagoya',
'email' => '[email protected]'
]
];
$climate->table($data);
// Table with custom header
$climate->green()->bold()->out('Employee List');
$climate->table($data);
// Dynamic table creation
$climate->yellow()->out('Dynamic table example:');
$dynamicData = [];
for ($i = 1; $i <= 5; $i++) {
$dynamicData[] = [
'ID' => $i,
'Product' => "Product{$i}",
'Price' => rand(1000, 10000),
'Stock' => rand(0, 100)
];
}
$climate->table($dynamicData);
Progress Bar Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Basic progress bar
$climate->green()->out('Processing files...');
$progress = $climate->progress()->total(100);
for ($i = 0; $i <= 100; $i++) {
$progress->current($i);
usleep(50000); // 50ms wait
}
$climate->green()->out('Processing complete!');
// Custom progress bar
$climate->yellow()->out('Database migration in progress...');
$progress = $climate->progress()->total(50);
$progress->label('Migration Progress');
for ($i = 0; $i <= 50; $i++) {
$progress->current($i, "Processing record {$i}/50");
usleep(100000); // 100ms wait
}
$climate->green()->out('Migration complete!');
Interactive Input Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
$climate->green()->bold()->out('User Registration System');
$climate->out('');
// Text input
$name = $climate->input('Please enter your name:')->prompt();
// Password input (hidden)
$password = $climate->password('Please enter password:')->prompt();
// Numeric input (with validation)
$age = $climate->input('Please enter your age:')->accept(function($response) {
return is_numeric($response) && $response > 0 && $response < 150;
}, 'Invalid age. Please enter a number between 1 and 149.')->prompt();
// Radio selection
$gender = $climate->radio('Please select gender:', [
'male' => 'Male',
'female' => 'Female',
'other' => 'Other'
])->prompt();
// Multiple selection
$interests = $climate->checkboxes('Please select your interests:', [
'programming' => 'Programming',
'design' => 'Design',
'music' => 'Music',
'sports' => 'Sports',
'reading' => 'Reading'
])->prompt();
// Confirmation
$confirm = $climate->confirm('Register with the above information?');
if ($confirm) {
$climate->green()->out('Registration completed!');
// Display results
$climate->yellow()->out('Registration details:');
$climate->table([
['Field', 'Value'],
['Name', $name],
['Age', $age],
['Gender', $gender],
['Interests', implode(', ', $interests)]
]);
} else {
$climate->red()->out('Registration cancelled.');
}
Padding and Alignment Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Padding
$climate->green()->out('Padding example:');
$climate->tab()->out('Tab indent');
$climate->tab(2)->out('Double tab indent');
// Custom padding
$climate->padding(10)->out('10 character padding');
$climate->padding(5, '=')->out('Custom padding character');
// Alignment
$climate->yellow()->out('Alignment example:');
$climate->center('Center aligned text');
$climate->right('Right aligned text');
// Combined padding
$climate->tab()->green()->bold()->out('Hierarchical display:');
$climate->tab(2)->out('Sub item 1');
$climate->tab(2)->out('Sub item 2');
$climate->tab(3)->blue()->out('Nested item');
Borders and Boxes Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Simple border
$climate->border();
$climate->out('Text surrounded by border');
$climate->border();
// Custom border
$climate->border('=', 50);
$climate->center('Custom Border');
$climate->border('=', 50);
// Multi-line box
$climate->green()->border('-', 60);
$climate->green()->out('System Information');
$climate->border('-', 60);
$systemInfo = [
'OS' => PHP_OS,
'PHP Version' => PHP_VERSION,
'Memory Limit' => ini_get('memory_limit'),
'Max Execution Time' => ini_get('max_execution_time') . 's'
];
foreach ($systemInfo as $key => $value) {
$climate->tab()->out("{$key}: {$value}");
}
$climate->border('-', 60);
JSON and CSV Output Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
$data = [
[
'id' => 1,
'name' => 'Product A',
'price' => 1000,
'category' => 'Electronics'
],
[
'id' => 2,
'name' => 'Product B',
'price' => 2000,
'category' => 'Books'
],
[
'id' => 3,
'name' => 'Product C',
'price' => 1500,
'category' => 'Clothing'
]
];
// JSON output
$climate->yellow()->out('JSON format:');
$climate->json($data);
$climate->out('');
// Formatted JSON
$climate->yellow()->out('Formatted JSON:');
$climate->dump($data);
Animation and Spinner Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Spinner animation
$climate->green()->out('Processing data...');
$spinner = ['|', '/', '-', '\\'];
$spinnerIndex = 0;
for ($i = 0; $i < 20; $i++) {
$climate->inline("\rProcessing " . $spinner[$spinnerIndex]);
$spinnerIndex = ($spinnerIndex + 1) % count($spinner);
usleep(200000); // 200ms
}
$climate->out("\rProcessing complete! ");
// Blinking effect
$climate->yellow()->out('Important notice:');
for ($i = 0; $i < 5; $i++) {
$climate->inline("\r" . ($i % 2 ? '★' : '☆') . ' System update available ' . ($i % 2 ? '★' : '☆'));
usleep(500000); // 500ms
}
$climate->out('');
Advanced Feature Combination Example
<?php
use League\CLImate\CLImate;
$climate = new CLImate;
// Application header
$climate->clear(); // Clear screen
$climate->green()->bold()->border('=', 70);
$climate->green()->bold()->center('Data Analysis Tool v1.0.0');
$climate->green()->bold()->border('=', 70);
$climate->out('');
// Menu display
$climate->yellow()->bold()->out('Available operations:');
$climate->tab()->out('1. Data Import');
$climate->tab()->out('2. Data Analysis');
$climate->tab()->out('3. Report Generation');
$climate->tab()->out('4. Settings');
$climate->tab()->out('5. Exit');
$climate->out('');
// Selection
$choice = $climate->radio('Please select an operation:', [
'1' => 'Data Import',
'2' => 'Data Analysis',
'3' => 'Report Generation',
'4' => 'Settings',
'5' => 'Exit'
])->prompt();
switch ($choice) {
case '1':
$climate->green()->out('Starting data import...');
$file = $climate->input('Enter CSV file path:')->prompt();
if (file_exists($file)) {
$progress = $climate->progress()->total(100);
$progress->label('Importing');
for ($i = 0; $i <= 100; $i += 10) {
$progress->current($i);
usleep(300000);
}
$climate->green()->bold()->out('✓ Import completed!');
} else {
$climate->red()->out('✗ File not found');
}
break;
case '2':
$climate->blue()->out('Running data analysis...');
// Display analysis results
$analysisData = [
['Item', 'Value', 'Status'],
['Total Records', '1,234', '✓'],
['Error Records', '12', '⚠'],
['Processing Time', '2.3s', '✓'],
['Memory Usage', '45MB', '✓']
];
$climate->table($analysisData);
break;
case '5':
$climate->red()->out('Exiting application...');
break;
default:
$climate->yellow()->out('This feature is currently under development.');
}
$climate->out('');
$climate->border('-', 70);
$climate->center('Thank you');
$climate->border('-', 70);