-
Notifications
You must be signed in to change notification settings - Fork 841
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
Changes from 11 commits
c111b33
da253f2
0cf7ba9
2910f35
72de492
b2cb946
2855cbc
54ad0f3
83a018e
d95758c
a200489
9d10d34
cc94e9e
0f607b3
baf9a6b
1d5a7bc
43cd310
c8c4f94
e4ba9d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -267,6 +268,7 @@ library | |
, store-core >= 0.4 && < 0.5 | ||
, annotated-wl-pprint | ||
, file-embed >= 0.0.10 | ||
build-tools: hsc2hs >= 0.68 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
if os(windows) | ||
cpp-options: -DWINDOWS | ||
build-depends: Win32 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.