mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-03 16:22:52 -06:00
Factor out code for setting the console encoding
Moved similar low-level code blocks in Main and GFI for setting the console encoding to the new module GF.System.Console.
This commit is contained in:
42
src/compiler/GF/System/Console.hs
Normal file
42
src/compiler/GF/System/Console.hs
Normal file
@@ -0,0 +1,42 @@
|
||||
{-# LANGUAGE CPP #-}
|
||||
module GF.System.Console(setConsoleEncoding,changeConsoleEncoding) where
|
||||
import System.IO
|
||||
#ifdef mingw32_HOST_OS
|
||||
import System.Win32.Console
|
||||
import System.Win32.NLS
|
||||
#endif
|
||||
|
||||
setConsoleEncoding =
|
||||
#ifdef mingw32_HOST_OS
|
||||
do codepage <- getACP
|
||||
setCP codepage
|
||||
setEncoding ("CP"++show codepage)
|
||||
#endif
|
||||
return () :: IO ()
|
||||
|
||||
changeConsoleEncoding code =
|
||||
do
|
||||
#ifdef mingw32_HOST_OS
|
||||
maybe (return ()) setCP (readCP code)
|
||||
#endif
|
||||
setEncoding code
|
||||
|
||||
setEncoding code =
|
||||
do enc <- mkTextEncoding code
|
||||
hSetEncoding stdin enc
|
||||
hSetEncoding stdout enc
|
||||
hSetEncoding stderr enc
|
||||
|
||||
#ifdef mingw32_HOST_OS
|
||||
setCP codepage =
|
||||
do setConsoleCP codepage
|
||||
setConsoleOutputCP codepage
|
||||
|
||||
readCP code =
|
||||
case code of
|
||||
'C':'P':c -> case reads c of
|
||||
[(cp,"")] -> Just cp
|
||||
_ -> Nothing
|
||||
"UTF-8" -> Just 65001
|
||||
_ -> Nothing
|
||||
#endif
|
||||
Reference in New Issue
Block a user