Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #166 On Unix-like OS, use unsafePerformIO (lookupEnv "TERM") #167

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions ansi-terminal/src/System/Console/ANSI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ import Control.Monad ( when, void )
import Data.Char ( digitToInt, isDigit, isHexDigit )
import Data.Colour.SRGB ( RGB (..) )
import Data.Word ( Word16 )
import System.Environment ( getEnvironment )
import System.IO
( BufferMode (..), Handle, hFlush, hGetBuffering, hGetEcho, hPutStr
, hReady, hSetBuffering, hSetEcho, stdin, stdout
Expand Down Expand Up @@ -638,11 +637,7 @@ hNowSupportsANSI = Internal.hNowSupportsANSI
--
-- @since 0.9
hSupportsANSIColor :: Handle -> IO Bool
hSupportsANSIColor h = (||) <$> hSupportsANSI h <*> isEmacsTerm
where
isEmacsTerm = (\env -> insideEmacs env && isDumb env) <$> getEnvironment
insideEmacs = any (\(k, _) -> k == "INSIDE_EMACS")
isDumb env = Just "dumb" == lookup "TERM" env
hSupportsANSIColor = Internal.hSupportsANSIColor

-- | Use heuristics to determine whether a given handle will support \'ANSI\'
-- control characters in output. The function is consistent with
Expand Down
36 changes: 32 additions & 4 deletions ansi-terminal/unix/System/Console/ANSI/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE Trustworthy #-}

module System.Console.ANSI.Internal
( getReportedCursorPosition
, getReportedLayerColor
, hSupportsANSI
, hNowSupportsANSI
, hSupportsANSIColor
) where

import Data.List ( uncons )
import Data.Maybe ( fromMaybe, mapMaybe )
import Data.Maybe ( fromMaybe, isJust, mapMaybe )
import System.Environment ( lookupEnv )
import System.IO ( Handle, hIsTerminalDevice, hIsWritable )
import System.IO.Unsafe ( unsafePerformIO )
import System.Timeout ( timeout )

import System.Console.ANSI.Types ( ConsoleLayer (..) )
Expand Down Expand Up @@ -72,8 +74,34 @@ hSupportsANSI :: Handle -> IO Bool
-- (https://github.com/hspec/hspec/commit/d932f03317e0e2bd08c85b23903fb8616ae642bd)
hSupportsANSI h = (&&) <$> hIsWritable h <*> hSupportsANSI'
where
hSupportsANSI' = (&&) <$> hIsTerminalDevice h <*> isNotDumb
isNotDumb = (/= Just "dumb") <$> lookupEnv "TERM"
hSupportsANSI' = (&& isNotDumb) <$> hIsTerminalDevice h

hNowSupportsANSI :: Handle -> IO Bool
hNowSupportsANSI = hSupportsANSI

hSupportsANSIColor :: Handle -> IO Bool
hSupportsANSIColor h = (|| isEmacsTerm) <$> hSupportsANSI h
where
isEmacsTerm = insideEmacs && isDumb
isDumb = not isNotDumb

-- | This function assumes that once it is first established whether or not the
-- TERM environment variable exists with contents dumb, that will not change.
-- This approach is taken because the use of C function setenv() in one thread
-- can cause other threads calling C function getenv() to crash. On Unix-like
-- operating systems, System.Environment.lookupEnv is implemented using C
-- function getenv().
isNotDumb :: Bool
isNotDumb = unsafePerformIO (lookupEnv "TERM") /= Just "dumb"

mpilgrem marked this conversation as resolved.
Show resolved Hide resolved
{-# NOINLINE isNotDumb #-}

-- | This function assumes that once it is first established whether or not the
-- INSIDE_EMACS environment variable exists, that will not change. This approach
-- is taken because the use of C function setenv() in one thread can cause other
-- threads calling C function getenv() to crash. On Unix-like operating systems,
-- System.Environment.lookupEnv is implemented using C function getenv().
insideEmacs :: Bool
insideEmacs = isJust $ unsafePerformIO (lookupEnv "INSIDE_EMACS")

{-# NOINLINE insideEmacs #-}
10 changes: 9 additions & 1 deletion ansi-terminal/win/System/Console/ANSI/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module System.Console.ANSI.Internal
, getReportedLayerColor
, hNowSupportsANSI
, hSupportsANSI
, hSupportsANSIColor
) where

import Control.Exception ( IOException, SomeException, catch, try )
import Data.Bits ( (.&.), (.|.) )
import Data.Maybe ( mapMaybe )
import System.Environment ( lookupEnv )
import System.Environment ( getEnvironment, lookupEnv )
import System.IO ( Handle, hIsTerminalDevice, hIsWritable, stdin )
import System.Console.ANSI.Types ( ConsoleLayer )

Expand Down Expand Up @@ -103,3 +104,10 @@ withHANDLE invalid action h =
if h == iNVALID_HANDLE_VALUE || h == nullHANDLE
then invalid -- Invalid handle or no handle
else action h

hSupportsANSIColor :: Handle -> IO Bool
hSupportsANSIColor h = (||) <$> hSupportsANSI h <*> isEmacsTerm
where
isEmacsTerm = (\env -> insideEmacs env && isDumb env) <$> getEnvironment
insideEmacs = any (\(k, _) -> k == "INSIDE_EMACS")
isDumb env = Just "dumb" == lookup "TERM" env
Loading