Skip to content
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/Output: cursor and linelength #28

Closed
dmvdbrugge opened this issue Nov 27, 2017 · 4 comments
Closed

Console/Output: cursor and linelength #28

dmvdbrugge opened this issue Nov 27, 2017 · 4 comments

Comments

@dmvdbrugge
Copy link
Contributor

dmvdbrugge commented Nov 27, 2017

Not sure if bug or mis-use but shouldn't cursorBack and -Forward affect the linelength in some way? Consider this case (overly simplified version obviously):

// Inside a Console\App::run()
for ($i = 1; $i <= 1000; $i++) {
    // I could use clearLine() instead, however: I know for sure that whatever I'm
    // writing >= what's already there, thus no need for the actual clearing
    $this->output->cursorBack($this->output->getLineLength());
    $this->output->write($i);
}

$this->output->clearLine();
$this->output->writeln('All done');

What is the final result of this? You'd think it'd be just a single line "All done". However, due to how it works internally, it prints 9 * 1 + 90 * 2 + 900 * 3 + 1 * 4 = 2893 spaces (obviously spanning multiple lines), then tries to move back 2893 places but only goes as far as the start of the current line, and then prints "All done" on the beginning of the line, resulting in a lot of unexpected "blank" lines.

Solution (if you don't consider this a bug) might be something like a new function cursorReset which moves the cursor to the beginning of the line and sets linelength back to 0. This loses the ability to clear the current line, however seeing my given case that's not always needed anyway: replace the cursorBack call with this cursorReset and all behaves as expected ( / needed for this scenario).

However while my case is an extreme one, the mismatch can happen way earlier. Think of a console width of 80 characters, I print 45, go back 15, print another 45. Actual line is now 75, all one line. However because of linelength, clearLine() will print 90 spaces, thus basically inserting a newline.

@devvoh
Copy link
Owner

devvoh commented Nov 27, 2017

Hm, the reason the cursor movement methods don't influence the lineLength is because they're not quite compatible, as you showed. Especially when you consider cursorForward(), cursorBackward() and cursorPlace().

The most fitting solution here is that as soon as you use a cursor-movement method, lineLength gets reset to 0. If I'm reading your example well that would work as your expected behavior states.

@devvoh
Copy link
Owner

devvoh commented Nov 27, 2017

There are some other solutions possible, but I feel they might get really complicated very quickly.

I think the solution I mentioned before should be enough, and then add some comments to clearLine() to specify that it will give unreliable results in combination with cursor movement.

@devvoh
Copy link
Owner

devvoh commented Nov 27, 2017

(additionally, after trying your example, the lineLength is 18260, and on my full-screen terminal it ends up just clearing the screen and printing "All done", hehe)

@dmvdbrugge
Copy link
Contributor Author

Fixed by #27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants