Skip to content

Commit

Permalink
Set the console to use UTF-8 on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kit-ty-kate committed May 23, 2024
1 parent f8c8d3d commit e490a0e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ users)

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

## Plugins

Expand Down Expand Up @@ -115,6 +116,7 @@ users)
## Internal

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

## Test

Expand Down
4 changes: 3 additions & 1 deletion src/client/opamCliMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,11 @@ let json_out () =
(Printexc.to_string e)

let main () =
if Sys.win32 then
if Sys.win32 then begin
(* Disable the critical error handling dialog *)
ignore (OpamStubs.setErrorMode (1 lor OpamStubs.getErrorMode ()));
OpamStubs.setConsoleToUTF8 ();
end;
OpamStd.Sys.at_exit (fun () ->
flush_all_noerror ();
if OpamClientConfig.(!r.print_stats) then (
Expand Down
1 change: 1 addition & 0 deletions src/core/opamStubs.dummy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ let win_create_process _ _ _ _ _ = that's_a_no_no
let getConsoleWindowClass = that's_a_no_no
let setErrorMode = that's_a_no_no
let getErrorMode = that's_a_no_no
let setConsoleToUTF8 = that's_a_no_no
3 changes: 3 additions & 0 deletions src/core/opamStubs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ val setErrorMode : int -> int

val getErrorMode : unit -> int
(** Windows only. Directly wraps GetErrorMode. *)

val setConsoleToUTF8 : unit -> unit
(** Windows only. Directly wraps SetConsoleOutputCP(CP_UTF8). *)
1 change: 1 addition & 0 deletions src/stubs/win32/opamWin32Stubs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ external getConsoleAlias : string -> string -> string = "OPAMW_GetConsoleAlias"
external getConsoleWindowClass : unit -> string option = "OPAMW_GetConsoleWindowClass"
external setErrorMode : int -> int = "OPAMW_SetErrorMode"
external getErrorMode : unit -> int = "OPAMW_GetErrorMode"
external setConsoleToUTF8 : unit -> unit = "OPAMW_SetConsoleToUTF8"
9 changes: 9 additions & 0 deletions src/stubs/win32/opamWindows.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <TlHelp32.h>
#include <Knownfolders.h>
#include <Objbase.h>
#include <WinCon.h>

#include <stdio.h>

Expand Down Expand Up @@ -795,3 +796,11 @@ CAMLprim value OPAMW_GetErrorMode(value mode)
{
return Val_int(GetErrorMode());
}

CAMLprim value OPAMW_SetConsoleToUTF8(value _unit) {
/* NOTE: Setting Input (SetConsoleCP) is necessary for more.com
* called by cmdliner to correctly output UTF-8 characters */
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
return Val_unit;
}

0 comments on commit e490a0e

Please sign in to comment.