Skip to content

Commit

Permalink
Merge #957
Browse files Browse the repository at this point in the history
957: cli: ANSI colour for windows 10, and easier utf-8 setup r=rvl a=rvl

Relates to #703.

# Overview

IOHK logging framework uses ANSI control codes for colouring its output (instead of the Win32-specific method). Windows console does not interpret ANSI colour codes unless told to (even then, it's only supported under win 10).

- Enables ANSI colour at startup ([System.Console.ANSI](http://hackage.haskell.org/package/ansi-terminal-0.10.1/docs/System-Console-ANSI.html#v:hSupportsANSIWithoutEmulation)).
- There is a better way of setting UTF-8 on Windows - [code-page package](http://hackage.haskell.org/package/code-page-0.2/docs/System-IO-CodePage.html#v:withCP65001).


Co-authored-by: Rodney Lorrimar <[email protected]>
  • Loading branch information
iohk-bors[bot] and rvl authored Nov 6, 2019
2 parents dddf461 + 8d1acc7 commit e28bfe8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
14 changes: 14 additions & 0 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module Cardano.CLI
-- * ANSI Terminal Helpers
, putErrLn
, hPutErrLn
, enableWindowsANSI

-- * Working with Sensitive Data
, getLine
Expand Down Expand Up @@ -212,6 +213,7 @@ import System.Console.ANSI
, SGR (..)
, hCursorBackward
, hSetSGR
, hSupportsANSIWithoutEmulation
)
import System.Directory
( XdgDirectory (..)
Expand All @@ -238,6 +240,7 @@ import System.IO
, hSetEcho
, stderr
, stdin
, stdout
)

import qualified Cardano.BM.Configuration.Model as CM
Expand Down Expand Up @@ -1254,6 +1257,17 @@ hPutErrLn h msg = withSGR h (SetColor Foreground Vivid Red) $ do
putErrLn :: Text -> IO ()
putErrLn = hPutErrLn stderr

-- | The IOHK logging framework prints out ANSI colour codes with its messages.
-- On Windows 10 and above it's possible to enable processing of these colour
-- codes. The 'hSupportsANSIWithoutEmulation' function does this as a side
-- effect. On older versions of Windows, special treatment is required (see:
-- 'System.Console.ANSI'). In this case, this function will achieve nothing, and
-- the ANSI control characters will be printed in grey (too bad).
enableWindowsANSI :: IO ()
enableWindowsANSI = do
void $ hSupportsANSIWithoutEmulation stdout
void $ hSupportsANSIWithoutEmulation stderr

{-------------------------------------------------------------------------------
Processing of Sensitive Data
-------------------------------------------------------------------------------}
Expand Down
6 changes: 2 additions & 4 deletions lib/core/test/unit/Main.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module Main where

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

main :: IO ()
main = do
setUtf8Encoding
hspecWith defaultConfig Spec.spec
main = withUtf8Encoding $ hspecWith defaultConfig Spec.spec
7 changes: 4 additions & 3 deletions lib/jormungandr/exe/cardano-wallet-jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Cardano.CLI
, cmdVersion
, cmdWallet
, databaseOption
, enableWindowsANSI
, getDataDir
, hostPreferenceOption
, listenOption
Expand All @@ -54,7 +55,7 @@ import Cardano.CLI
, withLogging
)
import Cardano.Launcher
( StdStream (..), setUtf8Encoding )
( StdStream (..), withUtf8Encoding )
import Cardano.Wallet.Api.Server
( HostPreference, Listen (..) )
import Cardano.Wallet.Jormungandr
Expand Down Expand Up @@ -128,8 +129,8 @@ import qualified Options.Applicative.Help.Pretty as D
-------------------------------------------------------------------------------}

main :: IO ()
main = do
setUtf8Encoding
main = withUtf8Encoding $ do
enableWindowsANSI
dataDir <- getDataDir "jormungandr"
runCli $ cli $ mempty
<> cmdLaunch dataDir
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 @@ -32,6 +32,7 @@ library
base
, aeson
, async
, code-page
, contra-tracer
, fmt
, iohk-monitoring
Expand Down
19 changes: 5 additions & 14 deletions lib/launcher/src/Cardano/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Cardano.Launcher

-- * Program startup
, installSignalHandlers
, setUtf8Encoding
, withUtf8Encoding

-- * Logging
, LauncherLog(..)
Expand Down Expand Up @@ -69,7 +69,8 @@ import System.Exit
( ExitCode (..) )
import System.IO
( hSetEncoding, mkTextEncoding, stderr, stdin, stdout )

import System.IO.CodePage
( withCP65001 )
import System.Process
( CreateProcess (..)
, StdStream (..)
Expand All @@ -82,8 +83,6 @@ import System.Process
#ifdef WINDOWS
import Cardano.Launcher.Windows
( installSignalHandlers )
import System.Win32.Console
( setConsoleCP, setConsoleOutputCP )
#else
import Cardano.Launcher.POSIX
( installSignalHandlers )
Expand Down Expand Up @@ -250,16 +249,8 @@ launcherLogText MsgLauncherCleanup = "Terminating child process"
-- 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
withUtf8Encoding :: IO a -> IO a
withUtf8Encoding action = withCP65001 (setUtf8EncodingHandles >> action)

setUtf8EncodingHandles :: IO ()
setUtf8EncodingHandles = do
Expand Down
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-launcher.nix

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

0 comments on commit e28bfe8

Please sign in to comment.