module Darcs.Patch.FileHunk
    ( FileHunk(..), IsHunk(..), showFileHunk
    )
    where

import Darcs.Prelude

import Darcs.Util.Path ( AnchoredPath )
import Darcs.Patch.Format ( FileNameFormat )
import Darcs.Patch.Invert ( Invert(..) )
import Darcs.Patch.Show ( formatFileName )

import Darcs.Util.Printer
    ( Doc, blueText, text, lineColor, vcat, userchunkPS
    , prefix, ($$), (<+>), Color(Cyan, Magenta) )

import qualified Data.ByteString as B ( ByteString )


data FileHunk wX wY = FileHunk !AnchoredPath !Int [B.ByteString] [B.ByteString]

type role FileHunk nominal nominal

class IsHunk p where
    isHunk :: p wX wY -> Maybe (FileHunk wX wY)

showFileHunk :: FileNameFormat -> FileHunk wX wY -> Doc
showFileHunk :: FileNameFormat -> FileHunk wX wY -> Doc
showFileHunk FileNameFormat
x (FileHunk AnchoredPath
f Int
line [ByteString]
old [ByteString]
new) =
           String -> Doc
blueText String
"hunk" Doc -> Doc -> Doc
<+> FileNameFormat -> AnchoredPath -> Doc
formatFileName FileNameFormat
x AnchoredPath
f Doc -> Doc -> Doc
<+> String -> Doc
text (Int -> String
forall a. Show a => a -> String
show Int
line)
        Doc -> Doc -> Doc
$$ Color -> Doc -> Doc
lineColor Color
Magenta (String -> Doc -> Doc
prefix String
"-" ([Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ByteString -> Doc) -> [ByteString] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Doc
userchunkPS [ByteString]
old))
        Doc -> Doc -> Doc
$$ Color -> Doc -> Doc
lineColor Color
Cyan    (String -> Doc -> Doc
prefix String
"+" ([Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ByteString -> Doc) -> [ByteString] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Doc
userchunkPS [ByteString]
new))

instance Invert FileHunk where
    invert :: FileHunk wX wY -> FileHunk wY wX
invert (FileHunk AnchoredPath
path Int
line [ByteString]
old [ByteString]
new) = AnchoredPath
-> Int -> [ByteString] -> [ByteString] -> FileHunk wY wX
forall wX wY.
AnchoredPath
-> Int -> [ByteString] -> [ByteString] -> FileHunk wX wY
FileHunk AnchoredPath
path Int
line [ByteString]
new [ByteString]
old