Cyotek Development Bloghttps://devblog.cyotek.com/tag/console/atom.xml2015-04-09T20:13:47ZColorEcho - adding colour to echoed batch texturn:uuid:532af147-0199-42a8-a1c4-f2b7e717af062015-04-09T20:13:47Z2015-04-09T20:13:47Z<p>We use batch files for... well, pretty much everything. From
simple files that simple optimize modified graphics, to the
tendril-like files that build our software. For some time now,
I've been using <code>cecho.exe</code> from a <a href="http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files" rel="external nofollow noopener">CodeProject article</a> so
that I highlight errors and successes. And this has worked fine
- as long as I was running the scripts in a console window.</p>
<p>However, when <a href="http://www.rickglos.com/post/How-to-run-windows-batch-files-from-Visual-Studio-2010-Solution-Explorer.aspx" rel="external nofollow noopener">running batch files through Visual Studio</a> any
output from <code>cecho.exe</code> simply wasn't displayed. That was
something I ignored. However, over the last couple of days I've
been finally setting up a CI server and have been testing both
<a href="http://jenkins-ci.org/" rel="external nofollow noopener">Jenkins</a> and <a href="https://www.jetbrains.com/teamcity/" rel="external nofollow noopener">TeamCity</a> and I had the exact same
behaviour of blank lines when running builds in both of these
tools - that I can't ignore.</p>
<p>I had a cursory glance through the C++ code from the original
article and while it looks fine, I make no pretence of being a
C++ developer. Given that the past two weeks I've been working
with PHP and F#, I not in a hurry to study a 3rd extra language!</p>
<p>I had observed that my own console tools which used coloured
output appeared perfectly well in Visual Studio, Jenkins and
TeamCity so I decided I would replicate the <code>cecho.exe</code> tool
using C#.</p>
<h2 id="using-the-tool">Using the tool</h2>
<p>As I'm not in a hurry to change all the batch files calling
<code>cecho</code> I've kept the exact same syntax (and for the time being
the same annoying behaviour regarding having to manually reset
colours and include line breaks).</p>
<ul>
<li><code>{XX}</code>: colours coded as two hexadecimal digits. E.g., <code>{0A}</code>
light green</li>
<li><code>{color}</code>: colour information as understandable text. E.g.,
<code>{light red on black}</code></li>
<li><code>{\n}</code>: New line character</li>
<li><code>{\t}</code>: Tab character</li>
<li><code>{\u0000}</code>: Unicode character code</li>
<li><code>{{</code>: escape character <code>{</code></li>
<li><code>{#}</code>: restore foreground colour</li>
<li><code>{##}</code>: restore foreground and background colour</li>
</ul>
<p>Colours are defined as</p>
<ul>
<li><code>0</code>: Black (<code>black</code>)</li>
<li><code>1</code>: Dark Blue (<code>navy</code>, <code>dark blue</code>)</li>
<li><code>2</code>: Dark Green (<code>green</code>, <code>dark green</code>)</li>
<li><code>3</code>: Dark Cyan (<code>teal</code>, <code>dark cyan</code>)</li>
<li><code>4</code>: Dark Red (<code>maroon</code>, <code>dark red</code>)</li>
<li><code>5</code>: Dark Magenta (<code>purple</code>, <code>dark magenta</code>)</li>
<li><code>6</code>: Dark Yellow (<code>olive</code>, <code>brown</code>, <code>dark yellow</code>)</li>
<li><code>7</code>: Gray (<code>silver</code>, <code>light gray</code>, <code>light grey</code>)</li>
<li><code>8</code>: Dark Gray (<code>gray</code>, <code>grey</code>, <code>dark gray</code>, <code>dark grey</code>)</li>
<li><code>9</code>: Blue (<code>blue</code>, <code>light blue</code>)</li>
<li><code>A</code>: Green (<code>lime</code>, <code>light green</code>)</li>
<li><code>B</code>: Cyan (<code>aqua</code>, <code>light cyan</code>)</li>
<li><code>C</code>: Red (<code>red</code>, <code>light red</code>)</li>
<li><code>D</code>: Magenta (<code>fuschia</code>, <code>magenta</code>, <code>light magenta</code>)</li>
<li><code>E</code>: Yellow (<code>yellow</code>)</li>
<li><code>F</code>: White (<code>white</code>)</li>
</ul>
<p>The names in brackets are alternatives you can use for
understandable text.</p>
<h2 id="note">Note</h2>
<p>For backwards compatibility, this program behaves the same way
as the original <code>cecho</code> - lines are not terminated with a
carriage return and the colours are not reset. Therefore you
should ensure you include <code>{#}</code> or <code>{##}</code> and <code>{\n}</code> at the end
of your statements.</p>
<p>Or of course, just modify the source to do this automatically if
you don't need compatibility.</p>
<h2 id="samples">Samples</h2>
<p>This first example uses the shorthand notation to change
<code>ERROR:</code> into red.</p>
<figure class="lang-bat highlight"><figcaption><span>bat</span></figcaption><pre class="code">
cecho {0c}ERROR:{#} Signing failed for program1.exe, retrying{\n}
</pre>
</figure>
<figure class="screenshot" ><a href="https://images.cyotek.com/image/devblog/colorecho-1a.png" class="gallery" title="Basic example using hex codes" ><img src="https://images.cyotek.com/image/thumbnail/devblog/colorecho-1a.png" alt="Basic example using hex codes" decoding="async" loading="lazy" /></a><figcaption>Basic example using hex codes</figcaption></figure>
<p>This example uses named colours instead of hex codes.</p>
<figure class="lang-bat highlight"><figcaption><span>bat</span></figcaption><pre class="code">
cecho This {yellow <span class="keyword">on</span> teal}word{##} is yellow <span class="keyword">on</span> a teal background{\n}
</pre>
</figure>
<figure class="screenshot" ><a href="https://images.cyotek.com/image/devblog/colorecho-1b.png" class="gallery" title="Basic example using colour names" ><img src="https://images.cyotek.com/image/thumbnail/devblog/colorecho-1b.png" alt="Basic example using colour names" decoding="async" loading="lazy" /></a><figcaption>Basic example using colour names</figcaption></figure>
<p>This final example prints out an extended character.</p>
<figure class="lang-bat highlight"><figcaption><span>bat</span></figcaption><pre class="code">
cecho {\u2593}
</pre>
</figure>
<figure class="screenshot" ><a href="https://images.cyotek.com/image/devblog/colorecho-1c.png" class="gallery" title="Printing extended characters" ><img src="https://images.cyotek.com/image/thumbnail/devblog/colorecho-1c.png" alt="Printing extended characters" decoding="async" loading="lazy" /></a><figcaption>Printing extended characters</figcaption></figure><h2 id="getting-the-source">Getting the source</h2>
<p>The source code this tool is available on our <a href="https://github.com/cyotek/ColorEcho" rel="external nofollow noopener">GitHub</a> page.</p>
<h2 id="update-history">Update History</h2>
<ul>
<li>2015-04-09 - First published</li>
<li>2020-11-21 - Updated formatting</li>
</ul>

<p><small>
All content <a href="https://devblog.cyotek.com/copyright-and-trademarks">Copyright (c) by Cyotek Ltd</a> or its respective writers. Permission to reproduce news and web log entries and other RSS feed content in unmodified form without notice is granted provided they are not used to endorse or promote any products or opinions (other than what was expressed by the author) and without taking them out of context. Written permission from the copyright owner must be obtained for everything else.<br />Original URL of this content is https://devblog.cyotek.com/post/colorecho-adding-colour-to-echoed-batch-text .
</small></p>Richard Mosshttps://www.cyotek.com/richard.moss@cyotek.com