You can use hcurses . After initializing the library, you can use scrSize to get the number of rows and columns on the screen.
To use System.Posix.IOCtl , you need to define the data type to represent the TIOCGWINSZ request, which populates the following structure:
struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; };
You will need to define a Haskell data type to store this information and make it an instance of Storable :
{-# LANGUAGE RecordWildCards #-} import Foreign.Storable import Foreign.Ptr import Foreign.C data Winsize = Winsize { ws_row :: CUShort , ws_col :: CUShort , ws_xpixel :: CUShort , ws_ypixel :: CUShort } instance Storable Winsize where sizeOf _ = 8 alignment _ = 2 peek p = do { ws_row <- peekByteOff p 0 ; ws_col <- peekByteOff p 2 ; ws_xpixel <- peekByteOff p 4 ; ws_ypixel <- peekByteOff p 6 ; return $ Winsize {..} } poke p Winsize {..} = do { pokeByteOff p 0 ws_row ; pokeByteOff p 2 ws_col ; pokeByteOff p 4 ws_xpixel ; pokeByteOff p 6 ws_ypixel }
Now you need to create a dummy data type to represent your query:
data TIOCGWINSZ = TIOCGWINSZ
Finally, you need to make your request an IOControl instance IOControl and bind it to the Winsize data Winsize .
instance IOControl TIOCGWINSZ Winsize where ioctlReq _ = ??
Will you need to replace ?? the constant represented by TIOCGWINSZ in your header files ( 0x5413 on my system).
Now you are ready to issue ioctl . This command does not care about the input, so you want to use the ioctl' form:
main = do { ws <- ioctl' 1 TIOCGWINSZ ; putStrLn $ "My terminal is " ++ show (ws_col ws) ++ " columns wide" }
Please note that 1 refers to STDOUT.
Phew!
pat
source share