2019-12-04 10:54:15 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Aviat\Kilo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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[0m";
|
|
|
|
|
|
|
|
public const BOLD_TEXT = "\e[1m";
|
|
|
|
|
|
|
|
public const UNDERLINE_TEXT = "\e[4m";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 text color
|
|
|
|
*
|
|
|
|
* @param int $color
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function color(int $color): string
|
|
|
|
{
|
2019-12-05 14:54:00 -05:00
|
|
|
return self::seq('%dm', $color);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:54:00 -05:00
|
|
|
/**
|
|
|
|
* Generate a sequence for an rgb color
|
|
|
|
*
|
|
|
|
* @param int $r
|
|
|
|
* @param int $g
|
|
|
|
* @param int $b
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-04 10:54:15 -05:00
|
|
|
public static function rgbColor(int $r, int $g, int $b): string
|
|
|
|
{
|
2019-12-05 14:54:00 -05:00
|
|
|
return self::seq('38;2;%d;%d;%dm', $r, $g, $b);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor the specified position
|
|
|
|
*
|
|
|
|
* @param int $line
|
|
|
|
* @param int $column
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function moveCursor(int $line, int $column): string
|
|
|
|
{
|
2019-12-05 14:54:00 -05:00
|
|
|
return self::seq('%d;%dH', $line, $column);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll the specified number of lines up
|
|
|
|
*
|
|
|
|
* @param int $lines
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function scrollUp(int $lines): string
|
|
|
|
{
|
2019-12-05 14:54:00 -05:00
|
|
|
return self::seq('%dS', $lines);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll the specified number of lines down
|
|
|
|
*
|
|
|
|
* @param int $lines
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function scrollDown(int $lines): string
|
|
|
|
{
|
2019-12-05 14:54:00 -05:00
|
|
|
return self::seq('%dT', $lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function seq(string $pattern, ...$args): string
|
|
|
|
{
|
|
|
|
return sprintf("\e[{$pattern}", ...$args);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
}
|