blob: e064b986946b237ab8f20153fae9652f8ac8ea77 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
{-# LANGUAGE CPP #-}
module GF.System.Console(
-- ** Console IO
-- *** Changing which character encoding to use for console IO
setConsoleEncoding,changeConsoleEncoding,
-- *** Console colors
TermColors(..),getTermColors
) where
import System.IO
import Control.Monad(guard)
import Control.Monad.Trans(MonadIO(..))
#ifdef mingw32_HOST_OS
import System.Win32.Console
import System.Win32.NLS
#else
import System.Console.Terminfo
#endif
-- | Set the console encoding (for Windows, has no effect on Unix-like systems)
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
data TermColors = TermColors { redFg,blueFg,restore :: String } deriving Show
noTermColors = TermColors "" "" ""
getTermColors :: MonadIO m => m TermColors
#ifdef mingw32_HOST_OS
getTermColors = return noTermColors
#else
getTermColors =
liftIO $
do term <- setupTermFromEnv
return $ maybe noTermColors id $ getCapability term $
do n <- termColors
guard (n>=8)
fg <- setForegroundColor
restore <- restoreDefaultColors
return $ TermColors (fg Red) (fg Blue) restore
#endif
|