terminal-size (empty) → 0.1.0.0
raw patch · 4 files changed
+118/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/System/Console/Terminal/Size.hsc +61/−0
- terminal-size.cabal +25/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/System/Console/Terminal/Size.hsc view
@@ -0,0 +1,61 @@+{- |+Get terminal window height and width without ncurses dependency++Only tested to work on GNU/Linux systems++Based on answer by Andreas Hammar at <http://stackoverflow.com/a/12807521/972985>+-}+module System.Console.Terminal.Size+ ( Window(..), size+ ) where++import Foreign+import Foreign.C.Error+import Foreign.C.Types++#include <sys/ioctl.h>+#include <unistd.h>+++#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)+++-- Interesting part of @struct winsize@+data CWin = CWin { wsRow, wsCol :: 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+++-- | Terminal window width and height+data Window a = Window+ { height :: !a+ , width :: !a+ } deriving (Show, Read)++instance Functor Window where+ fmap f (Window { height = h, width = w }) = Window { height = f h, width = f w }+++-- | Get terminal window width and height+--+-- >>> import System.Console.Terminal.Size+-- >>> size+-- Window {height = 60, width = 112}+size :: Integral n => IO (Window n)+size = with (CWin 0 0) $ \ws -> do+ throwErrnoIfMinus1 "ioctl" $+ ioctl (#const STDOUT_FILENO) (#const TIOCGWINSZ) ws+ CWin row col <- peek ws+ return $ Window (fromIntegral row) (fromIntegral col)++foreign import ccall "sys/ioctl.h ioctl"+ ioctl :: CInt -> CInt -> Ptr CWin -> IO CInt
+ terminal-size.cabal view
@@ -0,0 +1,25 @@+name: terminal-size+version: 0.1.0.0+synopsis: Get terminal window height and width+description:+ Get terminal window height and width without ncurses dependency+ .+ Only tested to work on GNU/Linux systems+license: BSD3+license-file: LICENSE+author: Andreas Hammar, Matvey Aksenov+maintainer: matvey.aksenov@gmail.com+category: System+build-type: Simple+cabal-version: >= 1.10++library+ default-language: Haskell2010+ build-depends: base >= 4 && < 5+ build-tools: hsc2hs+ hs-source-dirs: src+ exposed-modules: System.Console.Terminal.Size++source-repository head+ type: git+ location: https://github.com/biegunka/terminal-size