Debian Codegraph @

unix/System/Console/ANSI/Internal.hs

raw symbols SCIP
1 {-# LANGUAGE Trustworthy #-}
2
3 module System.Console.ANSI.Internal
4 ( getReportedCursorPosition
5 , getReportedLayerColor
6 , hSupportsANSI
7 , hNowSupportsANSI
8 , hSupportsANSIColor
9 ) where
10
11 import Data.List ( uncons )
12 import Data.Maybe ( fromMaybe, isJust, mapMaybe )
13 import System.Environment ( lookupEnv )
14 import System.IO ( Handle, hIsTerminalDevice, hIsWritable )
15 import System.IO.Unsafe ( unsafePerformIO )
16 import System.Timeout ( timeout )
17
18 import System.Console.ANSI.Types ( ConsoleLayer (..) )
19
20 getReportedCursorPosition :: IO String
21 getReportedCursorPosition = getReport "\ESC[" ["R"]
22
23 getReportedLayerColor :: ConsoleLayer -> IO String
24 getReportedLayerColor Underlining =
25 error "getReportedLayerColor does not support underlining."
26 getReportedLayerColor layer =
27 getReport ("\ESC]" ++ pS ++ ";rgb:") ["\BEL", "\ESC\\"]
28 where
29 pS = case layer of
30 Foreground -> "10"
31 Background -> "11"
32
33 getReport :: String -> [String] -> IO String
34 getReport _ [] = error "getReport requires a list of terminating sequences."
35 getReport startChars endChars = do
36 -- If, unexpectedly, no data is available on the console input stream then
37 -- the timeout will prevent the getChar blocking. For consistency with the
38 -- Windows equivalent, returns "" if the expected information is unavailable.
39 fromMaybe "" <$> timeout 500000 (getStart startChars "") -- 500 milliseconds
40 where
41 endChars' = mapMaybe uncons endChars
42
43 -- The list is built in reverse order, in order to avoid O(n^2) complexity.
44 -- So, getReport yields the reversed built list.
45
46 getStart :: String -> String -> IO String
47 getStart "" r = getRest r
48 getStart (h:hs) r = do
49 c <- getChar
50 if c == h
51 then getStart hs (c:r) -- Try to get the rest of the start characters
52 else pure $ reverse (c:r) -- If the first character(s) are not the
53 -- expected start then give up. This provides
54 -- a modicom of protection against unexpected
55 -- data in the input stream.
56 getRest :: String -> IO String
57 getRest r = do
58 c <- getChar
59 case lookup c endChars' of
60 Nothing -> getRest (c:r) -- Continue building the list, until the first of
61 -- the end characters is obtained.
62 Just es -> getEnd es (c:r) -- Try to get the rest of the end characters.
63
64 getEnd :: String -> String -> IO String
65 getEnd "" r = pure $ reverse r
66 getEnd (e:es) r = do
67 c <- getChar
68 if c /= e
69 then getRest (c:r) -- Continue building the list, with the original end
70 -- characters.
71 else getEnd es (c:r) -- Continue building the list, checking against the
72 -- remaining end characters.
73
74 hSupportsANSI :: Handle -> IO Bool
75 -- Borrowed from an HSpec patch by Simon Hengel
76 -- (https://github.com/hspec/hspec/commit/d932f03317e0e2bd08c85b23903fb8616ae642bd)
77 hSupportsANSI h = (&&) <$> hIsWritable h <*> hSupportsANSI'
78 where
79 hSupportsANSI' = (&& isNotDumb) <$> hIsTerminalDevice h
80
81 hNowSupportsANSI :: Handle -> IO Bool
82 hNowSupportsANSI = hSupportsANSI
83
84 hSupportsANSIColor :: Handle -> IO Bool
85 hSupportsANSIColor h = (|| isEmacsTerm) <$> hSupportsANSI h
86 where
87 isEmacsTerm = insideEmacs && isDumb
88 isDumb = not isNotDumb
89
90 -- | This function assumes that once it is first established whether or not the
91 -- TERM environment variable exists with contents dumb, that will not change.
92 -- This approach is taken because the use of C function setenv() in one thread
93 -- can cause other threads calling C function getenv() to crash. On Unix-like
94 -- operating systems, System.Environment.lookupEnv is implemented using C
95 -- function getenv().
96 isNotDumb :: Bool
97 isNotDumb = unsafePerformIO (lookupEnv "TERM") /= Just "dumb"
98
99 {-# NOINLINE isNotDumb #-}
100
101 -- | This function assumes that once it is first established whether or not the
102 -- INSIDE_EMACS environment variable exists, that will not change. This approach
103 -- is taken because the use of C function setenv() in one thread can cause other
104 -- threads calling C function getenv() to crash. On Unix-like operating systems,
105 -- System.Environment.lookupEnv is implemented using C function getenv().
106 insideEmacs :: Bool
107 insideEmacs = isJust $ unsafePerformIO (lookupEnv "INSIDE_EMACS")
108
109 {-# NOINLINE insideEmacs #-}