-- Copyright (C) 2003 David Roundy
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2, or (at your option)
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; see the file COPYING.  If not, write to
-- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-- Boston, MA 02110-1301, USA.

module Darcs.UI.PrintPatch
    ( contextualPrintPatch
    , printContent
    , printContentWithPager
    , printFriendly
    , printSummary
    , showFriendly
    , showWithSummary
    ) where

import Darcs.Prelude

import Darcs.Patch ( description, showContextPatch, content, summary )
import Darcs.Patch.Apply ( ApplyState )
import Darcs.Patch.Show ( ShowContextPatch, ShowPatch, ShowPatchFor(ForDisplay) )
import Darcs.UI.External ( viewDocWith )
import Darcs.UI.Options.All ( Verbosity(..), WithContext(..), WithSummary(..) )
import Darcs.Util.Printer ( Doc, prefix, putDocLnWith, ($$) )
import Darcs.Util.Printer.Color ( fancyPrinters )
import Darcs.Util.Tree ( Tree )
import Darcs.Util.Tree.Monad ( virtualTreeIO )

-- | @'printFriendly' opts patch@ prints @patch@ in accordance with the flags
-- in opts, ie, whether @--verbose@ or @--summary@ were passed at the
-- command-line.
printFriendly :: (ShowPatch p, ShowContextPatch p, ApplyState p ~ Tree) => Maybe (Tree IO)
              -> Verbosity -> WithSummary -> WithContext -> p wX wY -> IO ()
printFriendly :: Maybe (Tree IO)
-> Verbosity -> WithSummary -> WithContext -> p wX wY -> IO ()
printFriendly (Just Tree IO
pristine) Verbosity
_ WithSummary
_ WithContext
YesContext = Tree IO -> p wX wY -> IO ()
forall (p :: * -> * -> *) wX wY.
(ShowContextPatch p, ApplyState p ~ Tree) =>
Tree IO -> p wX wY -> IO ()
contextualPrintPatch Tree IO
pristine
printFriendly Maybe (Tree IO)
_ Verbosity
v WithSummary
s WithContext
_ = Printers -> Doc -> IO ()
putDocLnWith Printers
fancyPrinters (Doc -> IO ()) -> (p wX wY -> Doc) -> p wX wY -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Verbosity -> WithSummary -> p wX wY -> Doc
forall (p :: * -> * -> *) wX wY.
ShowPatch p =>
Verbosity -> WithSummary -> p wX wY -> Doc
showFriendly Verbosity
v WithSummary
s

-- | @'showFriendly' flags patch@ returns a 'Doc' representing the right
-- way to show @patch@ given the list @flags@ of flags darcs was invoked with.
showFriendly :: ShowPatch p => Verbosity -> WithSummary -> p wX wY -> Doc
showFriendly :: Verbosity -> WithSummary -> p wX wY -> Doc
showFriendly Verbosity
Verbose WithSummary
_          = p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
showWithContents
showFriendly Verbosity
_       WithSummary
YesSummary = p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
showWithSummary
showFriendly Verbosity
_       WithSummary
NoSummary  = p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
description

showWithSummary :: ShowPatch p => p wX wY -> Doc
showWithSummary :: p wX wY -> Doc
showWithSummary p wX wY
p = p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
description p wX wY
p Doc -> Doc -> Doc
$$ String -> Doc -> Doc
prefix String
"    " (p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
summary p wX wY
p)

showWithContents :: ShowPatch p => p wX wY -> Doc
showWithContents :: p wX wY -> Doc
showWithContents p wX wY
p = p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
description p wX wY
p Doc -> Doc -> Doc
$$ String -> Doc -> Doc
prefix String
"    " (p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
content p wX wY
p)

printSummary :: ShowPatch p => p wX wY -> IO ()
printSummary :: p wX wY -> IO ()
printSummary = Printers -> Doc -> IO ()
putDocLnWith Printers
fancyPrinters (Doc -> IO ()) -> (p wX wY -> Doc) -> p wX wY -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc -> Doc
prefix String
"    " (Doc -> Doc) -> (p wX wY -> Doc) -> p wX wY -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
summary

printContent :: ShowPatch p => p wX wY -> IO ()
printContent :: p wX wY -> IO ()
printContent = Printers -> Doc -> IO ()
putDocLnWith Printers
fancyPrinters (Doc -> IO ()) -> (p wX wY -> Doc) -> p wX wY -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc -> Doc
prefix String
"    " (Doc -> Doc) -> (p wX wY -> Doc) -> p wX wY -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
content

printContentWithPager :: ShowPatch p => p wX wY -> IO ()
printContentWithPager :: p wX wY -> IO ()
printContentWithPager = Printers -> Doc -> IO ()
viewDocWith Printers
fancyPrinters (Doc -> IO ()) -> (p wX wY -> Doc) -> p wX wY -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc -> Doc
prefix String
"    " (Doc -> Doc) -> (p wX wY -> Doc) -> p wX wY -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p wX wY -> Doc
forall (p :: * -> * -> *) wX wY. ShowPatch p => p wX wY -> Doc
content

-- | 'contextualPrintPatch' prints a patch, together with its context, on
-- standard output.
contextualPrintPatch :: (ShowContextPatch p, ApplyState p ~ Tree) => Tree IO
                     -> p wX wY -> IO ()
contextualPrintPatch :: Tree IO -> p wX wY -> IO ()
contextualPrintPatch Tree IO
s p wX wY
p = do
    (Doc
contextedPatch, Tree IO
_) <- TreeIO Doc -> Tree IO -> IO (Doc, Tree IO)
forall a. TreeIO a -> Tree IO -> IO (a, Tree IO)
virtualTreeIO (ShowPatchFor -> p wX wY -> TreeIO Doc
forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(ShowContextPatch p, ApplyMonad (ApplyState p) m) =>
ShowPatchFor -> p wX wY -> m Doc
showContextPatch ShowPatchFor
ForDisplay p wX wY
p) Tree IO
s
    Printers -> Doc -> IO ()
putDocLnWith Printers
fancyPrinters Doc
contextedPatch