1. Go to this page and download the library: Download xocdr/tui library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xocdr / tui example snippets
use Xocdr\Tui\UI;
use Xocdr\Tui\Components\Box;
use Xocdr\Tui\Components\Component;
use Xocdr\Tui\Components\Text;
class Counter extends UI
{
public function build(): Component
{
[$count, $setCount] = $this->state(0);
$this->onKeyPress(function ($input, $key) use ($setCount) {
if ($input === 'q' || $key->escape) {
$this->exit();
}
if ($input === ' ') {
$setCount(fn($c) => $c + 1);
}
});
return Box::create()
->flexDirection('column')
->padding(1)
->border('round')
->children([
Text::create("Count: {$count}")->bold(),
Text::create('Press SPACE to increment, Q to quit')->dim(),
]);
}
}
(new Counter())->run();
use Xocdr\Tui\Components\Text;
Text::create('Hello World')
->bold()
->italic()
->underline()
->strikethrough()
->dim()
->inverse()
->color('#ff0000') // Hex color
->bgColor('#0000ff') // Background color
->color('blue', 500) // Tailwind palette + shade
->bgColor('slate', 100) // Background palette + shade
->wrap('word'); // 'word' | 'none'
// Color shortcuts
Text::create('Error')->red();
Text::create('Success')->green();
Text::create('Info')->blue()->bold();
// Unified color API (accepts Color enum, hex, or palette name with shade)
use Xocdr\Tui\Ext\Color;
Text::create('Palette')->color('red', 500); // Palette name + shade
Text::create('Palette')->color(Color::Red, 500); // Color enum + shade
Text::create('Palette')->color(Color::Coral); // CSS color via enum
// Tailwind-like utility classes
Text::create('Hello')
->styles('bold text-green-500') // Multiple utilities
->styles('text-red bg-slate-900 underline'); // Colors + styles
// Bare colors as text color shorthand
Text::create('Error')->styles('red'); // Same as text-red
Text::create('Success')->styles('green-500'); // Same as text-green-500
// Dynamic styles with callables
Text::create('Status')
->styles(fn() => $active ? 'green' : 'red') // Conditional styling
->styles('bold', ['italic', 'underline']); // Mixed arguments
use Xocdr\Tui\Components\Fragment;
use Xocdr\Tui\Components\Spacer;
use Xocdr\Tui\Components\Newline;
use Xocdr\Tui\Components\Static_;
// Fragment - group without extra node
Fragment::create([
Text::create('Line 1'),
Text::create('Line 2'),
]);
// Spacer - fills available space (flexGrow: 1)
Box::row([
Text::create('Left'),
Spacer::create(),
Text::create('Right'),
]);
// Newline - line breaks
Newline::create(2); // Two line breaks
// Static - non-rerendering content (logs, history)
Static_::create($logItems);
use Xocdr\Tui\Hooks\Hooks;
$hooks = new Hooks($instance);
[$count, $setCount] = $hooks->state(0);
// Direct value
$setCount(5);
// Functional update
$setCount(fn($prev) => $prev + 1);
use Xocdr\Tui\Contracts\HooksAwareInterface;
use Xocdr\Tui\Hooks\HooksAwareTrait;
class MyComponent implements HooksAwareInterface
{
use HooksAwareTrait;
public function render(): mixed
{
[$count, $setCount] = $this->hooks()->state(0);
// ...
}
}
$runtime = (new MyApp())->run();
// Input events via InputManager
$runtime->getInputManager()->onInput(function ($key, $nativeKey) {
echo "Key pressed: $key";
}, priority: 10);
// Resize events via EventDispatcher
$runtime->getEventDispatcher()->on('resize', function ($event) {
echo "New size: {$event->width}x{$event->height}";
});
// Focus events via EventDispatcher
$runtime->getEventDispatcher()->on('focus', function ($event) {
echo "Focus changed to: {$event->currentId}";
});
// Remove handler
$handlerId = $runtime->getInputManager()->onInput($handler);
$runtime->getEventDispatcher()->off($handlerId);
class MyApp extends UI
{
public function build(): Component
{
$this->onKeyPress(function ($input, $key) {
// Handle input
});
return Text::create('Hello');
}
}
use Xocdr\Tui\Runtime;
// Using UI class (recommended)
$runtime = (new MyApp())->run(['fullscreen' => true]);
// Direct Runtime instantiation
$runtime = new Runtime(
$component,
['fullscreen' => true],
$customEventDispatcher, // optional
$customHookContext, // optional
$customRenderer // optional
);
$runtime->start();
$runtime->waitUntilExit();