Skip to content

Commit

Permalink
using terminal-size for help width
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgurakgun committed May 21, 2016
1 parent da1cbc2 commit 7f1b53d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions conjure-cp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Executable conjure
build-depends : conjure-cp
, base
, cmdargs
, terminal-size >= 0.3.2.1
default-extensions:
NoImplicitPrelude
OverloadedStrings
Expand Down
23 changes: 23 additions & 0 deletions src/exec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import Data.Char ( isDigit )
-- cmdargs
import System.Console.CmdArgs ( cmdArgs )

-- terminal-size
import qualified System.Console.Terminal.Size as Terminal ( size, width )


main :: IO ()
main = do
Expand All @@ -33,8 +36,28 @@ main = do
then ["--help"]
else args

-- if a width is not specified, we try to find out their terminal width automatically
-- and use the full width.
helpAutoWidth args = do
let
rawHelpVals = mapMaybe (stripPrefix "--help=") args
helpVals = concatMap (splitOn ",") rawHelpVals
containsWidth = any (all isDigit) helpVals
containsHelp = any (isPrefixOf "--help") args
case (containsHelp, containsWidth) of
(False, _) -> return args -- no "--help*"s were given
(_, True) -> return args -- a width was specified
_ -> do -- use the full with of the terminal
terminalSize <- Terminal.size
case terminalSize of
Nothing -> return args -- cannot work out the terminal size
Just s -> do
let terminalWidth = Terminal.width s
return $ args ++ ["--help=" ++ intercalate "," (helpVals ++ [show (terminalWidth :: Int)])]

args <- getArgs >>= return . compatRefineParam
>>= return . noArgPrintsHelp
>>= helpAutoWidth
input <- withArgs args (cmdArgs ui)
let workload = runLoggerPipeIO (logLevel input) $ do
logDebug ("Command line options: " <+> pretty (show input))
Expand Down

0 comments on commit 7f1b53d

Please sign in to comment.