Reimplement cecho.cmd to not depend on starting powershell.exe for speed boost

This commit is contained in:
Thomas Krijnen
2025-10-08 14:18:52 +02:00
parent 14c1b70768
commit e216d345c1
2 changed files with 43 additions and 5 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ if NOT "%CD%" == "%batpath%" (
set PROJECT_NAME=IfcOpenShell
call utils\cecho.cmd 15 0 "This script fetches and builds all %PROJECT_NAME% dependencies"
call utils\cecho.cmd 0 15 "This script fetches and builds all %PROJECT_NAME% dependencies"
echo.
:: Enable the delayed environment variable expansion needed in vs-cfg.cmd.
+42 -4
View File
@@ -1,4 +1,42 @@
:: Usage: call cecho.cmd background foreground "message here"
:: NOTES/TODOS 1) This is super slow 2) leading spaces are omitted 3) printing string with quotes doesn't work (quotes must be escaped awkwardly)
@powershell -command write-host -background %~1 -foreground "%~2" "%3"
@exit /b
@echo off
setlocal ENABLEDELAYEDEXPANSION
rem ==========================================================
rem cecho.cmd - Fast ANSI color echo for Windows 10+
rem Emulates PowerShell escape codes like `t and `n
rem Usage: call cecho.cmd <bg> <fg> "Message"
rem ==========================================================
for /F %%A in ('echo prompt $E ^| cmd') do set "ESC=%%A"
set "BG=%~1"
set "FG=%~2"
set "TEXT=%~3"
rem Replace PowerShell-style escapes with real characters
set "TEXT=%TEXT:`t= %"
set "TEXT=%TEXT:`"="%"
rem Foreground color table (015)
set "FGCOLOR[0]=30" & set "FGCOLOR[1]=34" & set "FGCOLOR[2]=32" & set "FGCOLOR[3]=36"
set "FGCOLOR[4]=31" & set "FGCOLOR[5]=35" & set "FGCOLOR[6]=33" & set "FGCOLOR[7]=37"
set "FGCOLOR[8]=90" & set "FGCOLOR[9]=94" & set "FGCOLOR[10]=92" & set "FGCOLOR[11]=96"
set "FGCOLOR[12]=91" & set "FGCOLOR[13]=95" & set "FGCOLOR[14]=93" & set "FGCOLOR[15]=97"
rem Background color table (07)
set "BGCOLOR[0]=40" & set "BGCOLOR[1]=44" & set "BGCOLOR[2]=42" & set "BGCOLOR[3]=46"
set "BGCOLOR[4]=41" & set "BGCOLOR[5]=45" & set "BGCOLOR[6]=43" & set "BGCOLOR[7]=47"
set "FGC=!FGCOLOR[%FG%]!"
set "BGC=!BGCOLOR[%BG%]!"
if defined BGC (
set "STYLE=!ESC![!BGC!;!FGC!m"
) else (
set "STYLE=!ESC![!FGC!m"
)
echo|set /p="!STYLE!!TEXT!!ESC![0m"
echo.
endlocal
exit /b 0