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

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
6 participants
@pierredup
Copy link
Contributor

pierredup commented May 31, 2018

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

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']);
    $output->write($part['string']);

    usleep(200000);
}

$cursor->moveToPosition(1, 8);

Result:

kapture 2018-05-31 at 12 39 10

@javiereguiluz

This comment has been minimized.

Copy link
Member

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:

https://godoc.org/github.com/nsf/termbox-go

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

https://godoc.org/github.com/gdamore/tcell

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

.Net (https://msdn.microsoft.com/en-us/library/system.console_methods(v=vs.110).aspx)

SetCursorPosition(Int32, Int32)

And some properties:

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

https://github.com/cronvel/terminal-kit/blob/master/doc/low-level.md#ref.movingCursor

.saveCursor()
.restoreCursor()
.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)
.resetScrollingRegion()
.moveTo(int x, int y)
.move(int x, int y)
.hideCursor()
.tabSet()
.tabClear()
.tabClearAll()
.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.

@ogizanagi

This comment has been minimized.

Copy link
Member

ogizanagi commented Jun 1, 2018

Also see the HOA Project Console Cursor API: https://github.com/hoaproject/Console#cursor

@yceruto
Copy link
Contributor

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

@nicolas-grekas

This comment has been minimized.

Copy link
Member

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

@pierredup

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