packages feed

ansi-terminal 0.10.3 → 0.11

raw patch · 6 files changed

+62/−45 lines, 6 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- System.Console.ANSI: getCursorPosition0 :: IO (Maybe (Int, Int))

Files

CHANGELOG.md view
@@ -1,8 +1,20 @@ Changes
 =======
 
+Version 0.11
+------------
+
+* Remove deprecated `getCursorPosition0`. (Use `getCursorPosition` instead.)
+* On Unix-like operating systems, the temporary turning off of echoing is moved
+  from `getReportedCursorPosition` to `hGetCursorPositon`.
+* On Unix-like operating systems, fix a bug in `getCursorPosition` and
+  `hGetCursorPosition`, where the console input stream was was not always
+  clear before the cursor position was emitted into it.
+
+
 Version 0.10.3
 --------------
+
 * Add `getCursorPosition` as a synonym of `getCursorPosition0` and deprecate the
   latter.
 
ansi-terminal.cabal view
@@ -1,6 +1,6 @@ Name:                ansi-terminal
-Version:             0.10.3
-Cabal-Version:       >= 1.8
+Version:             0.11
+Cabal-Version:       >= 1.10
 Category:            User Interfaces
 Synopsis:            Simple ANSI terminal support, with Windows compatibility
 Description:         ANSI terminal support for Haskell: allows cursor movement,
@@ -57,10 +57,11 @@                 Cpp-Options:            -DUNIX
                 Other-Modules:          System.Console.ANSI.Unix
 
-        Extensions:             CPP
+        Default-Extensions:     CPP
                                 ForeignFunctionInterface
 
-        Ghc-Options:                    -Wall
+        Ghc-Options:            -Wall
+        Default-Language:       Haskell2010
 
 Executable ansi-terminal-example
         Hs-Source-Dirs:         app
@@ -71,3 +72,4 @@         Ghc-Options:            -Wall
         if !flag(example)
                 Buildable:              False
+        Default-Language:       Haskell2010
app/Example.hs view
@@ -260,18 +260,18 @@   -- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A A B B C C D D E E F F
   forM_ [Dull .. Vivid] $ \intensity -> do
     forM_ [Black .. White] $ \color -> do
-    let i = fromEnum intensity * 8 + fromEnum color
-        eol = i == 15
-    setSGR [SetPaletteColor Background $ xtermSystem intensity color]
-    setSGR [SetPaletteColor Foreground $ xtermSystem Dull Black]
-    printf "%X " i
-    setSGR [SetPaletteColor Foreground $ xtermSystem Vivid White]
-    printf "%X" i
-    if eol
-      then putStrLn ""
-      else do
-        setSGR [Reset]
-        putStr " "
+      let i = fromEnum intensity * 8 + fromEnum color
+          eol = i == 15
+      setSGR [SetPaletteColor Background $ xtermSystem intensity color]
+      setSGR [SetPaletteColor Foreground $ xtermSystem Dull Black]
+      printf "%X " i
+      setSGR [SetPaletteColor Foreground $ xtermSystem Vivid White]
+      printf "%X" i
+      if eol
+        then putStrLn ""
+        else do
+          setSGR [Reset]
+          putStr " "
   putStrLn ""
 
   -- Next 216 colors (6 level RGB in xterm protocol), in 12 rows of 18
src/System/Console/ANSI/Unix.hs view
@@ -11,8 +11,10 @@ 
 import Data.Maybe (fromMaybe)
 import Control.Exception.Base (bracket)
+import Control.Monad (when)
 import System.IO (BufferMode (..), Handle, hGetBuffering, hGetEcho,
-  hIsTerminalDevice, hIsWritable, hPutStr, hSetBuffering, hSetEcho, stdin)
+  hIsTerminalDevice, hIsWritable, hPutStr, hReady, hSetBuffering, hSetEcho,
+  stdin)
 import System.Timeout (timeout)
 import Text.ParserCombinators.ReadP (readP_to_S)
 
