Skip to content

Commit

Permalink
Fix text locale encoding issues on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Oct 28, 2019
1 parent 61a87a4 commit e504c72
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 32 deletions.
30 changes: 5 additions & 25 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ module Cardano.CLI
, verbosityToArgs
, verbosityToMinSeverity

-- * Unicode Terminal Helpers
, setUtf8Encoding

-- * ANSI Terminal Helpers
, putErrLn
, hPutErrLn
Expand Down Expand Up @@ -173,8 +170,6 @@ import Fmt
( Buildable, pretty )
import GHC.Generics
( Generic )
import GHC.IO.Encoding
( setLocaleEncoding )
import GHC.TypeLits
( Symbol )
import Network.HTTP.Client
Expand Down Expand Up @@ -244,11 +239,8 @@ import System.IO
, hPutChar
, hSetBuffering
, hSetEcho
, hSetEncoding
, stderr
, stdin
, stdout
, utf8
)

import qualified Cardano.BM.Configuration.Model as CM
Expand Down Expand Up @@ -375,9 +367,9 @@ cmdWalletCreate = command "create" $ info (helper <*> cmd) $ mempty
getLine prompt parser
wSndFactor <- do
let prompt =
"(Enter a blank line if you do not wish to use a second \
\factor.)\n\
\Please enter a 9–12 word mnemonic second factor: "
"(Enter a blank line if you do not wish to use a second " <>
"factor.)\n" <>
"Please enter a 9–12 word mnemonic second factor: "
let parser =
optionalE (fromMnemonic @'[9,12] @"generation") . T.words
getLine prompt parser <&> \case
Expand Down Expand Up @@ -837,8 +829,8 @@ paymentOption = optionT $ mempty
<> long "payment"
<> metavar "PAYMENT"
<> help
"address to send to and amount to send separated by @\
\, e.g. '<amount>@<address>'"
("address to send to and amount to send separated by @" <>
", e.g. '<amount>@<address>'")

-- | [--address-pool-gap=INT], default: 20
poolGapOption :: Parser AddressPoolGap
Expand Down Expand Up @@ -1226,18 +1218,6 @@ withLogging configFile minSeverity action = bracket before after (action . snd)
logDebug tr "Logging shutdown."
shutdown sb

{-------------------------------------------------------------------------------
Unicode Terminal Helpers
-------------------------------------------------------------------------------}

-- | Override the system output encoding setting. This is needed because the CLI
-- prints UTF-8 characters regardless of the @LANG@ environment variable.
setUtf8Encoding :: IO ()
setUtf8Encoding = do
setLocaleEncoding utf8
hSetEncoding stdout utf8
hSetEncoding stderr utf8

{-------------------------------------------------------------------------------
ANSI Terminal Helpers
-------------------------------------------------------------------------------}
Expand Down
2 changes: 2 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ test-suite unit
, bytestring
, cardano-crypto
, cardano-wallet-core
, cardano-wallet-launcher
, cardano-wallet-test-utils
, cborg
, containers
Expand Down Expand Up @@ -238,6 +239,7 @@ test-suite unit
Data.Time.TextSpec
Data.Time.UtilsSpec
Network.Wai.Middleware.LoggingSpec
Spec

benchmark db
default-language:
Expand Down
13 changes: 12 additions & 1 deletion lib/core/test/unit/Main.hs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
module Main where

import Cardano.Launcher
( setUtf8Encoding )
import Prelude
import qualified Spec
import Test.Hspec.Runner

main :: IO ()
main = do
setUtf8Encoding
hspecWith defaultConfig Spec.spec
1 change: 1 addition & 0 deletions lib/core/test/unit/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}
3 changes: 1 addition & 2 deletions lib/jormungandr/exe/cardano-wallet-jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ import Cardano.CLI
, optionT
, requireFilePath
, runCli
, setUtf8Encoding
, setupDirectory
, stateDirOption
, verbosityOption
, verbosityToMinSeverity
, withLogging
)
import Cardano.Launcher
( StdStream (..) )
( StdStream (..), setUtf8Encoding )
import Cardano.Wallet.Api.Server
( HostPreference, Listen (..) )
import Cardano.Wallet.Jormungandr
Expand Down
1 change: 1 addition & 0 deletions lib/launcher/cardano-wallet-launcher.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
if os(windows)
build-depends: Win32
other-modules: Cardano.Launcher.Windows
cpp-options: -DWINDOWS
else
build-depends: unix
other-modules: Cardano.Launcher.POSIX
Expand Down
40 changes: 36 additions & 4 deletions lib/launcher/src/Cardano/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
-- Copyright: © 2018-2019 IOHK
-- License: Apache-2.0
--
-- This module contains a mechanism for launching external processes together,
-- and provides the functionality needed to kill them all if one goes down.
-- (would be achieved using @monitor@ and @kill@ in combination)
-- This module contains a mechanism for launching external processes, ensuring
-- that they are terminated on exceptions.

module Cardano.Launcher
( Command (..)
, StdStream(..)
, ProcessHasExited(..)
, launch
, withBackendProcess

-- * Program startup
, installSignalHandlers
, setUtf8Encoding

-- * Logging
, LauncherLog(..)
Expand Down Expand Up @@ -65,6 +67,9 @@ import GHC.Generics
( Generic )
import System.Exit
( ExitCode (..) )
import System.IO
( hSetEncoding, mkTextEncoding, stderr, stdin, stdout )

import System.Process
( CreateProcess (..)
, StdStream (..)
Expand All @@ -74,9 +79,11 @@ import System.Process
, withCreateProcess
)

#ifdef mingw32_HOST_OS
#ifdef WINDOWS
import Cardano.Launcher.Windows
( installSignalHandlers )
import System.Win32.Console
( setConsoleCP, setConsoleOutputCP )
#else
import Cardano.Launcher.POSIX
( installSignalHandlers )
Expand Down Expand Up @@ -233,3 +240,28 @@ launcherLogText (MsgLauncherFinish (ProcessHasExited name code)) =
launcherLogText (MsgLauncherFinish (ProcessDidNotStart name _e)) =
"Could not start "+|name|+""
launcherLogText MsgLauncherCleanup = "Terminating child process"

{-------------------------------------------------------------------------------
Unicode Terminal Helpers
-------------------------------------------------------------------------------}

-- | Force the locale text encoding to UTF-8. This is needed because the CLI
-- prints UTF-8 characters regardless of the @LANG@ environment variable or any
-- other settings.
--
-- On Windows the current console code page is changed to UTF-8.
setUtf8Encoding :: IO ()
#if WINDOWS
setUtf8Encoding = do
let utf8CodePage = 65001
setConsoleCP utf8CodePage
setConsoleOutputCP utf8CodePage
setUtf8EncodingHandles
#else
setUtf8Encoding = setUtf8EncodingHandles
#endif

setUtf8EncodingHandles :: IO ()
setUtf8EncodingHandles = do
utf8' <- mkTextEncoding "UTF-8//TRANSLIT"
mapM_ (\h -> hSetEncoding h utf8') [stdin, stdout, stderr]
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-core.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e504c72

Please sign in to comment.