Skip to content

Commit e490a0e

Browse files
committed
Set the console to use UTF-8 on Windows
1 parent f8c8d3d commit e490a0e

File tree

6 files changed

+19
-1
lines changed

6 files changed

+19
-1
lines changed

master_changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ users)
1717

1818
## Global CLI
1919
* Fix a typo in the variable description returned by "opam var" [#5961 @jmid]
20+
* Out-of-the-box UTF-8 paged --help on Windows [#5970 @kit-ty-kate]
2021

2122
## Plugins
2223

@@ -115,6 +116,7 @@ users)
115116
## Internal
116117

117118
## Internal: Windows
119+
* Set the console to use UTF-8 on Windows using SetConsoleCP and SetConsoleOutputCP [#5970 @kit-ty-kate]
118120

119121
## Test
120122

src/client/opamCliMain.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,11 @@ let json_out () =
483483
(Printexc.to_string e)
484484

485485
let main () =
486-
if Sys.win32 then
486+
if Sys.win32 then begin
487487
(* Disable the critical error handling dialog *)
488488
ignore (OpamStubs.setErrorMode (1 lor OpamStubs.getErrorMode ()));
489+
OpamStubs.setConsoleToUTF8 ();
490+
end;
489491
OpamStd.Sys.at_exit (fun () ->
490492
flush_all_noerror ();
491493
if OpamClientConfig.(!r.print_stats) then (

src/core/opamStubs.dummy.ml

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ let win_create_process _ _ _ _ _ = that's_a_no_no
4141
let getConsoleWindowClass = that's_a_no_no
4242
let setErrorMode = that's_a_no_no
4343
let getErrorMode = that's_a_no_no
44+
let setConsoleToUTF8 = that's_a_no_no

src/core/opamStubs.mli

+3
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ val setErrorMode : int -> int
142142

143143
val getErrorMode : unit -> int
144144
(** Windows only. Directly wraps GetErrorMode. *)
145+
146+
val setConsoleToUTF8 : unit -> unit
147+
(** Windows only. Directly wraps SetConsoleOutputCP(CP_UTF8). *)

src/stubs/win32/opamWin32Stubs.ml

+1
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ external getConsoleAlias : string -> string -> string = "OPAMW_GetConsoleAlias"
3939
external getConsoleWindowClass : unit -> string option = "OPAMW_GetConsoleWindowClass"
4040
external setErrorMode : int -> int = "OPAMW_SetErrorMode"
4141
external getErrorMode : unit -> int = "OPAMW_GetErrorMode"
42+
external setConsoleToUTF8 : unit -> unit = "OPAMW_SetConsoleToUTF8"

src/stubs/win32/opamWindows.c

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <TlHelp32.h>
2929
#include <Knownfolders.h>
3030
#include <Objbase.h>
31+
#include <WinCon.h>
3132

3233
#include <stdio.h>
3334

@@ -795,3 +796,11 @@ CAMLprim value OPAMW_GetErrorMode(value mode)
795796
{
796797
return Val_int(GetErrorMode());
797798
}
799+
800+
CAMLprim value OPAMW_SetConsoleToUTF8(value _unit) {
801+
/* NOTE: Setting Input (SetConsoleCP) is necessary for more.com
802+
* called by cmdliner to correctly output UTF-8 characters */
803+
SetConsoleCP(CP_UTF8);
804+
SetConsoleOutputCP(CP_UTF8);
805+
return Val_unit;
806+
}

0 commit comments

Comments
 (0)