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 11 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
8 changes: 8 additions & 0 deletions src/Stack/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Stack.Constants
,platformVariantEnvVar
,compilerOptionsCabalFlag
,ghcColorForceFlag
,minTerminalWidth
,defaultTerminalWidth
)
where

Expand Down Expand Up @@ -216,3 +218,9 @@ compilerOptionsCabalFlag Ghcjs = "--ghcjs-options"

ghcColorForceFlag :: String
ghcColorForceFlag = "-fdiagnostics-color=always"

minTerminalWidth :: Int
minTerminalWidth = 20
Copy link
Contributor

Choose a reason for hiding this comment

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

20 is really small. I think 40 might be a better minimum. Reasoning is that the user might resize their terminal.

I think having a max makes sense too, it's unpleasant to read a whole paragraph of text stretched on a line. Maybe 200 chars? Of course, an answer to that would be why doesn't the user resize their terminal. I use XMonad, though, so if I open up a terminal on a 4k display, I probably don't really want paragraphs to be single lines, though that does happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I wasn't sure what to do for a min. 40 does look more reasonable. I'm indifferent to a maximum personally, but that reasoning seems to make sense, I'll throw it in.

Hm, I just noticed that I'm doing the checking all wrong too, it's only checking the user-supplied one, and it's just ignoring it completely if it's too small. Should probably just clip to the allowed range if it's outside it. I'll redo that part, heh.


