module Terminal.Game.Character where

import Data.Char        as C
import Text.Unidecode   as D
import System.IO.Unsafe as U


import Terminal.Game.Utils

-- Non ASCII character still cause crashes on Win32 console (see this
-- report: https://gitlab.haskell.org/ghc/ghc/issues/7593 ).
-- We provide a function to substitute them when playing on Win32
-- console, with another appropriate chatacter.

win32SafeChar :: Char -> Char
win32SafeChar :: Char -> Char
win32SafeChar Char
c | Bool
areWeWin32 = Char -> Char
toASCII Char
c
                | Bool
otherwise  = Char
c
    where
          areWeWin32 :: Bool
          areWeWin32 :: Bool
areWeWin32 = forall a. IO a -> a
unsafePerformIO IO Bool
isWin32Console

-- ANCILLARIES --

toASCII :: Char -> Char
toASCII :: Char -> Char
toASCII Char
c | Char -> Bool
C.isAscii Char
c         = Char
c
          | Just Char
cm <- Maybe Char
lu       = Char
cm    -- hand-made substitution
          | [Char
cu] <- Char -> [Char]
unidecode Char
c = Char
cu    -- unidecode
          | Bool
otherwise           = Char
'?'   -- all else failing
    where
          lu :: Maybe Char
lu = forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Char
c [(Char, Char)]
subDictionary

subDictionary :: [(Char, Char)]
subDictionary :: [(Char, Char)]
subDictionary = [ -- various open/close quotes
                  (Char
'«', Char
'<'),
                  (Char
'»', Char
'>'),
                  (Char
'“', Char
'\''),
                  (Char
'”', Char
'\''),
                  (Char
'‘', Char
'\''),
                  (Char
'’', Char
'\''),

                  -- typographical marks
                  (Char
'—', Char
'-') ]