New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Add Cursor class to control the cursor in the terminal #27444

wants to merge 1 commit into
base: master


None yet
6 participants
Copy link

pierredup commented May 31, 2018

Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT

Add a new Cursor class to the Console component, that allows to manipulate the cursor in the terminal.

Options to move the cursor include: moveUp, moveDown, moveLeft, moveRight, show, hide, save and restore position, get current position etc.

Example usage:

$cursor = new Cursor($output);

$parts = [
    ['x' => 9, 'y' => 5, 'string' => '#'],
    ['x' => 9, 'y' => 7, 'string' => '#'],
    ['x' => 2, 'y' => 7, 'string' => '#####'],
    ['x' => 9, 'y' => 1, 'string' => '#######'],
    ['x' => 7, 'y' => 6, 'string' => '#'],
    ['x' => 9, 'y' => 2, 'string' => '#'],
    ['x' => 9, 'y' => 6, 'string' => '#'],
    ['x' => 9, 'y' => 3, 'string' => '#'],
    ['x' => 2, 'y' => 1, 'string' => '#####'],
    ['x' => 7, 'y' => 2, 'string' => '#'],
    ['x' => 9, 'y' => 4, 'string' => '#####'],
    ['x' => 1, 'y' => 2, 'string' => '#'],
    ['x' => 1, 'y' => 6, 'string' => '#'],
    ['x' => 2, 'y' => 4, 'string' => '#####'],
    ['x' => 1, 'y' => 3, 'string' => '#'],
    ['x' => 7, 'y' => 5, 'string' => '#'],

foreach ($parts as $part) {
    $cursor->moveToPosition($part['x'], $part['y']);


$cursor->moveToPosition(1, 8);


kapture 2018-05-31 at 12 39 10


This comment has been minimized.

Copy link

javiereguiluz commented May 31, 2018

For reference purposes, I've look for the APIs for this feature in other projects:

  1. Go libraries and .Net libraries have just a few methods:

func HideCursor()
func SetCursor(x, y int)

func HideCursor()
func ShowCursor(x int, y int)

.Net (

SetCursorPosition(Int32, Int32)

And some properties:

  1. On the other hand, JS libraries define a ton of methods:

.up(int n)
.down(int n)
.right(int n)
.left(int n)
.nextLine(int n)
.previousLine(int n)
.column(int x)
.scrollUp(int n)
.scrollDown(int n)
.scrollingRegion(top, bottom)
.moveTo(int x, int y)
.move(int x, int y)
.forwardTab(int n)
.backwardTab(int n)

So we need to think if we want a simple (but enough) API or a complex (but useful) API.


This comment has been minimized.

Copy link

ogizanagi commented Jun 1, 2018

Also see the HOA Project Console Cursor API:

Copy link

yceruto left a comment

Thanks for this feature! I leave two minor comments.

Show resolved Hide resolved src/Symfony/Component/Console/Cursor.php Outdated
Show resolved Hide resolved src/Symfony/Component/Console/Tests/CursorTest.php Outdated

@nicolas-grekas nicolas-grekas added this to the next milestone Jun 1, 2018

@pierredup pierredup force-pushed the pierredup:cursor-helper branch from 344100d to 78752d6 Jun 3, 2018


This comment has been minimized.

Copy link

nicolas-grekas commented Jun 19, 2018

@pierredup see test failures :) Also, any comment when comparing the proposed API with the linked ones? Do any of them provide any idea to borrow?

@pierredup pierredup force-pushed the pierredup:cursor-helper branch from 78752d6 to 7501b0f Feb 11, 2019

@pierredup pierredup force-pushed the pierredup:cursor-helper branch from 7501b0f to 2ad0290 Feb 11, 2019


This comment has been minimized.

Copy link
Contributor Author

pierredup commented Feb 11, 2019

I prefer having a few explicit methods (E.G moveUp, moveDown) instead of just a couple generic methods, so I would much rather go the route of the JS libraries (although not as many methods as they have).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment