From f83b424bf70b7b14b0268aeeafe1b3483fced49f Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 21 Mar 2024 05:35:00 +0100 Subject: chore: Git -> Backend --- app/Backend/CommitHash.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/Backend/CommitHash.hs (limited to 'app/Backend/CommitHash.hs') diff --git a/app/Backend/CommitHash.hs b/app/Backend/CommitHash.hs new file mode 100644 index 0000000..7ec1d54 --- /dev/null +++ b/app/Backend/CommitHash.hs @@ -0,0 +1,42 @@ +module Backend.CommitHash + ( CommitHash (..), + toShortText, + toText, + toTextUnsafe, + ) +where + +import Data.Binary (Binary) +import Data.Maybe (fromMaybe) +import Data.Text qualified as T +import GHC.Generics (Generic) +import Render qualified as P + +data CommitHash + = WorkingTree + | Commit T.Text + deriving (Eq, Ord, Show, Binary, Generic) + +toShortText :: CommitHash -> Maybe T.Text +toShortText = fmap (T.take 7) . toText + +toText :: CommitHash -> Maybe T.Text +toText WorkingTree = Nothing +toText (Commit hash) = Just hash + +toTextUnsafe :: CommitHash -> T.Text +toTextUnsafe (Commit hash) = hash +toTextUnsafe _ = error "toTextUnsafe: WorkingDir" + +instance P.Render CommitHash where + render = P.render . P.Detailed + +instance P.Render (P.Detailed CommitHash) where + render (P.Detailed commitHash) = + P.styled [P.color P.Yellow] $ + P.render (fromMaybe "" (toText commitHash)) + +instance P.Render (P.Summarized CommitHash) where + render (P.Summarized commitHash) = + P.styled [P.color P.Yellow] $ + P.render (fromMaybe "" (toShortText commitHash)) -- cgit v1.2.3