Skip to content

Commit

Permalink
Fix #166 On Unix-like OS, use unsafePerformIO (lookupEnv "TERM")
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Apr 21, 2024
1 parent 6bcbe9f commit f90ebf1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ansi-terminal/unix/System/Console/ANSI/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE Trustworthy #-}

module System.Console.ANSI.Internal
( getReportedCursorPosition
Expand All @@ -11,6 +11,7 @@ import Data.List ( uncons )
import Data.Maybe ( fromMaybe, 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 +73,12 @@ 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' = (&&) <$> hIsTerminalDevice h <*> pure isNotDumb

hNowSupportsANSI :: Handle -> IO Bool
hNowSupportsANSI = hSupportsANSI

-- | This function assumes that once it is first established whether or not the
-- TERM environment variable exits with contents dumb, that will not change.
isNotDumb :: Bool
isNotDumb = unsafePerformIO (lookupEnv "TERM") /= Just "dumb"

0 comments on commit f90ebf1

Please sign in to comment.