@@ -81,8 +83,7 @@ 
 -- getReportedCursorPosition :: IO String
 -- (See Common-Include.hs for Haddock documentation)
-getReportedCursorPosition = bracket (hGetEcho stdin) (hSetEcho stdin) $ \_ -> do
-  hSetEcho stdin False   -- Turn echo off
+getReportedCursorPosition = do
   -- If, unexpectedly, no data is available on the console input stream then
   -- the timeout will prevent the getChar blocking. For consistency with the
   -- Windows equivalent, returns "" if the expected information is unavailable.
@@ -110,15 +111,24 @@   to0base (row, col) = (row - 1, col - 1)
   getCursorPosition' = do
     input <- bracket (hGetBuffering stdin) (hSetBuffering stdin) $ \_ -> do
-      hSetBuffering stdin NoBuffering -- set no buffering (the contents of the
-                                      -- buffer will be discarded, so this needs
-                                      -- to be done before the cursor positon is
-                                      -- emitted)
-      hReportCursorPosition h
-      hFlush h -- ensure the report cursor position code is sent to the
-               -- operating system
-      getReportedCursorPosition
+      -- set no buffering (if 'no buffering' is not already set, the contents of
+      -- the buffer will be discarded, so this needs to be done before the
+      -- cursor positon is emitted)
+      hSetBuffering stdin NoBuffering
+      -- ensure that echoing is off
+      bracket (hGetEcho stdin) (hSetEcho stdin) $ \_ -> do
+        hSetEcho stdin False
+        clearStdin
+        hReportCursorPosition h
+        hFlush h -- ensure the report cursor position code is sent to the
+                 -- operating system
+        getReportedCursorPosition
     case readP_to_S cursorPosition input of
       [] -> return Nothing
       [((row, col),_)] -> return $ Just (row, col)
       (_:_) -> return Nothing
+  clearStdin = do
+    isReady <- hReady stdin
+    when isReady $ do
+      _ <-getChar
+      clearStdin
src/includes/Common-Include.hs view
@@ -207,14 +207,17 @@ --
 -- For example, on a Unix-like operating system:
 --
--- > hSetBuffering stdin NoBuffering -- set no buffering (the contents of the
--- >                                 -- buffer will be discarded, so this needs
--- >                                 -- to be done before the cursor positon is
--- >                                 -- emitted)
--- > reportCursorPosition
--- > hFlush stdout -- ensure the report cursor position code is sent to the
--- >               -- operating system
--- > input <- getReportedCursorPosition
+-- > -- set no buffering (if 'no buffering' is not already set, the contents of
+-- > -- the buffer will be discarded, so this needs to be done before the cursor
+-- > -- positon is emitted)
+-- > hSetBuffering stdin NoBuffering
+-- > -- ensure that echoing is off
+-- > input <- bracket (hGetEcho stdin) (hSetEcho stdin) $ \_ -> do
+-- >   hSetEcho stdin False
+-- >   reportCursorPosition
+-- >   hFlush stdout -- ensure the report cursor position code is sent to the
+-- >                 -- operating system
+-- >   getReportedCursorPosition
 --
 -- On Windows operating systems, the function is not supported on consoles, such
 -- as mintty, that are not based on the Win32 console of the Windows API.
@@ -241,13 +244,6 @@ -- @since 0.10.3
 getCursorPosition :: IO (Maybe (Int, Int))
 getCursorPosition = hGetCursorPosition stdout
-
--- | A synonym for 'getCursorPosition'.
---
--- @since 0.8.2
-{-# DEPRECATED getCursorPosition0 "Use getCursorPosition instead." #-}
-getCursorPosition0 :: IO (Maybe (Int, Int))
-getCursorPosition0 = getCursorPosition
 
 -- | Attempts to get the reported cursor position, combining the functions
 -- 'hReportCursorPosition' (with the specified handle),
src/includes/Exports-Include.hs view
@@ -119,6 +119,3 @@     -- * Getting the terminal size
   , getTerminalSize
   , hGetTerminalSize
-
-    -- * Deprecated
-  , getCursorPosition0