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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Laravel Auditor Audits Your App With Your Own AI Agent image

Laravel Auditor Audits Your App With Your Own AI Agent

Read article
A simple form builder that stays out of your way image

A simple form builder that stays out of your way

Read article
Laravel AI: Trace Agent Runs With Lifecycle Events image

Laravel AI: Trace Agent Runs With Lifecycle Events

Read article
Laravel AI: Get Raw HTTP Responses and Rate Limits image

Laravel AI: Get Raw HTTP Responses and Rate Limits

Read article
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article