2016-08-10 19:17:23 +03:00
|
|
|
module Renderer (Renderer, DiffArguments(..), Output(..), concatOutputs, Format(..)) where
|
2016-01-14 21:18:40 +03:00
|
|
|
|
2016-05-26 19:58:04 +03:00
|
|
|
import Prologue
|
2016-02-29 05:29:59 +03:00
|
|
|
import Data.Functor.Both
|
2016-01-14 21:18:40 +03:00
|
|
|
import Diff
|
2016-08-10 19:17:23 +03:00
|
|
|
import Source (SourceBlob)
|
|
|
|
import Data.Aeson.Types (Series, pairs)
|
|
|
|
import Data.Text as T (intercalate)
|
|
|
|
import Data.Aeson.Encoding (encodingToLazyByteString)
|
2016-01-14 21:18:40 +03:00
|
|
|
|
2016-07-29 19:24:12 +03:00
|
|
|
-- | A function that will render a diff, given the two source blobs.
|
2016-08-10 01:51:21 +03:00
|
|
|
type Renderer annotation = Both SourceBlob -> Diff Text annotation -> Output
|
2016-04-01 22:34:52 +03:00
|
|
|
|
|
|
|
data DiffArguments = DiffArguments { format :: Format, output :: Maybe FilePath, outputPath :: FilePath }
|
2016-04-12 20:10:24 +03:00
|
|
|
deriving (Show)
|
2016-04-01 22:34:52 +03:00
|
|
|
|
2016-08-10 01:51:21 +03:00
|
|
|
data Output = SplitOutput Text | PatchOutput Text | JSONOutput Series | SummaryOutput Series
|
|
|
|
|
2016-08-10 19:17:23 +03:00
|
|
|
concatOutputs :: [Output] -> Text
|
|
|
|
concatOutputs l@(JSONOutput _ : _) = toS . encodingToLazyByteString . pairs . mconcat $ toSeries <$> l
|
|
|
|
concatOutputs l@(SummaryOutput _ : _) = toS . encodingToLazyByteString . pairs . mconcat $ toSeries <$> l
|
|
|
|
concatOutputs l = T.intercalate "\n" (toText <$> l)
|
|
|
|
|
|
|
|
toSeries :: Output -> Series
|
|
|
|
toSeries (JSONOutput series) = series
|
|
|
|
toSeries (SummaryOutput series) = series
|
2016-08-17 05:03:48 +03:00
|
|
|
toSeries _ = mempty
|
2016-08-10 19:17:23 +03:00
|
|
|
|
|
|
|
toText :: Output -> Text
|
|
|
|
toText (SplitOutput text) = text
|
|
|
|
toText (PatchOutput text) = text
|
2016-08-17 05:03:48 +03:00
|
|
|
toText _ = mempty
|
2016-08-10 19:17:23 +03:00
|
|
|
|
|
|
|
|
2016-04-01 22:34:52 +03:00
|
|
|
-- | The available types of diff rendering.
|
2016-05-18 19:01:16 +03:00
|
|
|
data Format = Split | Patch | JSON | Summary
|
2016-04-12 20:10:24 +03:00
|
|
|
deriving (Show)
|