Skip to content

Commit

Permalink
Cygwin/MSYS support
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGlScott authored and supki committed May 29, 2015
1 parent 457b150 commit 6176787
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/System/Console/Terminal/Windows.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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 ()

Expand All @@ -34,8 +37,26 @@ 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 return Nothing 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)
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)
3 changes: 3 additions & 0 deletions terminal-size.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ library
if impl(ghc >= 7.4 && < 7.6)
build-depends:
ghc-prim
if os(windows)
build-depends:
process

build-tools:
hsc2hs
Expand Down

0 comments on commit 6176787

Please sign in to comment.