Файл: vendor/psy/psysh/src/Readline/Hoa/ConsoleCursor.php
Строк: 312
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2017, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace PsyReadlineHoa;
/**
* Class HoaConsoleCursor.
*
* Allow to manipulate the cursor.
*/
class ConsoleCursor
{
/**
* Move the cursor.
* Steps can be:
* • u, up, ↑ : move to the previous line;
* • U, UP : move to the first line;
* • r, right, → : move to the next column;
* • R, RIGHT : move to the last column;
* • d, down, ↓ : move to the next line;
* • D, DOWN : move to the last line;
* • l, left, ← : move to the previous column;
* • L, LEFT : move to the first column.
* Steps can be concatened by a single space if $repeat is equal to 1.
*/
public static function move(string $steps, int $repeat = 1)
{
if (1 > $repeat) {
return;
} elseif (1 === $repeat) {
$handle = explode(' ', $steps);
} else {
$handle = explode(' ', $steps, 1);
}
$tput = Console::getTput();
$output = Console::getOutput();
foreach ($handle as $step) {
switch ($step) {
case 'u':
case 'up':
case '↑':
$output->writeAll(
str_replace(
'%p1%d',
$repeat,
$tput->get('parm_up_cursor')
)
);
break;
case 'U':
case 'UP':
static::moveTo(null, 1);
break;
case 'r':
case 'right':
case '→':
$output->writeAll(
str_replace(
'%p1%d',
$repeat,
$tput->get('parm_right_cursor')
)
);
break;
case 'R':
case 'RIGHT':
static::moveTo(9999);
break;
case 'd':
case 'down':
case '↓':
$output->writeAll(
str_replace(
'%p1%d',
$repeat,
$tput->get('parm_down_cursor')
)
);
break;
case 'D':
case 'DOWN':
static::moveTo(null, 9999);
break;
case 'l':
case 'left':
case '←':
$output->writeAll(
str_replace(
'%p1%d',
$repeat,
$tput->get('parm_left_cursor')
)
);
break;
case 'L':
case 'LEFT':
static::moveTo(1);
break;
}
}
}
/**
* Move to the line X and the column Y.
* If null, use the current coordinate.
*/
public static function moveTo(int $x = null, int $y = null)
{
if (null === $x || null === $y) {
$position = static::getPosition();
if (null === $x) {
$x = $position['x'];
}
if (null === $y) {
$y = $position['y'];
}
}
Console::getOutput()->writeAll(
str_replace(
['%i%p1%d', '%p2%d'],
[$y, $x],
Console::getTput()->get('cursor_address')
)
);
}
/**
* Get current position (x and y) of the cursor.
*/
public static function getPosition(): array
{
$tput = Console::getTput();
$user7 = $tput->get('user7');
if (null === $user7) {
return [
'x' => 0,
'y' => 0,
];
}
Console::getOutput()->writeAll($user7);
$input = Console::getInput();
// Read $tput->get('user6').
$input->read(2); // skip