<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abel Orban</title>
    <description>The latest articles on DEV Community by Abel Orban (@slendersnax).</description>
    <link>https://dev.to/slendersnax</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F932363%2F486f723e-c8a6-4c7b-99e9-594447275f7d.jpeg</url>
      <title>DEV Community: Abel Orban</title>
      <link>https://dev.to/slendersnax</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slendersnax"/>
    <language>en</language>
    <item>
      <title>A way to enable ANSI escape sequences in the Windows virtual terminal using Python</title>
      <dc:creator>Abel Orban</dc:creator>
      <pubDate>Sun, 25 Sep 2022 22:35:41 +0000</pubDate>
      <link>https://dev.to/slendersnax/a-way-to-enable-ansi-escape-sequences-in-the-windows-virtual-terminal-using-python-32ok</link>
      <guid>https://dev.to/slendersnax/a-way-to-enable-ansi-escape-sequences-in-the-windows-virtual-terminal-using-python-32ok</guid>
      <description>&lt;p&gt;One of the projects that I'm currently working on is an implementation of &lt;a href="https://en.wikipedia.org/wiki/Spider_(solitaire)"&gt;the classic Spider Solitaire game&lt;/a&gt; that is played in the console, made using Python. &lt;/p&gt;

&lt;p&gt;To make the output easier on the eyes, I intended to change the colour of a few details using escape sequences, as I've used them before. During my first test to see if I managed to format the output correctly, I was surprised to see the sequences in string format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;←[90mXX←[97m ←[90mXX←[97m ←[90mXX←[97m ←[90mXX←[97m ←[90mXX←[97m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was even more surprising as I had already implemented &lt;a href="https://github.com/slendersnax/python-2048"&gt;2048 in a similar way&lt;/a&gt;, in the console, and it was working fine.&lt;/p&gt;

&lt;p&gt;After some digging, I found out that while the Windows terminal supports ANSI escape sequences, this feature &lt;em&gt;isn't enabled by default&lt;/em&gt;. You have to &lt;a href="https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing"&gt;enable it from within the application&lt;/a&gt;, something which is pretty easily done &lt;a href="https://stackoverflow.com/a/60194390"&gt;in Python as well&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I was still wondering why it was working in the 2048 implementation but not in this project, and I managed to figure it out after some inspection. I still hadn't implemented cleaning the screen in Spider Solitaire, which I did with the &lt;code&gt;os&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;os.system("cls")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, since I was planning on porting it to Linux anyway, as little effort as that may require with Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;os.system("cls" if os.name == "nt" else "clear")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suddenly, the output was coloured:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iwev74-5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hz06oouf3dmxmvxh69u4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iwev74-5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hz06oouf3dmxmvxh69u4.png" alt="the result" width="318" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had to implement clearing the screen anyway, but I never expected that this little bit of code had such an overarching effect. &lt;/p&gt;

&lt;p&gt;I'm not sure if Microsoft knows of this, as their page about &lt;a href="https://learn.microsoft.com/en-us/windows/console/clearing-the-screen"&gt;clearing the console screen&lt;/a&gt; also includes enabling the escape sequences. It may just be the overconfidence of youth talking, though.&lt;/p&gt;

&lt;p&gt;To check that it wasn't some background Python magic I wrote a small C++ program to check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;windows.h&amp;gt;

int main() {
    system("cls");
    std::cout &amp;lt;&amp;lt; "\033[31mHello, red\033[97m" &amp;lt;&amp;lt; std::endl;
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lo, and behold, without &lt;code&gt;system("cls")&lt;/code&gt;, it outputs all the characters, but with it, only the short text in the desired red colour:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ptQQ6Ln9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viu9uo299afryg85bg49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ptQQ6Ln9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viu9uo299afryg85bg49.png" alt="" width="320" height="24"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LLRA-wcz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyehuywsxde9quay527x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LLRA-wcz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyehuywsxde9quay527x.png" alt="" width="261" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It may have something to do with the fact that I'm still using Windows 10, even though I doubt it, but it is an interesting and odd observation nonetheless.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>python</category>
      <category>firstpost</category>
      <category>console</category>
    </item>
  </channel>
</rss>
