From 08f75854a59c2ebc4cf6685d5d5f7041d7ef29e3 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Tue, 17 Oct 2023 12:16:24 +0200 Subject: make --width a global option --- app/Main.hs | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index e8440f7..28632ad 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -426,7 +426,7 @@ -- COMMENT Maybe `anissue tags @topic` could serve that purpose? I imagine -- `anissue tags` (without a tag given) to list all tags. -- --- *Rationale: I would like to push for concepts out of anissue for as long as +-- \*Rationale: I would like to push for concepts out of anissue for as long as -- feasible . Currently, there is no notion of "topics".* -- TODO Add HTTP server @@ -445,6 +445,7 @@ module Main where +import Control.Applicative ((<|>)) import Data.Function ((&)) import Data.List (find) import Data.Maybe (catMaybes) @@ -476,7 +477,8 @@ data Options = Options { command :: Command, internalTags :: Bool, colorize :: Color, - noPager :: Bool + noPager :: Bool, + width :: Maybe Int } deriving (Show) @@ -493,6 +495,7 @@ options = <*> internalTagsFlag <*> colorOption <*> noPagerFlag + <*> widthOption internalTagsFlag :: O.Parser Bool internalTagsFlag = @@ -524,6 +527,18 @@ noPagerFlag = <> O.help "Don't pipe long output to $PAGER." ) +widthOption :: O.Parser (Maybe Int) +widthOption = + O.optional + ( O.option + O.auto + ( O.long "width" + <> O.short 'w' + <> O.metavar "INT" + <> O.help "Wheather to insert line breaks after at most that many characters." + ) + ) + data Command = List { files :: [String], @@ -531,8 +546,7 @@ data Command sort :: [Sort] } | Show - { id :: String, - width :: Maybe Int + { id :: String } deriving (Show) @@ -556,7 +570,6 @@ showCmd :: O.Parser Command showCmd = Show <$> idArg - <*> widthOption filesArg :: O.Parser [String] filesArg = O.many (O.strArgument (O.metavar "FILE" <> O.action "file")) @@ -571,18 +584,6 @@ idArg = ) ) -widthOption :: O.Parser (Maybe Int) -widthOption = - O.optional - ( O.option - O.auto - ( O.long "width" - <> O.short 'w' - <> O.metavar "INT" - <> O.help "Wheather to insert line breaks after at most that many characters." - ) - ) - die :: String -> IO a die s = do printf "error: %s\n" s @@ -591,9 +592,9 @@ die s = do main :: IO () main = do O.execParser (O.info (options <**> O.helper) O.idm) >>= \case - Options {colorize, noPager, command = List {sort, filters, files}} -> do + Options {colorize, noPager, width, command = List {sort, filters, files}} -> do issues <- listIssues sort filters files - putDoc colorize noPager . P.vsep $ + putDoc colorize noPager width . P.vsep $ map ( \issue -> let title = P.annotate P.bold $ P.pretty issue.title @@ -624,7 +625,7 @@ main = do ] ) issues - Options {colorize, command = Show {id, width}} -> do + Options {colorize, width, command = Show {id}} -> do issues <- listIssues [] [] [] case find ((==) (Just id) . I.id) issues of Nothing -> die (printf "no issue with id `%s'\n" id) @@ -633,7 +634,7 @@ main = do -- -- We have to set `noPager` unconditionally to `True` for now, as not -- all output is `mdcat` compatible. - putDoc colorize True $ + putDoc colorize True width $ P.annotate (P.color P.Green) $ P.pretty $ issue.file @@ -665,7 +666,7 @@ main = do ) ) ) - putDoc colorize True $ + putDoc colorize True width $ P.pretty $ "\n@file " ++ issue.file @@ -673,15 +674,19 @@ main = do ++ show issue.start.row ++ "\n" -putDoc :: Color -> Bool -> P.Doc P.AnsiStyle -> IO () -putDoc colorize noPager doc = do +putDoc :: Color -> Bool -> Maybe Int -> P.Doc P.AnsiStyle -> IO () +putDoc colorize noPager width doc = do isTty <- (== 1) <$> c_isatty 1 term <- Terminal.size let s = P.renderLazy $ P.layoutSmart P.defaultLayoutOptions - { P.layoutPageWidth = maybe P.Unbounded (\n -> P.AvailablePerLine n 1) (Terminal.width <$> term) + { P.layoutPageWidth = + maybe + P.Unbounded + (\n -> P.AvailablePerLine n 1) + (width <|> (Terminal.width <$> term)) } $ ( if colorize == Always || (colorize == Auto && isTty) then (\x -> x) -- cgit v1.2.3