Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
6 / 6
ANSI
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
6 / 6
 color
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 rgbColor
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 moveCursor
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 scrollUp
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 scrollDown
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 escapeSequence
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
1<?php declare(strict_types=1);
2
3namespace Aviat\Kilo;
4
5/**
6 * ANSI
7 */
8class ANSI {
9    // ------------------------------------------------------------------------
10    // General ANSI standard escape sequences
11    // ------------------------------------------------------------------------
12
13    /**
14     * Clear the terminal window
15     */
16    public const CLEAR_SCREEN = "\e[2J";
17
18    /**
19     * Clear the terminal line
20     */
21    public const CLEAR_LINE = "\e[K";
22
23    // ------------------------------------------------------------------------
24    // Text formatting escape sequences
25    // ------------------------------------------------------------------------
26
27    /**
28     * Removes text attributes, such as bold, underline, blink, inverted colors
29     */
30    public const RESET_TEXT = "\e[m";
31
32    public const BOLD_TEXT = "\e[1m";
33
34    public const UNDERLINE_TEXT = "\e[4m";
35
36    /**
37     * Move the cursor to position 0,0 which is the top left
38     */
39    public const RESET_CURSOR = "\e[H";
40
41    // ------------------------------------------------------------------------
42    // VT-series escapes, not really ANSI standard
43    // ------------------------------------------------------------------------
44
45    public const HIDE_CURSOR = "\e[?25l";
46    public const SHOW_CURSOR = "\e[?25h";
47
48    /**
49     * Generate the ascii sequence for basic text color
50     *
51     * @param int $color
52     * @return string
53     */
54    public static function color(int $color): string
55    {
56        return self::escapeSequence('%dm', $color);
57    }
58
59    /**
60     * Generate a sequence for an rgb color
61     *
62     * @param int $r
63     * @param int $g
64     * @param int $b
65     * @return string
66     */
67    public static function rgbColor(int $r, int $g, int $b): string
68    {
69        return self::escapeSequence('38;2;%d;%d;%dm', $r, $g, $b);
70    }
71
72    /**
73     * Move the cursor the specified position
74     *
75     * @param int $line
76     * @param int $column
77     * @return string
78     */
79    public static function moveCursor(int $line, int $column): string
80    {
81        // The terminal has a 1-based coordinate system,
82        // add one to each to allow 0-based coordinate system input
83        return self::escapeSequence('%d;%dH', $line + 1, $column + 1);
84    }
85
86    /**
87     * Scroll the specified number of lines up
88     *
89     * @param int $lines
90     * @return string
91     */
92    public static function scrollUp(int $lines): string
93    {
94        return self::escapeSequence('%dS', $lines);
95    }
96
97    /**
98     * Scroll the specified number of lines down
99     *
100     * @param int $lines
101     * @return string
102     */
103    public static function scrollDown(int $lines): string
104    {
105        return self::escapeSequence('%dT', $lines);
106    }
107
108    /**
109     * Simplify creating ansi escape codes
110     *
111     * @param string $pattern
112     * @param mixed ...$args
113     * @return string
114     */
115    private static function escapeSequence(string $pattern, mixed ...$args): string
116    {
117        return sprintf("\e[{$pattern}", ...$args);
118    }
119}