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 →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article