aboutsummaryrefslogtreecommitdiffstats
path: root/app/Patch.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-03 13:29:00 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-03 13:29:00 +0100
commit32c76fb1f411896a4727ba2a76cdfc9dcd3dc48a (patch)
tree9237f6dcd3c165d864861ab9533c0ed4a093d2a7 /app/Patch.hs
parent22c71e24edc8655f15c8dba02244b8e6d059da5c (diff)
feat: color patches
Diffstat (limited to 'app/Patch.hs')
-rw-r--r--app/Patch.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/app/Patch.hs b/app/Patch.hs
new file mode 100644
index 0000000..f1c547a
--- /dev/null
+++ b/app/Patch.hs
@@ -0,0 +1,37 @@
+module Patch
+ ( Patch,
+ parse,
+ render,
+ )
+where
+
+import Control.Exception (throw)
+import Data.Text qualified as T
+import Exception qualified as E
+import Prettyprinter (pretty)
+import Prettyprinter qualified as P
+import Prettyprinter.Render.Terminal qualified as P
+import Text.Diff.Parse qualified as D
+import Text.Diff.Parse.Types qualified as D
+
+newtype Patch = Patch
+ { fileDeltas :: D.FileDeltas
+ }
+ deriving (Show)
+
+parse :: T.Text -> Patch
+parse = either (throw . E.InvalidDiff) Patch . D.parseDiff
+
+render :: Patch -> P.Doc P.AnsiStyle
+render (Patch {..}) =
+ P.vsep $ map prettyFileDelta fileDeltas
+ where
+ prettyFileDelta (D.FileDelta {..}) = prettyContent fileDeltaContent
+ prettyContent D.Binary = P.emptyDoc
+ prettyContent (D.Hunks hunks) = P.vsep (map prettyHunk hunks)
+ prettyHunk (D.Hunk {..}) = P.vsep $ map prettyLine hunkLines
+ prettyLine (D.Line {..}) =
+ case lineAnnotation of
+ D.Added -> P.annotate (P.color P.Green) $ P.pretty ("+" :: T.Text) <> pretty lineContent
+ D.Removed -> P.annotate (P.color P.Red) $ P.pretty ("-" :: T.Text) <> pretty lineContent
+ D.Context -> P.annotate (P.color P.White) $ P.pretty (" " :: T.Text) <> pretty lineContent