cecho - simple, fast & efficient colors for batch

Discussion in 'Scripting' started by kebabstorm, Apr 25, 2022.

  1. kebabstorm

    kebabstorm MDL Novice

    Aug 3, 2016
    37
    30
    0
    #1 kebabstorm, Apr 25, 2022
    Last edited: Apr 28, 2022
    cecho - small executable for showing colors in console.

    Fast - pure C code, no CRT, no temp files, no inefficient codepaths
    Compatible - imports only from ntdll.dll/kernel32.dll, uses functions available since Windows 2000 , uses the most efficient versions of string functions (from ntdll instead of kernel32 or msvcrt or god forbid, the new universal CRT)
    Small - 6KB (6656 bytes) size makes it good for embedded applications.

    Otherwise works just like regular echo with more control (newlines, tabs, unicode chars) , can detect whether you actually have a console or have redirected stdout (f.ex with ">") and strips out the colors so redirecting output works too.


    cecho.png


    Code:
    @setlocal enableextensions disabledelayedexpansion
    @echo off &pushd "%~dp0"
    @set cecho=echo
    
    if not exist cecho.exe call :x_cecho
    if exist cecho.exe set cecho=cecho
    
    ::
    :: cecho by thomas_polaert@yahoo.fr & anzz1
    ::
    echo cecho by thomas_polaert@yahoo.fr ^& anzz1
    
    %cecho%
    
    %cecho% {00}00 - black
    %cecho% {01}01 - navy
    %cecho% {02}02 - green
    %cecho% {03}03 - teal
    %cecho% {04}04 - maroon
    %cecho% {05}05 - purple
    %cecho% {06}06 - olive
    %cecho% {07}07 - silver
    %cecho% {08}08 - gray
    %cecho% {09}09 - blue
    %cecho% {0A}0A - lime
    %cecho% {0B}0B - aqua
    %cecho% {0C}0C - red
    %cecho% {0D}0D - fuchsia
    %cecho% {0E}0E - yellow
    %cecho% {0F}0F - white
    
    echo.
    
    %cecho% {00}00 {10}10 {20}20 {30}30 {40}40 {50}50 {60}60 {70}70 {80}80 {90}90 {A0}A0 {B0}B0 {C0}C0 {D0}D0 {E0}E0 {F0}F0
    %cecho% {01}01 {11}11 {21}21 {31}31 {41}41 {51}51 {61}61 {71}71 {81}81 {91}91 {A1}A1 {B1}B1 {C1}C1 {D1}D1 {E1}E1 {F1}F1
    %cecho% {02}02 {12}12 {22}22 {32}32 {42}42 {52}52 {62}62 {72}72 {82}82 {92}92 {A2}A2 {B2}B2 {C2}C2 {D2}D2 {E2}E2 {F2}F2
    %cecho% {03}03 {13}13 {23}23 {33}33 {43}43 {53}53 {63}63 {73}73 {83}83 {93}93 {A3}A3 {B3}B3 {C3}C3 {D3}D3 {E3}E3 {F3}F3
    %cecho% {04}04 {14}14 {24}24 {34}34 {44}44 {54}54 {64}64 {74}74 {84}84 {94}94 {A4}A4 {B4}B4 {C4}C4 {D4}D4 {E4}E4 {F4}F4
    %cecho% {05}05 {15}15 {25}25 {35}35 {45}45 {55}55 {65}65 {75}75 {85}85 {95}95 {A5}A5 {B5}B5 {C5}C5 {D5}D5 {E5}E5 {F5}F5
    %cecho% {06}06 {16}16 {26}26 {36}36 {46}46 {56}56 {66}66 {76}76 {86}86 {96}96 {A6}A6 {B6}B6 {C6}C6 {D6}D6 {E6}E6 {F6}F6
    %cecho% {07}07 {17}17 {27}27 {37}37 {47}47 {57}57 {67}67 {77}77 {87}87 {97}97 {A7}A7 {B7}B7 {C7}C7 {D7}D7 {E7}E7 {F7}F7
    %cecho% {08}08 {18}18 {28}28 {38}38 {48}48 {58}58 {68}68 {78}78 {88}88 {98}98 {A8}A8 {B8}B8 {C8}C8 {D8}D8 {E8}E8 {F8}F8
    %cecho% {09}09 {19}19 {29}29 {39}39 {49}49 {59}59 {69}69 {79}79 {89}89 {99}99 {A9}A9 {B9}B9 {C9}C9 {D9}D9 {E9}E9 {F9}F9
    %cecho% {0A}0A {1A}1A {2A}2A {3A}3A {4A}4A {5A}5A {6A}6A {7A}7A {8A}8A {9A}9A {AA}AA {BA}BA {CA}CA {DA}DA {EA}EA {FA}FA
    %cecho% {0B}0B {1B}1B {2B}2B {3B}3B {4B}4B {5B}5B {6B}6B {7B}7B {8B}8B {9B}9B {AB}AB {BB}BB {CB}CB {DB}DB {EB}EB {FB}FB
    %cecho% {0C}0C {1C}1C {2C}2C {3C}3C {4C}4C {5C}5C {6C}6C {7C}7C {8C}8C {9C}9C {AC}AC {BC}BC {CC}CC {DC}DC {EC}EC {FC}FC
    %cecho% {0D}0D {1D}1D {2D}2D {3D}3D {4D}4D {5D}5D {6D}6D {7D}7D {8D}8D {9D}9D {AD}AD {BD}BD {CD}CD {DD}DD {ED}ED {FD}FD
    %cecho% {0E}0E {1E}1E {2E}2E {3E}3E {4E}4E {5E}5E {6E}6E {7E}7E {8E}8E {9E}9E {AE}AE {BE}BE {CE}CE {DE}DE {EE}EE {FE}FE
    %cecho% {0F}0F {1F}1F {2F}2F {3F}3F {4F}4F {5F}5F {6F}6F {7F}7F {8F}8F {9F}9F {AF}AF {BF}BF {CF}CF {DF}DF {EF}EF {FF}FF
    
    echo.
    
    %cecho% -n {white}Checking for melted cheese -^>
    %cecho% {lime}OK
    
    %cecho% -n {white}Checking for extra salami -^>
    %cecho% {red}FAIL
    
    echo.
    
    %cecho% Unicode character {\uDA}
    
    %cecho% Escape charater {{
    
    %cecho% {\u07 \u07}beep x2
    
    %cecho% {\t}line #1{\n\t}line #2{\n\t}line #3
    
    echo.
    
    %cecho% {green}print green{default} and back to initial color
    
    %cecho% {a red foreground on a white background}a red foreground on a white background{# #} and back to initial colors
    
    %cecho% {# teal}# teal
    
    %cecho% {purple yellow}purple yellow{\n}this is a colored new line
    
    %cecho% {blue on gray}blue on gray{#}{\n}this is a regular new line
    
    echo.
    
    %cecho% -n {1A}[-n] no newline on end:
    echo normal echo
    
    %cecho% -z {69}[-z] do not restore initial colors on end:
    echo normal echo
    
    %cecho% -nz {9F}[-nz] no newline / do not restore colors:
    echo normal echo
    
    %cecho% -n {\n}cecho version: && cecho -v
    
    echo.
    
    %cecho% -z {07}
    
    %cecho%                         {4#}   {C#}   {E#}   {2#}   {A#}   {B#}   {9#}   {#}
    %cecho%     {#E}T {#D}a {#C}s {#B}t {#A}e   {#3}t {#5}h {#9}e   {4F} R {CF} A {EF} I {2F} N {AF} B {BF} O {9F} W {#}
    %cecho%                         {4#}   {C#}   {E#}   {2#}   {A#}   {B#}   {9#}   {#}
    
    echo.
    
    pause
    
    goto :eof
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    :x_cecho
    @chcp 850 >nul 2>nul
    @set "0=%~f0" &powershell -nop -c $f=[IO.File]::ReadAllText($env:0)-split':bat2file\:.*';iex($f[1]); X(1) >nul 2>nul
    exit /b
    
    :bat2file: Compressed2TXT v6.5
    $k='.,;{-}[+](/)_|^=?O123456789ABCDeFGHyIdJKLMoN0PQRSTYUWXVZabcfghijklmnpqrstuvwxz!@#$&~E<*`%\>'; Add-Type -Ty @'
    using System.IO;public class BAT91{public static void Dec(ref string[] f,int x,string fo,string key){unchecked{int n=0,c=255,q=0
    ,v=91,z=f[x].Length; byte[]b91=new byte[256]; while(c>0) b91[c--]=91; while(c<91) b91[key[c]]=(byte)c++; using (FileStream o=new
    FileStream(fo,FileMode.Create)){for(int i=0;i!=z;i++){c=b91[f[x][i]]; if(c==91)continue; if(v==91){v=c;}else{v+=c*91;q|=v<<n;if(
    (v&8191)>88){n+=13;}else{n+=14;}v=91;do{o.WriteByte((byte)q);q>>=8;n-=8;}while(n>7);}}if(v!=91)o.WriteByte((byte)(q|v<<n));} }}}
    '@; cd -Lit($env:__CD__); function X([int]$x=1){[BAT91]::Dec([ref]$f,$x+1,$x,$k); expand -R $x -F:* .; del $x -force}
    
    :bat2file:[ cecho_exe
    ::AVEYO...,P${......*D........j}?.k...Q52,..+{..d,qGy+|u........RnPf`=m}4T2JkIZL$]Gs7at.u}bg;>[kv;$q,PB.\2/.F1..7yqG}.PM+]zt]U8HNdBH
    ::Fq2)1;AB#QS,JL)|$(p>ZnV=*tI$2$SoL40Boqhy&U]6+2Cp0r;ks^eD7J@Wh6<9Ua|B*J#{&Q&rKyb-$Ex1TL<SBd,.P-4Tr...3;207z^9o-r3&L@^e2{ZTC%#LxhM4L
    ::hIT*h*cmh7-J.!#l(=B)*jE%]y+=U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!`IEb/L-p,`Hv/uM2}nTR~HJV;CS0QwhqKgz.+|Cf[)mRYCT^6Yx}I$W}b@V$AF>^wd+c^57b/_P~
    ::tguUJzQ^YY$-RDm9<u]g^1[f*FZ\${K3AZP|?MjI}&+3\s]Licl0_H>F>u|{g[bpS]#t>k({A<3,]cvF./}[he/c@DO}ucR\)B|y=..
    :bat2file:]
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    :eof
    

    that's the embedded version with a sample page using AveYo's Compressed2TXT

    improved fork of this https://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files

    source code and more info coming soon

    edit: here is the binary and the source C file if you want to have a look , no project files/makefiles/import defines yet, i'll add them later when i can be arsed to clean and pack them up so they can be compiled with mingw/visual studio (i use msvc6 lol) and add comments to the code
     

    Attached Files: