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

include generic Mainable instance for [(String, d)] #298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
30 changes: 14 additions & 16 deletions src/Diagrams/Backend/CmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,11 @@ 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.
-- | 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
Expand All @@ -504,6 +493,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
Expand Down