News

Build Terminal UIs in PHP With ANSI Kit

Published
Build Terminal UIs in PHP With ANSI Kit image

AnsiKit is a zero-dependency ANSI escape helper for building terminal UIs in PHP. It features a chainable API for styles, color, and cursor control, along with useful components and utilities:

Here's a quick example of using the input component with a confirmation, and rendering a table using the package's table component (the examples folder has more complete examples):

use Ajaxray\AnsiKit\AnsiTerminal;
use Ajaxray\AnsiKit\Components\Table;
use Ajaxray\AnsiKit\Support\Input;
 
$t = new AnsiTerminal();
$t->clearScreen()->cursorHome();
 
// Single-line input
$name = Input::line('What is your name? [Anonymous] ', 'Anonymous');
$t->writeStyled("Hello, {$name}!\n", [AnsiTerminal::TEXT_BOLD, AnsiTerminal::FG_GREEN]);
 
// Confirmation
$proceed = Input::confirm('Do you want to enter a short bio?', true);
if (!$proceed) {
$t->writeStyled("Okay, skipping bio.\n", [AnsiTerminal::FG_YELLOW]);
exit(0);
}
 
// Example of the Table component
(new Table())
->setHeaders('Name','Age')
->addRow('Ada', '36')
->addRow('Linus', '54')
->render();

AnsiKit features the following components and helpers to help you build UIs on the command line:

  • Table component
  • Banner component
  • Progressbar component
  • Spinner component
  • Choice component
  • Keypress helper
  • Input helper
  • And more...

💻 You can get started with AnsiKit on GitHub: ajaxray/AnsiKit

📕 The GitHub repo has an examples folder: examples

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Building EasyReply: How We Used Laravel to Unify Customer Support image

Building EasyReply: How We Used Laravel to Unify Customer Support

Read article
PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum image

PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum

Read article
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article