terminal-size-0.3.2.1/0000755000000000000000000000000012615425305012627 5ustar0000000000000000terminal-size-0.3.2.1/LICENSE0000644000000000000000000000277312615425305013645 0ustar0000000000000000Copyright (c) 2013-2015, Matvey Aksenov All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Matvey Aksenov nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. terminal-size-0.3.2.1/Setup.hs0000644000000000000000000000005612615425305014264 0ustar0000000000000000import Distribution.Simple main = defaultMain terminal-size-0.3.2.1/terminal-size.cabal0000644000000000000000000000235312615425305016401 0ustar0000000000000000name: terminal-size version: 0.3.2.1 synopsis: Get terminal window height and width description: Get terminal window height and width without ncurses dependency. license: BSD3 license-file: LICENSE author: Andreas Hammar, Matvey Aksenov maintainer: matvey.aksenov@gmail.com category: System build-type: Simple cabal-version: >= 1.10 extra-source-files: README.markdown CHANGELOG.markdown source-repository head type: git location: https://github.com/biegunka/terminal-size source-repository this type: git location: https://github.com/biegunka/terminal-size tag: 0.3.2.1 library default-language: Haskell2010 build-depends: base >= 4 && < 5 if impl(ghc >= 7.2 && < 7.6) build-depends: ghc-prim if os(windows) build-depends: process build-tools: hsc2hs hs-source-dirs: src exposed-modules: System.Console.Terminal.Size other-modules: System.Console.Terminal.Common if os(Windows) other-modules: System.Console.Terminal.Windows else other-modules: System.Console.Terminal.Posix ghc-options: -Wall -fno-warn-unused-do-bind terminal-size-0.3.2.1/README.markdown0000644000000000000000000000062012615425305015326 0ustar0000000000000000terminal-size ============= [![Hackage](https://budueba.com/hackage/terminal-size)](https://hackage.haskell.org/package/terminal-size) [![Build Status](https://travis-ci.org/biegunka/terminal-size.png)](https://travis-ci.org/biegunka/terminal-size) Get terminal window width and height Usage ----- ``` >>> import System.Console.Terminal.Size >>> size Just (Window {height = 60, width = 112}) ``` terminal-size-0.3.2.1/CHANGELOG.markdown0000644000000000000000000000064312615425305015665 0ustar00000000000000000.3.2.1 ======= * GHC 7.2 support (https://github.com/biegunka/terminal-size/pull/9) 0.3.2 ===== * Cygwin/MSYS support (https://github.com/biegunka/terminal-size/pull/8) 0.3.1 ===== * `Typeable`, `Data`, `Generic`, and `Generic1` instances for `Window`. 0.3.0 ===== * Windows support (https://github.com/biegunka/terminal-size/pull/4) * Added `Eq`, `Foldable` and `Traversable` instances for `Window` terminal-size-0.3.2.1/src/0000755000000000000000000000000012615425305013416 5ustar0000000000000000terminal-size-0.3.2.1/src/System/0000755000000000000000000000000012615425305014702 5ustar0000000000000000terminal-size-0.3.2.1/src/System/Console/0000755000000000000000000000000012615425305016304 5ustar0000000000000000terminal-size-0.3.2.1/src/System/Console/Terminal/0000755000000000000000000000000012615425305020057 5ustar0000000000000000terminal-size-0.3.2.1/src/System/Console/Terminal/Windows.hs0000644000000000000000000000423012615425305022044 0ustar0000000000000000 module System.Console.Terminal.Windows(size) where import System.Console.Terminal.Common import Control.Monad import Data.Word import Foreign.Ptr import Foreign.Storable import Foreign.Marshal.Alloc import System.Exit import System.IO import System.Process type HANDLE = Ptr () data CONSOLE_SCREEN_BUFFER_INFO sizeCONSOLE_SCREEN_BUFFER_INFO :: Int sizeCONSOLE_SCREEN_BUFFER_INFO = 22 posCONSOLE_SCREEN_BUFFER_INFO_srWindow :: Int posCONSOLE_SCREEN_BUFFER_INFO_srWindow = 10 -- 4 x Word16 Left,Top,Right,Bottom c_STD_OUTPUT_HANDLE :: Word32 c_STD_OUTPUT_HANDLE = -11 foreign import stdcall unsafe "windows.h GetConsoleScreenBufferInfo" c_GetConsoleScreenBufferInfo :: HANDLE -> Ptr CONSOLE_SCREEN_BUFFER_INFO -> IO Bool foreign import stdcall unsafe "windows.h GetStdHandle" c_GetStdHandle :: Word32 -> IO HANDLE size :: Integral n => IO (Maybe (Window n)) size = do hdl <- c_GetStdHandle c_STD_OUTPUT_HANDLE allocaBytes sizeCONSOLE_SCREEN_BUFFER_INFO $ \p -> do b <- c_GetConsoleScreenBufferInfo hdl p if not b then do -- This could happen on Cygwin or MSYS let stty = (shell "stty size") { std_in = UseHandle stdin , std_out = CreatePipe } (_, mbStdout, _, rStty) <- createProcess stty exStty <- waitForProcess rStty case exStty of ExitFailure _ -> return Nothing ExitSuccess -> maybe (return Nothing) (\hSize -> do sizeStr <- hGetContents hSize let [r, c] = map read $ words sizeStr :: [Int] return $ Just $ Window (fromIntegral r) (fromIntegral c) ) mbStdout else do [left,top,right,bottom] <- forM [0..3] $ \i -> do v <- peekByteOff p ((i*2) + posCONSOLE_SCREEN_BUFFER_INFO_srWindow) return $ fromIntegral (v :: Word16) return $ Just $ Window (1+bottom-top) (1+right-left) terminal-size-0.3.2.1/src/System/Console/Terminal/Common.hs0000644000000000000000000000156112615425305021646 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveFoldable #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE DeriveTraversable #-} #if __GLASGOW_HASKELL__ >= 702 #define LANGUAGE_DeriveGeneric {-# LANGUAGE DeriveGeneric #-} #endif module System.Console.Terminal.Common ( Window(..) ) where import Data.Data (Typeable, Data) #if __GLASGOW_HASKELL__ < 710 import Data.Foldable (Foldable) import Data.Traversable (Traversable) #endif #ifdef LANGUAGE_DeriveGeneric import GHC.Generics ( Generic #if __GLASGOW_HASKELL__ >= 706 , Generic1 #endif ) #endif -- | Terminal window width and height data Window a = Window { height :: !a , width :: !a } deriving ( Show, Eq, Read, Data, Typeable , Foldable, Functor, Traversable #ifdef LANGUAGE_DeriveGeneric , Generic #if __GLASGOW_HASKELL__ >= 706 , Generic1 #endif #endif ) terminal-size-0.3.2.1/src/System/Console/Terminal/Posix.hsc0000644000000000000000000000344212615425305021663 0ustar0000000000000000 module System.Console.Terminal.Posix ( size, fdSize, hSize ) where import System.Console.Terminal.Common 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 #include #let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__) -- Interesting part of @struct winsize@ data CWin = CWin CUShort CUShort instance Storable CWin where sizeOf _ = (#size struct winsize) alignment _ = (#alignment struct winsize) peek ptr = do row <- (#peek struct winsize, ws_row) ptr col <- (#peek struct winsize, ws_col) ptr return $ CWin row col poke ptr (CWin row col) = do (#poke struct winsize, ws_row) ptr row (#poke struct winsize, ws_col) ptr col fdSize :: Integral n => Fd -> IO (Maybe (Window n)) fdSize (Fd fd) = with (CWin 0 0) $ \ws -> do throwErrnoIfMinus1 "ioctl" $ ioctl fd (#const TIOCGWINSZ) ws CWin row col <- peek ws return . Just $ Window (fromIntegral row) (fromIntegral col) `catch` handler where handler :: IOError -> IO (Maybe (Window h)) handler _ = return Nothing foreign import ccall "sys/ioctl.h ioctl" ioctl :: CInt -> CInt -> Ptr CWin -> IO CInt size :: Integral n => IO (Maybe (Window n)) size = fdSize (Fd (#const STDOUT_FILENO)) 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-0.3.2.1/src/System/Console/Terminal/Size.hs0000644000000000000000000000311612615425305021326 0ustar0000000000000000{-# LANGUAGE CPP #-} {- | Get terminal window height and width without ncurses dependency Based on answer by Andreas Hammar at -} module System.Console.Terminal.Size ( Window(..) , size #if !defined(mingw32_HOST_OS) , fdSize , hSize #endif ) where import System.Console.Terminal.Common #if defined(mingw32_HOST_OS) import qualified System.Console.Terminal.Windows as Host #else import qualified System.Console.Terminal.Posix as Host import System.Posix.Types(Fd) import System.IO(Handle) #endif -- | 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 = Host.size #if !defined(mingw32_HOST_OS) -- | /Not available on Windows:/ -- 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 -- >>> 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 = Host.fdSize -- | /Not available on Windows:/ -- 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 = Host.hSize #endif