Skip to content

Commit 99d6f4c

Browse files
committed
Enable VT100 Sequences on Windows
By default, Windows doesn't interpret VT100 sequences. Previously: ``` ☐[31m☐[1merror: ☐[0munknown option --bin-path; use --help to list available options ``` Now (pretending error is in red): ``` error: unknown option --bin-path; use --help to list available options ```
1 parent ed61206 commit 99d6f4c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/TSCBasic/TerminalController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010

1111
import TSCLibc
12+
#if os(Windows)
13+
import MSVCRT
14+
#endif
1215

1316
/// A class to have better control on tty output streams: standard output and standard error.
1417
/// Allows operations like cursor movement and colored text output on tty.
@@ -84,6 +87,18 @@ public final class TerminalController {
8487
} else {
8588
width = 80
8689
}
90+
91+
#if os(Windows)
92+
// Enable VT100 interpretation
93+
let hOut = GetStdHandle(STD_OUTPUT_HANDLE)
94+
var dwMode: DWORD = 0
95+
96+
guard hOut != INVALID_HANDLE_VALUE else { return nil }
97+
guard GetConsoleMode(hOut, &dwMode) else { return nil }
98+
99+
dwMode |= DWORD(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
100+
guard SetConsoleMode(hOut, dwMode) else { return nil }
101+
#endif
87102
self.stream = stream
88103
}
89104

0 commit comments

Comments
 (0)