defaultTerminalWidth :: Int
defaultTerminalWidth = 100
5 changes: 5 additions & 0 deletions src/Stack/Options/GlobalParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ globalOptsParser currentDir kind defLogLevel =
completeWith ["always", "never", "auto"] <>
help "Specify when to use color in output; WHEN is 'always', 'never', or 'auto'" <>
hide)) <*>
optionalFirst (option auto
(long "terminal-width" <>
metavar "INT" <>
help "Specify the width of the terminal, used for pretty-print messages")) <*>
optionalFirst
(strOption
(long "stack-yaml" <>
Expand All @@ -64,6 +68,7 @@ globalOptsFromMonoid defaultTerminal GlobalOptsMonoid{..} = GlobalOpts
, globalCompiler = getFirst globalMonoidCompiler
, globalTerminal = fromFirst defaultTerminal globalMonoidTerminal
, globalColorWhen = fromFirst ColorAuto globalMonoidColorWhen
, globalTermWidth = getFirst globalMonoidTermWidth
, globalStackYaml = maybe SYLDefault SYLOverride $ getFirst globalMonoidStackYaml }

initOptsParser :: Parser InitOpts
Expand Down
3 changes: 2 additions & 1 deletion src/Stack/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ displayWithColor
=> a -> m T.Text
displayWithColor x = do
useAnsi <- liftM logUseColor $ view logOptionsL
return $ if useAnsi then displayAnsi x else displayPlain x
termWidth <- liftM logTermWidth $ view logOptionsL
return $ (if useAnsi then displayAnsi else displayPlain) termWidth x

-- TODO: switch to using implicit callstacks once 7.8 support is dropped

Expand Down
1 change: 1 addition & 0 deletions src/Stack/Runners.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ withRunnerGlobal GlobalOpts{..} = withRunner
globalTimeInLog
globalTerminal
globalColorWhen
globalTermWidth
(isJust globalReExecVersion)

withMiniConfigAndLock
Expand Down
2 changes: 2 additions & 0 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ data GlobalOpts = GlobalOpts
, globalCompiler :: !(Maybe (CompilerVersion 'CVWanted)) -- ^ Compiler override
, globalTerminal :: !Bool -- ^ We're in a terminal?
, globalColorWhen :: !ColorWhen -- ^ When to use ansi terminal colors
, globalTermWidth :: !(Maybe Int) -- ^ Terminal width override
, globalStackYaml :: !(StackYamlLoc FilePath) -- ^ Override project stack.yaml
} deriving (Show)

Expand All @@ -465,6 +466,7 @@ data GlobalOptsMonoid = GlobalOptsMonoid
, globalMonoidCompiler :: !(First (CompilerVersion 'CVWanted)) -- ^ Compiler override
, globalMonoidTerminal :: !(First Bool) -- ^ We're in a terminal?
, globalMonoidColorWhen :: !(First ColorWhen) -- ^ When to use ansi colors
, globalMonoidTermWidth :: !(First Int) -- ^ Terminal width override
, globalMonoidStackYaml :: !(First FilePath) -- ^ Override project stack.yaml
} deriving (Show, Generic)

Expand Down
13 changes: 12 additions & 1 deletion src/Stack/Types/Runner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import Language.Haskell.TH
import Language.Haskell.TH.Syntax (lift)
import Lens.Micro
import Stack.Prelude hiding (lift)
import Stack.Constants
import System.Console.ANSI
import System.FilePath
import System.IO
import System.Log.FastLogger
import System.Terminal

-- | Monadic environment.
data Runner = Runner
Expand Down Expand Up @@ -77,6 +79,7 @@ newtype Sticky = Sticky

data LogOptions = LogOptions
{ logUseColor :: Bool
, logTermWidth :: Int
, logUseUnicode :: Bool
, logUseTime :: Bool
, logMinLevel :: LogLevel
Expand Down Expand Up @@ -243,19 +246,24 @@ withRunner :: MonadIO m
-> Bool -- ^ use time?
-> Bool -- ^ terminal?
-> ColorWhen
-> Maybe Int -- ^ terminal width override
-> Bool -- ^ reexec?
-> (Runner -> m a)
-> m a
withRunner logLevel useTime terminal colorWhen reExec inner = do
withRunner logLevel useTime terminal colorWhen widthOverride reExec inner = do
useColor <- case colorWhen of
ColorNever -> return False
ColorAlways -> return True
ColorAuto -> liftIO $ hSupportsANSI stderr
termWidth <- case widthOverride >>= checkWidth of
Nothing -> fromMaybe defaultTerminalWidth <$> liftIO getTerminalWidth
Just w -> return w
canUseUnicode <- liftIO getCanUseUnicode
withSticky terminal $ \sticky -> inner Runner
{ runnerReExec = reExec
, runnerLogOptions = LogOptions
{ logUseColor = useColor
, logTermWidth = termWidth
, logUseUnicode = canUseUnicode
, logUseTime = useTime
, logMinLevel = logLevel
Expand All @@ -264,6 +272,9 @@ withRunner logLevel useTime terminal colorWhen reExec inner = do
, runnerTerminal = terminal
, runnerSticky = sticky
}
where checkWidth w
| w < minTerminalWidth = Nothing
| otherwise = Just w

-- | Taken from GHC: determine if we should use Unicode syntax
getCanUseUnicode :: IO Bool
Expand Down
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
18 changes: 9 additions & 9 deletions src/Text/PrettyPrint/Leijen/Extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,24 @@ instance HasAnsiAnn AnsiAnn where
instance HasAnsiAnn () where
getAnsiAnn _ = mempty

displayPlain :: Display a => a -> T.Text
displayPlain = LT.toStrict . displayAnsiSimple . renderDefault . fmap (const mempty) . display
displayPlain :: Display a => Int -> a -> T.Text
displayPlain w = LT.toStrict . displayAnsiSimple . renderDefault w . fmap (const mempty) . display

-- TODO: tweak these settings more?
-- TODO: options for settings if this is released as a lib

renderDefault :: Doc a -> SimpleDoc a
renderDefault = renderPretty 1 120
renderDefault :: Int -> Doc a -> SimpleDoc a
renderDefault = renderPretty 1

displayAnsi :: (Display a, HasAnsiAnn (Ann a)) => a -> T.Text
displayAnsi = LT.toStrict . displayAnsiSimple . renderDefault . toAnsiDoc . display
displayAnsi :: (Display a, HasAnsiAnn (Ann a)) => Int -> a -> T.Text
displayAnsi w = LT.toStrict . displayAnsiSimple . renderDefault w . toAnsiDoc . display

hDisplayAnsi
:: (Display a, HasAnsiAnn (Ann a), MonadIO m)
=> Handle -> a -> m ()
hDisplayAnsi h x = liftIO $ do
=> Handle -> Int -> a -> m ()
hDisplayAnsi h w x = liftIO $ do
useAnsi <- hSupportsANSI h
T.hPutStr h $ if useAnsi then displayAnsi x else displayPlain x
T.hPutStr h $ if useAnsi then displayAnsi w x else displayPlain w x

displayAnsiSimple :: SimpleDoc AnsiAnn -> LT.Text
displayAnsiSimple doc =
Expand Down
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