From 19b148277aa5a80bce1c87e33bb857e66698f4eb Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 8 Dec 2023 07:29:35 +0100 Subject: feat: easily separate renderables by blank lines --- app/Render.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'app/Render.hs') diff --git a/app/Render.hs b/app/Render.hs index f63a3bc..964581f 100644 --- a/app/Render.hs +++ b/app/Render.hs @@ -10,6 +10,7 @@ module Render -- * Render Render (..), (<<<), + (===), styled, -- * Reporting styles @@ -72,6 +73,24 @@ instance Render Day where startsWithNL = ("\n" `isPrefixOf`) . show . render endsWithNL = ("\n" `isSuffixOf`) . show . render +(===) :: (Render a, Render b) => a -> b -> Doc AnsiStyle +(===) a' b' = + case (nonEmpty a', nonEmpty b') of + (Nothing, Nothing) -> emptyDoc + (Just a, Nothing) -> a + (Nothing, Just b) -> b + (Just a, Just b) -> + if + | endsWithNL a && startsWithNL b -> a <> b + | endsWithNL a || startsWithNL b -> a <> hardline <> b + | otherwise -> a <> hardline <> hardline <> b + where + nonEmpty x' = + let x = render x' + in if not (null (show x)) then Just (render x) else Nothing + startsWithNL = ("\n" `isPrefixOf`) . show . render + endsWithNL = ("\n" `isSuffixOf`) . show . render + -- | `styled` allows to annotate a document with multiple annotations. It obsoletes prettyprinter's `annotate`. styled :: [AnsiStyle] -> Doc AnsiStyle -> Doc AnsiStyle styled [] doc = doc -- cgit v1.2.3