Search code examples
powershellpython-poetrywindows-terminal

Is there a way to activate colored output for Poetry in Windows Terminal?


I am asking this question because I have not found an answer before. I recently switched to Windows Terminal and some commands that the "Command Line" or "PowerShell" used to send me with syntax highlighting, are now sent to me without.

Example: on the left the Command Line and PowerShell opened via Windows Terminal, on the right without.

I don't think this is specifically due to the command I am running.

Is there a way to get back the same syntax highlighting as before? If not, I am interested to know what can explain this difference?

I already tried to reboot my computer after the installation.


Solution

  • This issue was reported here in poetry's GitHub repo: No colors in Windows Terminal or cmd #1972. A fix has since been made here: https://github.com/python-poetry/cleo/pull/313.

    Old answer

    At the time of this writing, poetry doesn't use ANSI escapes by default in the Windows Terminal, but you can force it to by passing the --ansi flag, or setting the ANSICON environment variable. This behaviour is due to a dependency that poetry uses called cleo, and the underlying issue is tracked in the corresponding GitHub repo: cleo fails to detect ANSI support in Windows when run under a VT-native environment #104.

    If you're curious about things at a deeper level- how ANSI escape support can be enabled/disabled in a Windows console, see https://superuser.com/q/413073/1749748.

    As mentioned in the comments there by Yaekiou, you can create an alias of poetry that adds the ansi flag by default, but I don't see why one would do that instead of just modifying the system environment variables or shell profile script to add the ANSICON variable.