Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect terminal width and use it for pretty-printed output #3395

Merged
merged 19 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/System/Terminal.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{-# LANGUAGE CPP #-}
#ifndef WINDOWS
{-# LANGUAGE ForeignFunctionInterface #-}
#endif

module System.Terminal
( getTerminalWidth
) where

#ifndef WINDOWS
import Foreign
import Foreign.C.Types

#include <sys/ioctl.h>
#include <unistd.h>

newtype WindowWidth = WindowWidth CUShort
deriving (Eq, Ord, Show)

instance Storable WindowWidth where
sizeOf _ = (#size struct winsize)
alignment _ = (#alignment struct winsize)
peek p = WindowWidth <$> (#peek struct winsize, ws_col) p
poke p (WindowWidth w) = do
(#poke struct winsize, ws_col) p w

foreign import ccall "sys/ioctl.h ioctl"
ioctl :: CInt -> CInt -> Ptr WindowWidth -> IO CInt
#endif

-- | Get the width, in columns, of the terminal if we can.
getTerminalWidth :: IO (Maybe Int)
#ifndef WINDOWS
getTerminalWidth =
alloca $ \p -> do
errno <- ioctl (#const STDOUT_FILENO) (#const TIOCGWINSZ) p
if errno < 0
then return Nothing
else do
WindowWidth w <- peek p
return . Just . fromIntegral $ w
#else
getTerminalWidth = pure Nothing
#endif
2 changes: 2 additions & 0 deletions stack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ library
System.Process.PagerEditor
System.Process.Read
System.Process.Run
System.Terminal
other-modules: Hackage.Security.Client.Repository.HttpLib.HttpClient
build-depends: Cabal >= 2.0 && < 2.1
, aeson (>= 1.0 && < 1.2)
Expand Down Expand Up @@ -267,6 +268,7 @@ library
, store-core >= 0.4 && < 0.5
, annotated-wl-pprint
, file-embed >= 0.0.10
build-tools: hsc2hs >= 0.68
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even correct? It doesn't seem to help the appveyor problem, but maybe it's supposed to be there anyway?

I've never really used build-tools before, and I also can't tell if hsc2hs is part of GHC and thus doesn't need to be listed here or what.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could be that hsc2hs needs to be added to stackage. For now, adding stack install hsc2hs to the travis script should fix the issue. Adding hsc2hs to extra-deps might help, as it would specify a version to install, not 100% sure if that works, it used to not, but that may have been fixed.

Copy link
Collaborator Author

@kadoban kadoban Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice catch, it isn't in stackage. Appveyor seems to be the only one that doesn't like it.

Huh, I'm getting other travis CI errors that look like actual code problems. Wacky that I'm not getting it locally though, I'll have to look into that. Edit: fixed travisCI

if os(windows)
cpp-options: -DWINDOWS
build-depends: Win32
Expand Down