php-kilo/src/Terminal/ANSI.php

166 lines
3.7 KiB
PHP

<?php declare(strict_types=1);
namespace Aviat\Kilo\Terminal;
use Aviat\Kilo\Terminal\Enum\Color;
use Aviat\Kilo\Terminal\Enum\Color256;
/**
* ANSI
*/
class ANSI {
// ------------------------------------------------------------------------
// General ANSI standard escape sequences
// ------------------------------------------------------------------------
/**
* Clear the terminal window
*/
public const CLEAR_SCREEN = "\e[2J";
/**
* Clear the terminal line
*/
public const CLEAR_LINE = "\e[K";
// ------------------------------------------------------------------------
// Text formatting escape sequences
// ------------------------------------------------------------------------
/**
* Removes text attributes, such as bold, underline, blink, inverted colors
*/
public const RESET_TEXT = "\e[m";
public const BOLD_TEXT = "\e[1m";
public const DIM_TEXT = "\e[2m";
public const ITALIC_TEXT = "\d[3m";
public const UNDERLINE_TEXT = "\e[4m";
public const BLINKING_TEXT = "\e[5m";
public const INVERSE_TEXT = "\e[7m";
public const STRIKE_TEXT = "\e[9m";
/**
* Move the cursor to position 0,0 which is the top left
*/
public const RESET_CURSOR = "\e[H";
// ------------------------------------------------------------------------
// VT-series escapes, not really ANSI standard
// ------------------------------------------------------------------------
public const HIDE_CURSOR = "\e[?25l";
public const SHOW_CURSOR = "\e[?25h";
/**
* Generate the ascii sequence for basic foreground text color
*
* @param Color|Color256|int $color
* @param Color $ground
* @return string
*/
public static function color(Color|Color256| int $color, Color $ground = Color::Fg): string
{
if ( ! $color instanceof Color)
{
return self::color256($color, $ground);
}
return self::escapeSequence('%dm', $color->value);
}
/**
* Generate the ANSI sequence for a 256 mode color
*
* @param Color256 | int $color
* @param Color $ground
* @return string
*/
public static function color256(Color256| int $color, Color $ground = Color::Fg): string
{
if ($color instanceof Color256)
{
$color = $color->value;
}
return self::escapeSequence('%d;5;%dm', $ground->value, $color);
}
/**
* Invert the foreground/background on a text segment
*
* @param string $text
* @return string
*/
public static function invert(string $text): string
{
return self::INVERSE_TEXT . $text . self::RESET_TEXT;
}
/**
* Generate a sequence for an rgb color
*
* @param int $r
* @param int $g
* @param int $b
* @return string
*/
public static function rgbColor(int $r, int $g, int $b): string
{
return self::escapeSequence('38;2;%d;%d;%dm', $r, $g, $b);
}
/**
* Move the cursor the specified position
*
* @param int $line
* @param int $column
* @return string
*/
public static function moveCursor(int $line, int $column): string
{
// The terminal has a 1-based coordinate system,
// add one to each to allow 0-based coordinate system input
return self::escapeSequence('%d;%dH', $line + 1, $column + 1);
}
/**
* Scroll the specified number of lines up
*
* @param int $lines
* @return string
*/
public static function scrollUp(int $lines): string
{
return self::escapeSequence('%dS', $lines);
}
/**
* Scroll the specified number of lines down
*
* @param int $lines
* @return string
*/
public static function scrollDown(int $lines): string
{
return self::escapeSequence('%dT', $lines);
}
/**
* Simplify creating ansi escape codes
*
* @param string $pattern
* @param mixed ...$args
* @return string
*/
private static function escapeSequence(string $pattern, mixed ...$args): string
{
return sprintf("\e[{$pattern}", ...$args);
}
}