Skip to content

Commit c675869

Browse files
authored
Merge pull request #821 from tleedjarv/win-color
Try to detect if Windows terminal supports colors
2 parents 788c267 + feda437 commit c675869

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

src/system/system_generic.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ let terminalStateFunctions () =
118118
startReading = (fun () -> ());
119119
stopReading = (fun () -> ()) }
120120

121+
let termVtCapable fd = Unix.isatty fd
122+
121123
let has_stdout ~info:_ = true
122124
let has_stderr ~info:_ = true
123125

src/system/system_intf.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ type terminalStateFunctions =
115115
startReading : unit -> unit; stopReading : unit -> unit }
116116
val terminalStateFunctions : unit -> terminalStateFunctions
117117

118+
val termVtCapable : Unix.file_descr -> bool
119+
118120
val has_stdout : info:string -> bool
119121
val has_stderr : info:string -> bool
120122

src/system/system_win.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,7 @@ let terminalStateFunctions () =
199199
startReading = (fun () -> setConsoleMode 0x18);
200200
stopReading = (fun () -> setConsoleMode 0x19) }
201201

202+
external termVtCapable : Unix.file_descr -> bool = "win_vt_capable"
203+
202204
external has_stdout : info:string -> bool = "win_hasconsole_gui_stdout"
203205
external has_stderr : info:string -> bool = "win_hasconsole_gui_stderr"

src/system/system_win_stubs.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,19 @@ CAMLprim value win_set_console_output_cp (value cp) {
591591
}
592592
CAMLreturn(Val_unit);
593593
}
594+
595+
CAMLprim value win_vt_capable(value fd)
596+
{
597+
CAMLparam1(fd);
598+
DWORD mode;
599+
600+
if (Handle_val(fd) == INVALID_HANDLE_VALUE) {
601+
CAMLreturn(Val_int(0));
602+
}
603+
604+
if (!GetConsoleMode(Handle_val(fd), &mode)) {
605+
CAMLreturn(Val_int(0));
606+
}
607+
608+
CAMLreturn(Val_int(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING));
609+
}

src/uitext.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ let setColorPreference () =
104104
let envOk = try let _ = System.getenv "NO_COLOR" in false
105105
with Not_found -> true
106106
and termOk = try System.getenv "TERM" <> "dumb" with Not_found -> true
107-
and ttyOk = (Unix.isatty Unix.stdin) && (Unix.isatty Unix.stderr) in
107+
and ttyOk = (Unix.isatty Unix.stdout) && (Unix.isatty Unix.stderr) in
108108
let colorOk = envOk && termOk && ttyOk && not (Prefs.read dumbtty) in
109109
colorEnabled :=
110110
match Prefs.read colorMode with
111111
| `True -> true
112112
| `False -> false
113-
| `Default -> colorOk && Sys.os_type <> "Win32"
113+
| `Default -> colorOk && (not Sys.win32
114+
|| (System.termVtCapable Unix.stdout
115+
&& System.termVtCapable Unix.stderr))
114116

115117
let color t =
116118
if not !colorEnabled then "" else

0 commit comments

Comments
 (0)