packages feed

terminal-size 0.2.0.0 → 0.2.1.0

raw patch · 2 files changed

+41/−11 lines, 2 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ System.Console.Terminal.Size: fdSize :: Integral n => Fd -> IO (Maybe (Window n))
+ System.Console.Terminal.Size: hSize :: Integral n => Handle -> IO (Maybe (Window n))

Files

src/System/Console/Terminal/Size.hsc view
@@ -6,16 +6,21 @@ Based on answer by Andreas Hammar at <http://stackoverflow.com/a/12807521/972985> -} module System.Console.Terminal.Size-  ( Window(..), size+  ( Window(..), size, fdSize, hSize   ) where  import Control.Exception (catch)+import Data.Typeable (cast) import Foreign import Foreign.C.Error import Foreign.C.Types+import GHC.IO.FD (FD(FD, fdFD))+import GHC.IO.Handle.Internals (withHandle_)+import GHC.IO.Handle.Types (Handle, Handle__(Handle__, haDevice)) #if defined(__GLASGOW_HASKELL__) && (__GLASGOW_HASKELL__ < 706) import Prelude hiding (catch) #endif+import System.Posix.Types (Fd(Fd))  #include <sys/ioctl.h> #include <unistd.h>@@ -48,22 +53,47 @@ instance Functor Window where   fmap f (Window { height = h, width = w }) = Window { height = f h, width = f w } ---- | Get terminal window width and height+-- | Get terminal window width and height for a specified file descriptor. If+-- it's not attached to a terminal then 'Nothing' is returned. -- -- >>> import System.Console.Terminal.Size--- >>> size--- Just (Window {height = 60, width = 112})-size :: Integral n => IO (Maybe (Window n))-size = with (CWin 0 0) $ \ws -> do+-- >>> import System.Posix+-- >>> fdSize stdOutput+-- Just (Window {height = 56, width = 85})+-- >>> fd <- openFd "foo" ReadWrite (Just stdFileMode) defaultFileFlags+-- >>> fdSize fd+-- Nothing+fdSize :: Integral n => Fd -> IO (Maybe (Window n))+fdSize (Fd fd) = with (CWin 0 0) $ \ws -> do   throwErrnoIfMinus1 "ioctl" $-    ioctl (#const STDOUT_FILENO) (#const TIOCGWINSZ) ws+    ioctl fd (#const TIOCGWINSZ) ws   CWin row col <- peek ws   return . Just $ Window (fromIntegral row) (fromIntegral col)- `catch` handler+ `catch`+  handler  where-  handler :: (IOError -> IO (Maybe (Window h)))+  handler :: IOError -> IO (Maybe (Window h))   handler _ = return Nothing  foreign import ccall "sys/ioctl.h ioctl"   ioctl :: CInt -> CInt -> Ptr CWin -> IO CInt++-- | Get terminal window width and height for @stdout@.+--+-- >>> import System.Console.Terminal.Size+-- >>> size+-- Just (Window {height = 60, width = 112})+size :: Integral n => IO (Maybe (Window n))+size = fdSize (Fd (#const STDOUT_FILENO))++-- | Same as 'fdSize', but takes 'Handle' instead of 'Fd' (file descriptor).+--+-- >>> import System.Console.Terminal.Size+-- >>> import System.IO+-- >>> hSize stdout+-- Just (Window {height = 56, width = 85})+hSize :: Integral n => Handle -> IO (Maybe (Window n))+hSize h = withHandle_ "hSize" h $ \Handle__ { haDevice = dev } ->+  case cast dev of+    Nothing -> return Nothing+    Just FD { fdFD = fd } -> fdSize (Fd fd)
terminal-size.cabal view
@@ -1,5 +1,5 @@ name:                terminal-size-version:             0.2.0.0+version:             0.2.1.0 synopsis:            Get terminal window height and width description:   Get terminal window height and width without ncurses dependency