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
|
|
|
|
*/
|
2021-04-09 19:40:28 -04:00
|
|
|
public const RESET_TEXT = "\e[m";
|
2019-12-04 10:54:15 -05:00
|
|
|
|
|
|
|
public const BOLD_TEXT = "\e[1m";
|
|
|
|
|
2021-08-13 11:16:57 -04:00
|
|
|
public const DIM_TEXT = "\e[2m";
|
|
|
|
|
|
|
|
public const ITALIC_TEXT = "\d[3m";
|
|
|
|
|
2019-12-04 10:54:15 -05:00
|
|
|
public const UNDERLINE_TEXT = "\e[4m";
|
|
|
|
|
2021-08-13 11:16:57 -04:00
|
|
|
public const BLINKING_TEXT = "\e[5m";
|
|
|
|
|
|
|
|
public const INVERSE_TEXT = "\e[7m";
|
|
|
|
|
|
|
|
public const STRIKE_TEXT = "\e[9m";
|
|
|
|
|
2019-12-04 10:54:15 -05:00
|
|
|
/**
|
|
|
|
* 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";
|
|
|
|
|
|
|
|
/**
|
2021-08-13 11:16:57 -04:00
|
|
|
* Generate the ascii sequence for basic foreground text color
|
2019-12-04 10:54:15 -05:00
|
|
|
*
|
2021-12-03 11:59:42 -05:00
|
|
|
* @param Enum\Color $color
|
2019-12-04 10:54:15 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-12-03 11:59:42 -05:00
|
|
|
public static function color(Enum\Color $color): string
|
2019-12-04 10:54:15 -05:00
|
|
|
{
|
2021-12-03 11:59:42 -05:00
|
|
|
return self::escapeSequence('%dm', $color->value);
|
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
|
|
|
|
{
|
2021-03-09 17:22:49 -05:00
|
|
|
return self::escapeSequence('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
|
|
|
|
{
|
2021-03-05 20:47:59 -05:00
|
|
|
// The terminal has a 1-based coordinate system,
|
|
|
|
// add one to each to allow 0-based coordinate system input
|
2021-03-09 17:22:49 -05:00
|
|
|
return self::escapeSequence('%d;%dH', $line + 1, $column + 1);
|
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
|
|
|
|
{
|
2021-03-09 17:22:49 -05:00
|
|
|
return self::escapeSequence('%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
|
|
|
|
{
|
2021-03-09 17:22:49 -05:00
|
|
|
return self::escapeSequence('%dT', $lines);
|
2019-12-05 14:54:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:11:20 -05:00
|
|
|
/**
|
|
|
|
* Simplify creating ansi escape codes
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @param mixed ...$args
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-09 17:22:49 -05:00
|
|
|
private static function escapeSequence(string $pattern, mixed ...$args): string
|
2019-12-05 14:54:00 -05:00
|
|
|
{
|
|
|
|
return sprintf("\e[{$pattern}", ...$args);
|
2019-12-04 10:54:15 -05:00
|
|
|
}
|
|
|
|
}
|