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
 seq
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[0m";
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::seq('%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::seq('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        return self::seq('%d;%dH', $line, $column);
82    }
83
84    /**
85     * Scroll the specified number of lines up
86     *
87     * @param int $lines
88     * @return string
89     */
90    public static function scrollUp(int $lines): string
91    {
92        return self::seq('%dS', $lines);
93    }
94
95    /**
96     * Scroll the specified number of lines down
97     *
98     * @param int $lines
99     * @return string
100     */
101    public static function scrollDown(int $lines): string
102    {
103        return self::seq('%dT', $lines);
104    }
105
106    /**
107     * Simplify creating ansi escape codes
108     *
109     * @param string $pattern
110     * @param mixed ...$args
111     * @return string
112     */
113    private static function seq(string $pattern, ...$args): string
114    {
115        return sprintf("\e[{$pattern}", ...$args);
116    }
117}