Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Terminal Color Mode refactoring and force Color Mode #47407

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

Merged
merged 1 commit into from
Sep 7, 2022

Conversation

julien-boudry
Copy link
Contributor

Q A
Branch? 6.2 for features
Bug fix? no
New feature? yes
Deprecations? no
License MIT

Continue #46944

Terminal Color Mode refactoring: Adding a way to force color mode by the dev. user (with a new method), fewer getenv() calls (cache value), simpler tests (use the a new method and a new constant).

For example, it can be useful in an environment where none of the expected environment variables are available (Docker container...) , but where the support of a specific mode is imperative.

A future evolution could be to add an optional default option to all commands to force a particular mode by the final user.

@julien-boudry julien-boudry force-pushed the arcenciel2 branch 2 times, most recently from e851fd9 to d430f59 Compare August 27, 2022 14:17
@julien-boudry
Copy link
Contributor Author

julien-boudry commented Aug 27, 2022

Do you prefer as actually:

Terminal::getTermColorMode();
Terminal::setTermColorMode();
(private) Terminal::$termColorMode;

Or?

Terminal::getColorMode();
Terminal::setColorMode();
(private) Terminal::$colorMode;

@alamirault
Copy link
Contributor

Do you prefer as actually:

Terminal::getTermColorMode();
Terminal::setTermColorMode();
(private) Terminal::$termColorMode;

Or?

Terminal::getColorMode();
Terminal::setColorMode();
(private) Terminal::$colorMode;

I prefer Terminal::getColorMode();

…r mode by user, fewer getenv() calls, simpler tests.

It can be useful for example in the case of an environment where none of the expected environment variables are available (Docker container...) , but where the support of a specific mode is imperative.
@julien-boudry
Copy link
Contributor Author

I agree. I just push a new version with just theses naming changes.
For me, it's ready to merge.

@julien-boudry
Copy link
Contributor Author

julien-boudry commented Sep 7, 2022

About Psalm, which triggers some erros here. They are all false positives, because it is in fact impossible. The case is handled upstream in the getColorMode method, but Psalm is not smart enough for that.

There is - I think - only one way to deal with it:

  • Add a static method like resetColorMode(): void {self::$colorMode = null;}
  • Disallow null values as arguments to Terminal::setColorMode(AnsiColorMode $colorMode) instead of Terminal::setColorMode(?AnsiColorMode $colorMode).

However, this is an unnecessary burden on the public API to please psalm, when the code looks clean and logical to me.

Original Psalm log, actually:

Error: src/Symfony/Component/Console/Terminal.php:29:44: InvalidNullableReturnType: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but 'Symfony\Component\Console\Output\AnsiColorMode|null' contains null (see https://psalm.dev/144)
Error: src/Symfony/Component/Console/Terminal.php:43:24: NullableReturnStatement: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but the function returns 'null' (see https://psalm.dev/139)
Error: src/Symfony/Component/Console/Terminal.php:49:24: NullableReturnStatement: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but the function returns 'null' (see https://psalm.dev/139)
Error: src/Symfony/Component/Console/Terminal.php:60:24: NullableReturnStatement: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but the function returns 'null' (see https://psalm.dev/139)
Error: src/Symfony/Component/Console/Terminal.php:66:24: NullableReturnStatement: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but the function returns 'null' (see https://psalm.dev/139)
Error: src/Symfony/Component/Console/Terminal.php:72:16: NullableReturnStatement: The declared return type 'Symfony\Component\Console\Output\AnsiColorMode' for Symfony\Component\Console\Terminal::getColorMode is not nullable, but the function returns 'null' (see https://psalm.dev/139)
Error: Process completed with exit code 2.

@fabpot
Copy link
Member

fabpot commented Sep 7, 2022

Thank you @julien-boudry.

@fabpot fabpot merged commit 9560d73 into symfony:6.2 Sep 7, 2022
@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants