barthy-koeln/bash-echolorized
Minimal bash helper for clean, colorized CLI output in CI/CD scripts and git hooks. Source it via Composer or npm/yarn and use e_info/e_success/e_warning/e_error plus colored_output and tagged_output for readable, tagged status lines.
Installation Download the Bash script directly (no Composer package exists—use the raw script from the repo):
curl -o echorized https://raw.githubusercontent.com/barthy-koeln/bash-echolorized/main/echolorized
chmod +x echorized
Place it in a /bin or /scripts directory in your Laravel project.
Basic Usage in Laravel
Call the script from PHP using shell_exec or Laravel’s Process facade:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process(['./echolorized', '--color', 'red', 'Error: Task failed']);
$process->run();
echo $process->getOutput();
First Laravel Integration
Add a helper method in a service class (e.g., app/Services/ConsoleHelper.php):
namespace App\Services;
class ConsoleHelper {
public static function coloredEcho(string $message, string $color = 'default'): void {
$command = './echolorized --color ' . $color . ' "' . escapeshellarg($message) . '"';
shell_exec($command);
}
}
Use it in an Artisan command:
use App\Services\ConsoleHelper;
class MyCommand extends Command {
protected $signature = 'my:command';
protected $description = 'Demonstrates colored output';
public function handle() {
ConsoleHelper::coloredEcho('Success!', 'green');
ConsoleHelper::coloredEcho('Warning!', 'yellow');
}
}
Artisan Command Styling
Replace echo or $this->info() calls with echolorized for consistent styling:
// Before
$this->info('User created: ' . $user->name);
// After
ConsoleHelper::coloredEcho("User created: {$user->name}", 'blue');
Dynamic Styling in Loops Use conditional logic to style output dynamically:
foreach ($tasks as $task) {
$color = $task->completed ? 'green' : 'red';
ConsoleHelper::coloredEcho($task->name, $color);
}
Background/Foreground Combinations Leverage ANSI codes for advanced styling (e.g., red text on yellow background):
echorized --fg red --bg yellow "Critical Alert"
Call from PHP:
shell_exec('echolorized --fg red --bg yellow "Critical Alert"');
Integration with Laravel’s Console Component
Extend Symfony’s Output class to support echolorized:
use Symfony\Component\Console\Output\OutputInterface;
class ColoredOutput extends OutputInterface {
public function styled(string $message, string $style): void {
$command = './echolorized --style ' . $style . ' "' . escapeshellarg($message) . '"';
shell_exec($command);
}
}
CI/CD Pipeline Feedback
Use echolorized to highlight build stages:
echorized --color green "Tests passed!"
echorized --color red "Tests failed: 2 errors"
Call from Laravel’s Artisan::call() in a deployment script.
Debugging and Logging Add context to log messages in development:
if (config('app.debug')) {
ConsoleHelper::coloredEcho('Debug: ' . $variable, 'cyan');
}
User Feedback in Queues Style job completion messages (if output is viewed in a terminal):
dispatch(new ProcessPodcast)->after(function ($job, $podcast) {
ConsoleHelper::coloredEcho("Podcast processed: {$podcast->title}", 'green');
});
Environment Awareness Check if output is a TTY before coloring:
if (stream_isatty(STDOUT)) {
ConsoleHelper::coloredEcho('Colored message', 'blue');
} else {
echo 'Colored message (fallback)' . PHP_EOL;
}
Bundling the Script
Include echolorized in your Laravel project’s /vendor/bin or /scripts directory to ensure it’s version-controlled and portable.
Cross-Platform Compatibility Handle Windows Terminal limitations:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
putenv('ANSICON=1'); // Enable ANSI on Windows
}
Laravel Service Provider Register a facade for cleaner syntax:
// In a service provider
Facades\Facade::register('Echolorized', 'App\Facades\EcholorizedFacade');
// Facade class
namespace App\Facades;
use Illuminate\Support\Facades\Facade as BaseFacade;
class EcholorizedFacade extends BaseFacade {
public static function coloredEcho(string $message, string $color) {
return (new \App\Services\ConsoleHelper())->coloredEcho($message, $color);
}
}
Usage:
Echolorized::coloredEcho('Hello, world!', 'magenta');
ANSI Code Corruption
$log = preg_replace('/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?m/', '', $log);
Windows Compatibility
cmd.exe (requires Windows Terminal or WSL).win-ansi-escapes.Shell Injection Risks
shell_exec is unsafe.escapeshellarg():
$message = escapeshellarg($userInput);
shell_exec("./echolorized --color red {$message}");
Non-TTY Output
if (!stream_isatty(STDOUT)) {
echo "Fallback: {$message}\n";
return;
}
Script Location Dependencies
echolorized breaks portability.$PATH:
$scriptPath = base_path('scripts/echolorized');
shell_exec("{$scriptPath} --color blue 'Message'");
Verify ANSI Support Test if your terminal supports ANSI:
echo -e "\e[31mRed text\e[0m"
If it doesn’t render, use a tool like ANSI Escape for Node.js or a PHP polyfill.
Check Script Permissions
Ensure echolorized is executable:
chmod +x ./echolorized
Inspect Output
Pipe output to cat -v to see hidden ANSI codes:
./echolorized "Test" | cat -v
Default Colors
The script uses a predefined palette. To customize, edit the Bash script’s color_map array or pass custom ANSI codes:
echorized --fg "\e[38;5;202m" "Custom color"
Style Shortcuts
Use built-in styles (success, error, warn) for consistency:
echorized --style success "Operation completed"
Bold/Italic Text Combine styles for emphasis:
echorized --fg red --bold "Critical Error"
Add Custom Colors
Extend the Bash script’s color_map to support additional colors:
declare -A color_map=(
["custom"]="\e[38;5;196m" # Custom RGB color
)
PHP Wrapper Enhancements
Add methods for common use cases in your ConsoleHelper:
class ConsoleHelper {
public
How can I help you explore Laravel packages today?