summaryrefslogtreecommitdiff
path: root/src/Erebos/TextFormat.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2026-02-07 17:10:21 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2026-02-07 17:10:21 +0100
commit722e30758b7a222a0e074bd17d8116001560c156 (patch)
tree29779a5114865dc20a6b08f0d2ab22c7ffd666c6 /src/Erebos/TextFormat.hs
parent00fb858401afbac6a0b90ba0540a24939cabc5e2 (diff)
Terminal: use FormattedText in printLine
Diffstat (limited to 'src/Erebos/TextFormat.hs')
-rw-r--r--src/Erebos/TextFormat.hs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/Erebos/TextFormat.hs b/src/Erebos/TextFormat.hs
index 88fe0c2..6674ebc 100644
--- a/src/Erebos/TextFormat.hs
+++ b/src/Erebos/TextFormat.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE OverloadedStrings #-}
+
module Erebos.TextFormat (
FormattedText,
plainText,
@@ -8,8 +10,11 @@ module Erebos.TextFormat (
Color(..),
setForegroundColor, setBackgroundColor,
+ endWithNewline,
+
renderPlainText,
formattedTextLength,
+ formattedTextHeight,
) where
import Data.Text (Text)
@@ -35,14 +40,40 @@ setBackgroundColor :: Color -> TextStyle -> TextStyle
setBackgroundColor color (CustomTextColor fg _) = CustomTextColor fg (Just color)
+endWithNewline :: FormattedText -> FormattedText
+endWithNewline = EndWithNewline
+
+
renderPlainText :: FormattedText -> Text
renderPlainText = \case
PlainText text -> text
ConcatenatedText ftexts -> mconcat $ map renderPlainText ftexts
FormattedText _ ftext -> renderPlainText ftext
+ EndWithNewline ftext -> let res = renderPlainText ftext
+ in case T.unsnoc res of
+ Just ( _, '\n') -> res
+ _ -> res <> "\n"
formattedTextLength :: FormattedText -> Int
formattedTextLength = \case
PlainText text -> T.length text
ConcatenatedText ftexts -> sum $ map formattedTextLength ftexts
FormattedText _ ftext -> formattedTextLength ftext
+ EndWithNewline ftext -> formattedTextLength ftext
+
+formattedTextHeight :: FormattedText -> Int
+formattedTextHeight = countLines . collectParts
+ where
+ collectParts = \case
+ PlainText text -> [ text ]
+ ConcatenatedText ftexts -> concatMap collectParts ftexts
+ FormattedText _ ftext -> collectParts ftext
+ EndWithNewline ftext -> collectParts ftext
+ countLines (t : ts)
+ | T.null t = countLines ts
+ | otherwise = 1 + countLines (dropLine (t : ts))
+ countLines [] = 0
+ dropLine (t : ts)
+ | Just ( '\n', t' ) <- T.uncons (T.dropWhile (/= '\n') t) = t' : ts
+ | otherwise = dropLine ts
+ dropLine [] = []