| 1 |
{-# LANGUAGE Safe #-} |
| 2 |
|
| 3 |
module System.Console.ANSI.Internal |
| 4 |
( getReportedCursorPosition |
| 5 |
, getReportedLayerColor |
| 6 |
, hNowSupportsANSI |
| 7 |
, hSupportsANSI |
| 8 |
, hSupportsANSIColor |
| 9 |
) where |
| 10 |
|
| 11 |
import Control.Exception ( IOException, SomeException, catch, try ) |
| 12 |
import Data.Bits ( (.&.), (.|.) ) |
| 13 |
import Data.Maybe ( mapMaybe ) |
| 14 |
import System.Environment ( getEnvironment, lookupEnv ) |
| 15 |
import System.IO ( Handle, hIsTerminalDevice, hIsWritable, stdin ) |
| 16 |
import System.Console.ANSI.Types ( ConsoleLayer ) |
| 17 |
|
| 18 |
-- Provided by the ansi-terminal package |
| 19 |
import System.Console.ANSI.Windows.Foreign |
| 20 |
( INPUT_RECORD (..), INPUT_RECORD_EVENT (..), KEY_EVENT_RECORD (..) |
| 21 |
, cWcharsToChars, eNABLE_VIRTUAL_TERMINAL_PROCESSING |
| 22 |
, getConsoleMode, getNumberOfConsoleInputEvents, iNVALID_HANDLE_VALUE |
| 23 |
, nullHANDLE, readConsoleInput, setConsoleMode, unicodeAsciiChar |
| 24 |
) |
| 25 |
import System.Console.ANSI.Windows.Win32.MinTTY ( isMinTTYHandle ) |
| 26 |
import System.Console.ANSI.Windows.Win32.Types |
| 27 |
( DWORD, HANDLE, withHandleToHANDLE ) |
| 28 |
|
| 29 |
getReportedCursorPosition :: IO String |
| 30 |
getReportedCursorPosition = getReported |
| 31 |
|
| 32 |
getReportedLayerColor :: ConsoleLayer -> IO String |
| 33 |
getReportedLayerColor _ = getReported |
| 34 |
|
| 35 |
getReported :: IO String |
| 36 |
getReported = catch getReported' getReportedExceptionHandler |
| 37 |
where |
| 38 |
getReported' = withHandleToHANDLE stdin action |
| 39 |
where |
| 40 |
action hdl = do |
| 41 |
n <- getNumberOfConsoleInputEvents hdl |
| 42 |
if n == 0 |
| 43 |
then pure "" |
| 44 |
else do |
| 45 |
es <- readConsoleInput hdl n |
| 46 |
pure $ stringFromInputEvents es |
| 47 |
stringFromInputEvents = cWcharsToChars . wCharsFromInputEvents |
| 48 |
wCharsFromInputEvents = mapMaybe wCharFromInputEvent |
| 49 |
wCharFromInputEvent e = if isKeyEvent && isKeyDown |
| 50 |
then Just (unicodeAsciiChar $ keyEventChar keyEventRecord) |
| 51 |
else Nothing |
| 52 |
where |
| 53 |
eventType = inputEventType e |
| 54 |
eventRecord = inputEvent e |
| 55 |
isKeyEvent = eventType == 1 |
| 56 |
keyEventRecord = case eventRecord of |
| 57 |
InputKeyEvent keyEventRecord' -> keyEventRecord' |
| 58 |
_ -> error "Unexpected input event, given input event type." |
| 59 |
isKeyDown = keyEventKeyDown keyEventRecord |
| 60 |
|
| 61 |
getReportedExceptionHandler :: IOException -> IO a |
| 62 |
getReportedExceptionHandler e = error msg |
| 63 |
where |
| 64 |
msg = "Error: " ++ show e ++ "\nThis error may be avoided by using a " ++ |
| 65 |
"console based on the Windows' Console API, such as Command Prompt " ++ |
| 66 |
"or PowerShell." |
| 67 |
|
| 68 |
hSupportsANSI :: Handle -> IO Bool |
| 69 |
hSupportsANSI = hSupportsANSI' False |
| 70 |
|
| 71 |
hNowSupportsANSI :: Handle -> IO Bool |
| 72 |
hNowSupportsANSI = hSupportsANSI' True |
| 73 |
|
| 74 |
hSupportsANSI' :: Bool -> Handle -> IO Bool |
| 75 |
hSupportsANSI' tryToEnable handle = do |
| 76 |
isWritable <- hIsWritable handle |
| 77 |
if isWritable |
| 78 |
then withHandleToHANDLE handle $ withHANDLE |
| 79 |
(pure False) -- Invalid handle or no handle |
| 80 |
( \h -> do |
| 81 |
tryMode <- try (getConsoleMode h) :: IO (Either SomeException DWORD) |
| 82 |
case tryMode of |
| 83 |
Left _ -> isMinTTYHandle h -- No ConHost mode |
| 84 |
Right mode -> do |
| 85 |
let isVTEnabled = mode .&. eNABLE_VIRTUAL_TERMINAL_PROCESSING /= 0 |
| 86 |
isNotDumb = (/= Just "dumb") <$> lookupEnv "TERM" |
| 87 |
isTDNotDumb <- (&&) <$> hIsTerminalDevice handle <*> isNotDumb |
| 88 |
if isTDNotDumb && not isVTEnabled && tryToEnable |
| 89 |
then do |
| 90 |
let mode' = mode .|. eNABLE_VIRTUAL_TERMINAL_PROCESSING |
| 91 |
trySetMode <- try (setConsoleMode h mode') |
| 92 |
:: IO (Either SomeException ()) |
| 93 |
case trySetMode of |
| 94 |
Left _ -> pure False -- Can't enable VT processing |
| 95 |
Right () -> pure True -- VT processing enabled |
| 96 |
else pure $ isTDNotDumb && isVTEnabled |
| 97 |
) |
| 98 |
else pure False |
| 99 |
|
| 100 |
-- | This function applies another to the Windows handle, if the handle is |
| 101 |
-- valid. If it is invalid, the specified default action is returned. |
| 102 |
withHANDLE :: IO a -> (HANDLE -> IO a) -> HANDLE -> IO a |
| 103 |
withHANDLE invalid action h = |
| 104 |
if h == iNVALID_HANDLE_VALUE || h == nullHANDLE |
| 105 |
then invalid -- Invalid handle or no handle |
| 106 |
else action h |
| 107 |
|
| 108 |
hSupportsANSIColor :: Handle -> IO Bool |
| 109 |
hSupportsANSIColor h = (||) <$> hSupportsANSI h <*> isEmacsTerm |
| 110 |
where |
| 111 |
isEmacsTerm = (\env -> insideEmacs env && isDumb env) <$> getEnvironment |
| 112 |
insideEmacs = any (\(k, _) -> k == "INSIDE_EMACS") |
| 113 |
isDumb env = Just "dumb" == lookup "TERM" env |