From 95158b4a25a092aabeefe075a03d275df179cea8 Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Mon, 29 May 2017 06:46:42 -0500 Subject: [PATCH 1/2] include generic Mainable instance for [(String, d)] Closes #293. --- src/Diagrams/Backend/CmdLine.hs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/Diagrams/Backend/CmdLine.hs b/src/Diagrams/Backend/CmdLine.hs index 1daead12..0730aa2e 100644 --- a/src/Diagrams/Backend/CmdLine.hs +++ b/src/Diagrams/Backend/CmdLine.hs @@ -478,22 +478,6 @@ instance Mainable d => Mainable (IO d) where mainRender opts dio = dio >>= mainRender opts --- | @defaultMultiMainRender@ is an implementation of 'mainRender' where --- instead of a single diagram it takes a list of diagrams paired with names --- as input. The generated executable then takes a @--selection@ option --- specifying the name of the diagram that should be rendered. The list of --- available diagrams may also be printed by passing the option @--list@. --- --- Typically a backend can write its @[(String,QDiagram b v n Any)]@ instance as --- --- @ --- instance Mainable [(String,QDiagram b v n Any)] where --- type MainOpts [(String,QDiagram b v n Any)] = (DiagramOpts, DiagramMultiOpts) --- mainRender = defaultMultiMainRender --- @ --- --- We do not provide this instance in general so that backends can choose to --- opt-in to this form or provide a different instance that makes more sense. defaultMultiMainRender :: Mainable d => (MainOpts d, DiagramMultiOpts) -> [(String, d)] -> IO () defaultMultiMainRender (opts,multi) ds = if multi^.list @@ -504,6 +488,15 @@ defaultMultiMainRender (opts,multi) ds = Nothing -> putStrLn $ "Unknown diagram: " ++ sel Just d -> mainRender opts d +-- | This instance allows pairing 'Mainable' things with 'String's; +-- the generated executable takes a @--selection@ option specifying +-- the name of the thing that should be rendered. The list of +-- available things may also be printed by passing the option +-- @--list@. +instance Mainable d => Mainable [(String, d)] where + type MainOpts [(String, d)] = (MainOpts d, DiagramMultiOpts) + mainRender = defaultMultiMainRender + -- | Display the list of diagrams available for rendering. showDiaList :: [String] -> IO () showDiaList ds = do From 5546869dc800729aee004a596d068a5525c6e61c Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Mon, 29 May 2017 08:45:38 -0500 Subject: [PATCH 2/2] comment on defaultMultiMainRender --- src/Diagrams/Backend/CmdLine.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Diagrams/Backend/CmdLine.hs b/src/Diagrams/Backend/CmdLine.hs index 0730aa2e..ec472fb4 100644 --- a/src/Diagrams/Backend/CmdLine.hs +++ b/src/Diagrams/Backend/CmdLine.hs @@ -478,6 +478,11 @@ instance Mainable d => Mainable (IO d) where mainRender opts dio = dio >>= mainRender opts +-- | Used to implement the 'Mainable' instance for '[(String, d)]'. +-- This used to be exported so backends could use it to implement +-- their own instances; now a generic instance is provided. Hence +-- backends should no longer use this function, but it is still +-- exported for now to avoid unnecessary breakage. defaultMultiMainRender :: Mainable d => (MainOpts d, DiagramMultiOpts) -> [(String, d)] -> IO () defaultMultiMainRender (opts,multi) ds = if multi^.list