Cursor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 107
rs 10
c 1
b 0
f 0
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A left() 0 3 1
A up() 0 3 1
A prev() 0 3 1
A clear() 0 3 1
A down() 0 3 1
A clearDown() 0 3 1
A moveTo() 0 3 1
A right() 0 3 1
A clearUp() 0 3 1
A next() 0 3 1
A eraseLine() 0 3 1
1
<?php
2
3
/**
4
 * Platine Console
5
 *
6
 * Platine Console is a powerful library with support of custom
7
 * style to build command line interface applications
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Console
12
 * Copyright (c) 2017-2020 Jitendra Adhikari
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Cursor.php
35
 *
36
 *  The Output Cursor class
37
 *
38
 *  @package    Platine\Console\Output
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Console\Output;
50
51
/**
52
 * @class Cursor
53
 * @package Platine\Console\Output
54
 */
55
class Cursor
56
{
57
    /**
58
     * Returns signal to move cursor up "count" times.
59
     * @param int $count
60
     * @return string
61
     */
62
    public function up(int $count = 1): string
63
    {
64
        return sprintf("\e[%dA", max($count, 1));
65
    }
66
67
    /**
68
     * Returns signal to move cursor down "count" times.
69
     * @param int $count
70
     * @return string
71
     */
72
    public function down(int $count = 1): string
73
    {
74
        return sprintf("\e[%dB", max($count, 1));
75
    }
76
77
    /**
78
     * Returns signal to move cursor right "count" times.
79
     * @param int $count
80
     * @return string
81
     */
82
    public function right(int $count = 1): string
83
    {
84
        return sprintf("\e[%dC", max($count, 1));
85
    }
86
87
    /**
88
     * Returns signal to move cursor left "count" times.
89
     * @param int $count
90
     * @return string
91
     */
92
    public function left(int $count = 1): string
93
    {
94
        return sprintf("\e[%dD", max($count, 1));
95
    }
96
97
    /**
98
     * Returns signal to move cursor next line "count" times.
99
     * @param int $count
100
     * @return string
101
     */
102
    public function next(int $count = 1): string
103
    {
104
        return str_repeat("\e[E", max($count, 1));
105
    }
106
107
    /**
108
     * Returns signal to move cursor previous line "count" times.
109
     * @param int $count
110
     * @return string
111
     */
112
    public function prev(int $count = 1): string
113
    {
114
        return str_repeat("\e[F", max($count, 1));
115
    }
116
117
    /**
118
     * Returns signal to erase current line.
119
     * @return string
120
     */
121
    public function eraseLine(): string
122
    {
123
        return "\e[2K";
124
    }
125
126
    /**
127
     * Returns signal to clear string.
128
     * @return string
129
     */
130
    public function clear(): string
131
    {
132
        return "\e[2K";
133
    }
134
135
    /**
136
     * Returns signal to erase lines upward.
137
     * @return string
138
     */
139
    public function clearUp(): string
140
    {
141
        return "\e[1J";
142
    }
143
144
    /**
145
     * Returns signal to erase lines downward.
146
     * @return string
147
     */
148
    public function clearDown(): string
149
    {
150
        return "\e[J";
151
    }
152
153
    /**
154
     * Returns signal to move cursor to given x, y position.
155
     * @param int $x
156
     * @param int $y
157
     * @return string
158
     */
159
    public function moveTo(int $x, int $y): string
160
    {
161
        return sprintf("\e[%d;%dH", $y, $x);
162
    }
163
}
164