Skip to content

Commit

Permalink
Support ANSI escape sequence output on more Windows consoles (crystal…
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored and rdp committed Jan 19, 2022
1 parent 99ea035 commit dc7a799
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ jobs:
echo "CRYSTAL_LIBRARY_PATH=$(pwd)\libs" >> ${env:GITHUB_ENV}
echo 'CRYSTAL_CONFIG_PATH=$ORIGIN\src' >> ${env:GITHUB_ENV}
echo 'CRYSTAL_CONFIG_LIBRARY_PATH=$ORIGIN\lib' >> ${env:GITHUB_ENV}
echo "TERM=dumb" >> ${env:GITHUB_ENV}
echo "LLVM_CONFIG=$(pwd)\llvm\bin\llvm-config.exe" >> ${env:GITHUB_ENV}
echo "CRYSTAL_CONFIG_BUILD_COMMIT=$(git rev-parse --short=9 HEAD)" >> ${env:GITHUB_ENV}
echo "SOURCE_DATE_EPOCH=$(Get-Date -Millisecond 0 -UFormat %s)" >> ${env:GITHUB_ENV}
Expand Down
8 changes: 7 additions & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,14 @@ module Crystal::System::FileDescriptor
console_handle = false
handle = LibC._get_osfhandle(fd)
if handle != -1
if LibC.GetConsoleMode(LibC::HANDLE.new(handle), out _) != 0
handle = LibC::HANDLE.new(handle)
if LibC.GetConsoleMode(handle, out old_mode) != 0
console_handle = true
if fd == 1 || fd == 2 # STDOUT or STDERR
if LibC.SetConsoleMode(handle, old_mode | LibC::ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0
at_exit { LibC.SetConsoleMode(handle, old_mode) }
end
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/consoleapi.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "c/winnt"

lib LibC
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004

fun GetConsoleMode(hConsoleHandle : HANDLE, lpMode : DWORD*) : BOOL
fun SetConsoleMode(hConsoleHandle : HANDLE, dwMode : DWORD) : BOOL

fun GetConsoleCP : DWORD
fun GetConsoleOutputCP : DWORD
Expand Down

0 comments on commit dc7a799

Please sign in to comment.