on master help sign in
Merge pull request #3643 from RyanGlScott/colors-in-mintty

Allow colored output on MinTTY consoles on Windows

commitb6e1a8cad92269e804f855c9da4b436e494ca2a9 authorNiklas Larsson <metaniklas@gmail.com> dateWed 1 Feb 2017, 18:29 (10 years ago) parentd08fc3b29ec929
2 files changed+24−3
idris.cabal+2−1 src/Util/System.hs+22−2
idris.cabal+2−1
@@ -328,7 +328,8 @@ Library
328328 if os(darwin)
329329 build-depends: unix < 2.8
330330 if os(windows)
331− build-depends: Win32 < 2.4
331+ build-depends: mintty >= 0.1 && < 0.2
332+ , Win32 < 2.4
332333 if flag(FFI)
333334 build-depends: libffi < 0.2
334335 cpp-options: -DIDRIS_FFI
src/Util/System.hs+22−2
@@ -38,6 +38,11 @@ import System.FilePath (dropFileName, isAbsolute, searchPathSeparator)
3838import Tools_idris
3939#endif
4040
41+#ifdef mingw32_HOST_OS
42+import Graphics.Win32.Misc (getStdHandle, sTD_OUTPUT_HANDLE)
43+import System.Console.MinTTY (isMinTTYHandle)
44+#endif
45+
4146catchIO :: IO a -> (IOError -> IO a) -> IO a
4247catchIO = CE.catch
4348
@@ -67,8 +72,23 @@ foreign import ccall "isatty" isATTYRaw :: CInt -> IO CInt
6772
6873isATTY :: IO Bool
6974isATTY = do
70− tty <- isATTYRaw 1 -- fd stdout
71− return $ tty /= 0
75+ tty <- isATTYRaw 1 -- fd stdout
76+ mintty <- isMinTTY
77+ return $ (tty /= 0) || mintty
78+
79+-- | Return 'True' if the process's standard output is attached to a MinTTY
80+-- console (e.g., Cygwin or MSYS) on Windows. Return 'False' otherwise.
81+--
82+-- Unfortunately, we must check this separately since 'isATTY' always returns
83+-- 'False' on MinTTY consoles.
84+isMinTTY :: IO Bool
85+#ifdef mingw32_HOST_OS
86+isMinTTY = do
87+ h <- getStdHandle sTD_OUTPUT_HANDLE
88+ isMinTTYHandle h
89+#else
90+isMinTTY = return False
91+#endif
7292
7393withTempdir :: String -> (FilePath -> IO a) -> IO a
7494withTempdir subdir callback