ansi-terminal 0.8.0.3 → 0.8.0.4
raw patch · 6 files changed
+46/−14 lines, 6 filesdep +minttyPVP ok
version bump matches the API change (PVP)
Dependencies added: mintty
API changes (from Hackage documentation)
Files
- CHANGELOG.md +6/−0
- ansi-terminal.cabal +2/−1
- src/System/Console/ANSI/Unix.hs +11/−0
- src/System/Console/ANSI/Windows.hs +5/−1
- src/System/Console/ANSI/Windows/Emulator.hs +13/−0
- src/includes/Common-Include.hs +9/−12
CHANGELOG.md view
@@ -1,6 +1,12 @@ Changes ======= +Version 0.8.0.4 +--------------- + +* On Windows, `hSupportsANSI` now recognises if the handle is connected to a + 'mintty' terminal. + Version 0.8.0.3 ---------------
ansi-terminal.cabal view
@@ -1,5 +1,5 @@ Name: ansi-terminal -Version: 0.8.0.3 +Version: 0.8.0.4 Cabal-Version: >= 1.8 Category: User Interfaces Synopsis: Simple ANSI terminal support, with Windows compatibility @@ -40,6 +40,7 @@ , colour if os(windows) Build-Depends: containers >= 0.5.0.0 + , mintty , Win32 >= 2.0 Cpp-Options: -DWINDOWS Other-Modules: System.Console.ANSI.Windows
src/System/Console/ANSI/Unix.hs view
@@ -9,6 +9,7 @@ ) where import Control.Exception.Base (bracket) +import System.Environment (getEnvironment) import System.IO (BufferMode (..), Handle, hFlush, hGetBuffering, hGetEcho, hIsTerminalDevice, hPutStr, hSetBuffering, hSetEcho, stdin, stdout) import Text.ParserCombinators.ReadP (readP_to_S) @@ -60,6 +61,16 @@ hShowCursor h = hPutStr h showCursorCode hSetTitle h title = hPutStr h $ setTitleCode title + +-- hSupportsANSI :: Handle -> IO Bool +-- (See Common-Include.hs for Haddock documentation) +-- +-- Borrowed from an HSpec patch by Simon Hengel +-- (https://github.com/hspec/hspec/commit/d932f03317e0e2bd08c85b23903fb8616ae642bd) +hSupportsANSI h = (&&) <$> hIsTerminalDevice h <*> isNotDumb + where + -- cannot use lookupEnv since it only appeared in GHC 7.6 + isNotDumb = (/= Just "dumb") . lookup "TERM" <$> getEnvironment -- getReportedCursorPosition :: IO String -- (See Common-Include.hs for Haddock documentation)
src/System/Console/ANSI/Windows.hs view
@@ -8,7 +8,7 @@ #include "Exports-Include.hs" ) where -import System.IO (Handle, hIsTerminalDevice, stdout) +import System.IO (Handle, stdout) import System.Console.ANSI.Types import qualified System.Console.ANSI.Unix as U @@ -177,6 +177,10 @@ setTitleCode :: String -> String setTitleCode = nativeOrEmulated U.setTitleCode E.setTitleCode + +-- hSupportsANSI :: Handle -> IO Bool +-- (See Common-Include.hs for Haddock documentation) +hSupportsANSI = E.hSupportsANSI -- getReportedCursorPosition :: IO String -- (See Common-Include.hs for Haddock documentation)
src/System/Console/ANSI/Windows/Emulator.hs view
@@ -13,6 +13,7 @@ import Data.List (foldl', minimumBy) import Data.Maybe (mapMaybe) import qualified Data.Map.Strict as Map (Map, empty, insert, lookup) +import System.Environment (getEnvironment) import System.IO (Handle, hFlush, hIsTerminalDevice, stdin, stdout) import System.IO.Unsafe (unsafePerformIO) import Text.ParserCombinators.ReadP (readP_to_S) @@ -21,6 +22,7 @@ import Data.Colour.Names (black, blue, cyan, green, grey, lime, magenta, maroon, navy, olive, purple, red, silver, teal, white, yellow) import Data.Colour.SRGB (RGB (..), toSRGB) +import System.Console.MinTTY (isMinTTYHandle) import System.Console.ANSI.Types import qualified System.Console.ANSI.Unix as Unix @@ -412,6 +414,17 @@ dg = g' - g db = b' - b in dr * dr + dg * dg + db * db + +-- hSupportsANSI :: Handle -> IO Bool +-- (See Common-Include.hs for Haddock documentation) +hSupportsANSI h = (||) <$> isTDNotDumb <*> isMinTTY + where + isMinTTY = withHandleToHANDLE h isMinTTYHandle +-- Borrowed from an HSpec patch by Simon Hengel +-- (https://github.com/hspec/hspec/commit/d932f03317e0e2bd08c85b23903fb8616ae642bd) + isTDNotDumb = (&&) <$> hIsTerminalDevice h <*> isNotDumb + -- cannot use lookupEnv since it only appeared in GHC 7.6 + isNotDumb = (/= Just "dumb") . lookup "TERM" <$> getEnvironment -- getReportedCursorPosition :: IO String -- (See Common-Include.hs for Haddock documentation)
src/includes/Common-Include.hs view
@@ -4,7 +4,6 @@ -- of the corresponding more general functions, inclduding the related Haddock -- documentation. -import System.Environment #if !MIN_VERSION_base(4,8,0) import Control.Applicative #endif @@ -95,21 +94,19 @@ -- | Use heuristics to determine whether the functions defined in this -- package will work with a given handle. -- --- For Unix-like operating systems or Windows, the current implementation checks --- that: (1) the handle is a terminal (see further below); and (2) a @TERM@ +-- For Unix-like operating systems, the current implementation checks +-- that: (1) the handle is a terminal; and (2) a @TERM@ -- environment variable is not set to @dumb@ (which is what the GNU Emacs text -- editor sets for its integrated terminal). -- --- The function 'hIsTerminalDevice' is used to check if the handle is a --- terminal. However, on Windows, this function may not identify a handle to a --- non-native terminal (for example, 'mintty') as a terminal. +-- For Windows, the current implementation performs the same checks as for +-- Unix-like operating systems and, as an alternative, checks whether the +-- handle is connected to a \'mintty\' terminal. (That is because the function +-- 'hIsTerminalDevice' is used to check if the handle is a +-- terminal. However, where a non-native Windows terminal (such as \'mintty\') +-- is implemented using redirection, that function will not identify a +-- handle to the terminal as a terminal.) hSupportsANSI :: Handle -> IO Bool --- Borrowed from an HSpec patch by Simon Hengel --- (https://github.com/hspec/hspec/commit/d932f03317e0e2bd08c85b23903fb8616ae642bd) -hSupportsANSI h = (&&) <$> hIsTerminalDevice h <*> (not <$> isDumb) - where - -- cannot use lookupEnv since it only appeared in GHC 7.6 - isDumb = maybe False (== "dumb") . lookup "TERM" <$> getEnvironment -- | Parses the characters emitted by 'reportCursorPosition' into the console -- input stream. Returns the cursor row and column as a tuple.