From 9e45b6638fb32b0ca20514db8a53c545902c91af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 29 Aug 2026 12:15:10 +0200 Subject: Copy TextFormat modules from erebos project --- src/TextFormat/Types.hs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/TextFormat/Types.hs (limited to 'src/TextFormat/Types.hs') diff --git a/src/TextFormat/Types.hs b/src/TextFormat/Types.hs new file mode 100644 index 0000000..40deabd --- /dev/null +++ b/src/TextFormat/Types.hs @@ -0,0 +1,58 @@ +module TextFormat.Types ( + FormattedText(..), + TextStyle(..), + Color(..), +) where + +import Data.String +import Data.Text (Text) + + +data FormattedText + = PlainText Text + | ConcatenatedText [ FormattedText ] + | FormattedText TextStyle FormattedText + | EndWithNewline FormattedText + +instance IsString FormattedText where + fromString = PlainText . fromString + +instance Semigroup FormattedText where + ConcatenatedText xs <> ConcatenatedText ys = ConcatenatedText (xs ++ ys) + x <> ConcatenatedText ys = ConcatenatedText (x : ys) + ConcatenatedText xs <> y = ConcatenatedText (xs ++ [ y ]) + x <> y = ConcatenatedText [ x, y ] + +instance Monoid FormattedText where + mempty = ConcatenatedText [] + mconcat [] = ConcatenatedText [] + mconcat [ x ] = x + mconcat xs = ConcatenatedText $ concatMap flatten xs + where + flatten (ConcatenatedText ys) = ys + flatten y = [ y ] + + +data TextStyle + = CustomTextColor (Maybe Color) (Maybe Color) + + +data Color + = DefaultColor + | Black + | Red + | Green + | Yellow + | Blue + | Magenta + | Cyan + | White + | BrightBlack + | BrightRed + | BrightGreen + | BrightYellow + | BrightBlue + | BrightMagenta + | BrightCyan + | BrightWhite + deriving (Eq) -- cgit v1.2.3