summaryrefslogtreecommitdiff
path: root/src/Erebos/TextFormat.hs
blob: 88fe0c2ed76aef70af1d9b21a3effeb2aaa8b11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Erebos.TextFormat (
    FormattedText,
    plainText,

    TextStyle,
    withStyle, noStyle,

    Color(..),
    setForegroundColor, setBackgroundColor,

    renderPlainText,
    formattedTextLength,
) where

import Data.Text (Text)
import Data.Text qualified as T

import Erebos.TextFormat.Types


plainText :: Text -> FormattedText
plainText = PlainText


withStyle :: TextStyle -> FormattedText -> FormattedText
withStyle = FormattedText

noStyle :: TextStyle
noStyle = CustomTextColor Nothing Nothing

setForegroundColor :: Color -> TextStyle -> TextStyle
setForegroundColor color (CustomTextColor _ bg) = CustomTextColor (Just color) bg

setBackgroundColor :: Color -> TextStyle -> TextStyle
setBackgroundColor color (CustomTextColor fg _) = CustomTextColor fg (Just color)


renderPlainText :: FormattedText -> Text
renderPlainText = \case
    PlainText text -> text
    ConcatenatedText ftexts -> mconcat $ map renderPlainText ftexts
    FormattedText _ ftext -> renderPlainText ftext

formattedTextLength :: FormattedText -> Int
formattedTextLength = \case
    PlainText text -> T.length text
    ConcatenatedText ftexts -> sum $ map formattedTextLength ftexts
    FormattedText _ ftext -> formattedTextLength ftext