on master help sign in
Allow colored output on MinTTY consoles on Windows
commitfb221e8aec7fab8b818191fb1a76a0c0c8b034da authorRyan Scott <ryan.gl.scott@gmail.com> dateMon 30 Jan 2017, 22:52 (10 years ago) parentd08fc3b
4 files changed+30−6
idris.cabal+2−1 src/Idris/Main.hs+3−2 src/Idris/Output.hs+5−3 src/Util/System.hs+20
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/Idris/Main.hs+3−2
@@ -105,8 +105,9 @@ idrisMain opts =
105105
106106 when (DefaultTotal `elem` opts) $ do i <- getIState
107107 putIState (i { default_total = DefaultCheckingTotal })
108− tty <- runIO isATTY
109− setColourise $ not quiet && last (tty : opt getColour opts)
108+ tty <- runIO isATTY
109+ mintty <- runIO isMinTTY
110+ setColourise $ not quiet && last ((tty || mintty) : opt getColour opts)
110111
111112
112113
src/Idris/Output.hs+5−3
@@ -20,7 +20,7 @@ import Idris.IdeMode
2020
2121import Util.Pretty
2222import Util.ScreenSize (getScreenWidth)
23−import Util.System (isATTY)
23+import Util.System (isATTY, isMinTTY)
2424
2525import Prelude hiding ((<$>))
2626
@@ -65,14 +65,16 @@ iRender d = do w <- getWidth
6565 let ideMode = case idris_outputmode ist of
6666 IdeMode _ _ -> True
6767 _ -> False
68− tty <- runIO isATTY
68+ tty <- runIO isATTY
69+ mintty <- runIO isMinTTY
6970 case w of
7071 InfinitelyWide -> return $ renderPretty 1.0 1000000000 d
7172 ColsWide n -> return $
7273 if n < 1
7374 then renderPretty 1.0 1000000000 d
7475 else renderPretty 0.8 n d
75− AutomaticWidth | ideMode || not tty -> return $ renderPretty 1.0 80 d
76+ AutomaticWidth | ideMode || not (tty || mintty) ->
77+ return $ renderPretty 1.0 80 d
7678 | otherwise -> do width <- runIO getScreenWidth
7779 return $ renderPretty 0.8 width d
7880
src/Util/System.hs+20
@@ -16,6 +16,7 @@ module Util.System( tempfile
1616 , readSource
1717 , setupBundledCC
1818 , isATTY
19+ , isMinTTY
1920 ) where
2021
2122import Control.Exception as CE
@@ -38,6 +39,11 @@ import System.FilePath (dropFileName, isAbsolute, searchPathSeparator)
3839import Tools_idris
3940#endif
4041
42+#ifdef mingw32_HOST_OS
43+import Graphics.Win32.Misc (getStdHandle, sTD_OUTPUT_HANDLE)
44+import System.Console.MinTTY (isMinTTYHandle)
45+#endif
46+
4147catchIO :: IO a -> (IOError -> IO a) -> IO a
4248catchIO = CE.catch
4349
@@ -70,6 +76,20 @@ isATTY = do
7076 tty <- isATTYRaw 1 -- fd stdout
7177 return $ tty /= 0
7278
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
92+
7393withTempdir :: String -> (FilePath -> IO a) -> IO a
7494withTempdir subdir callback
7595 = do dir <- getTemporaryDirectory