1 module Language.Symantic.Document.Term
2 ( module Language.Symantic.Document.Sym
3 , module Language.Symantic.Document.Term
6 import Control.Applicative (Applicative(..))
8 import Data.Function (($), (.), id)
9 import Data.Monoid (Monoid(..))
10 import Data.Ord (Ord(..))
11 import Data.Semigroup (Semigroup(..))
12 import Data.String (IsString(..))
13 import GHC.Exts (IsList(..))
14 import Prelude (pred, fromIntegral, Num(..))
15 import System.Console.ANSI
16 import Text.Show (Show(..))
17 import qualified Data.List as List
18 import qualified Data.Text.Lazy as TL
19 import qualified Data.Text.Lazy.Builder as TLB
21 import Language.Symantic.Document.Sym
26 { reader_indent :: !Indent -- ^ Current indentation level, used by 'newline'.
27 , reader_newline :: Term -- ^ How to display 'newline'.
28 , reader_wrap_column :: !Column -- ^ 'Column' after which 'wrap' breaks on a 'breakpoint' or 'breakspace'.
29 , reader_sgr :: ![SGR] -- ^ Active ANSI codes.
30 , reader_colorable :: !Bool -- ^ Whether colors are activated or not.
31 , reader_decorable :: !Bool -- ^ Whether decorations are activated or not.
34 -- | Default 'Reader'.
38 , reader_newline = newlineWithIndent
39 , reader_wrap_column = Nat 80
41 , reader_colorable = True
42 , reader_decorable = True
57 (State -> TLB.Builder -> TLB.Builder) -> -- normal continuation
58 (State -> TLB.Builder -> TLB.Builder) -> -- should-wrap continuation
61 -- | Render a 'Term' into a 'TL.Text'.
62 textTerm :: Term -> TL.Text
63 textTerm = TLB.toLazyText . buildTerm
65 -- | Render a 'Term' into a 'TLB.Builder'.
66 buildTerm :: Term -> TLB.Builder
67 buildTerm (Term t) = t defReader defState oko oko
70 instance IsList Term where
74 instance Semigroup Term where
75 x <> y = Term $ \ro st ok ko ->
77 (\sx tx -> unTerm y ro sx
78 (\sy ty -> ok sy (tx<>ty))
79 (\sy ty -> ko sy (tx<>ty)))
80 (\sx tx -> unTerm y ro sx
81 (\sy ty -> ko sy (tx<>ty))
82 (\sy ty -> ko sy (tx<>ty)))
83 instance Monoid Term where
86 instance IsString Term where
89 writeH :: Column -> TLB.Builder -> Term
91 Term $ \ro st ok ko ->
92 let newCol = st + len in
93 (if newCol <= reader_wrap_column ro then ok else ko)
96 instance Textable Term where
97 empty = Term $ \_ro st ok _ko -> ok st mempty
98 charH t = writeH (Nat 1) (TLB.singleton t)
99 stringH t = writeH (length t) (fromString t)
100 textH t = writeH (length t) (TLB.fromText t)
101 ltextH t = writeH (length t) (TLB.fromLazyText t)
103 integer = stringH . show
104 replicate cnt t | cnt <= 0 = empty
105 | otherwise = t <> replicate (pred cnt) t
106 newline = Term $ \ro -> unTerm (reader_newline ro) ro
107 instance Alignable Term where
108 align t = Term $ \ro st -> unTerm t ro{reader_indent=st} st
109 withNewline nl t = Term $ \ro -> unTerm t ro{reader_newline=nl}
110 withIndent ind t = Term $ \ro -> unTerm t ro{reader_indent=ind}
111 incrIndent ind t = Term $ \ro -> unTerm t ro{reader_indent=reader_indent ro + ind}
112 column f = Term $ \ro st -> unTerm (f st) ro st
113 newlineWithoutIndent = Term $ \_ro _st ok _ko ->
114 ok 0 $ TLB.singleton '\n'
115 newlineWithIndent = Term $ \ro _st ok _ko ->
116 ok (reader_indent ro) $
117 TLB.singleton '\n' <>
118 fromString (List.replicate (fromIntegral $ reader_indent ro) ' ')
119 instance Wrapable Term where
120 ifWrap y x = Term $ \ro st ok ko ->
121 unTerm x ro st ok (\_sx _tx -> unTerm y ro st ok ko)
122 breakpoint onNoBreak onBreak t = Term $ \ro st ok ko ->
123 unTerm (onNoBreak <> t) ro st ok
124 (\_sp _tp -> unTerm (onBreak <> t) ro st ok ko)
125 withWrapColumn col t = Term $ \ro -> unTerm t ro{reader_wrap_column=col}
127 writeSGR :: (Reader -> Bool) -> SGR -> Term -> Term
128 writeSGR isOn s (Term t) =
131 then unTerm (o <> m <> c) ro
134 o = Term $ \_ro st ok _ko -> ok st $ fromString $ setSGRCode [s]
135 m = Term $ \ro -> t ro{reader_sgr=s:reader_sgr ro}
136 c = Term $ \ro st ok _ko -> ok st $ fromString $ setSGRCode $ Reset:List.reverse (reader_sgr ro)
138 instance Colorable Term where
139 colorable f = Term $ \ro -> unTerm (f (reader_colorable ro)) ro
140 withColorable b t = Term $ \ro -> unTerm t ro{reader_colorable=b}
141 reverse = writeSGR reader_colorable $ SetSwapForegroundBackground True
142 black = writeSGR reader_colorable $ SetColor Foreground Dull Black
143 red = writeSGR reader_colorable $ SetColor Foreground Dull Red
144 green = writeSGR reader_colorable $ SetColor Foreground Dull Green
145 yellow = writeSGR reader_colorable $ SetColor Foreground Dull Yellow
146 blue = writeSGR reader_colorable $ SetColor Foreground Dull Blue
147 magenta = writeSGR reader_colorable $ SetColor Foreground Dull Magenta
148 cyan = writeSGR reader_colorable $ SetColor Foreground Dull Cyan
149 white = writeSGR reader_colorable $ SetColor Foreground Dull White
150 blacker = writeSGR reader_colorable $ SetColor Foreground Vivid Black
151 redder = writeSGR reader_colorable $ SetColor Foreground Vivid Red
152 greener = writeSGR reader_colorable $ SetColor Foreground Vivid Green
153 yellower = writeSGR reader_colorable $ SetColor Foreground Vivid Yellow
154 bluer = writeSGR reader_colorable $ SetColor Foreground Vivid Blue
155 magentaer = writeSGR reader_colorable $ SetColor Foreground Vivid Magenta
156 cyaner = writeSGR reader_colorable $ SetColor Foreground Vivid Cyan
157 whiter = writeSGR reader_colorable $ SetColor Foreground Vivid White
158 onBlack = writeSGR reader_colorable $ SetColor Background Dull Black
159 onRed = writeSGR reader_colorable $ SetColor Background Dull Red
160 onGreen = writeSGR reader_colorable $ SetColor Background Dull Green
161 onYellow = writeSGR reader_colorable $ SetColor Background Dull Yellow
162 onBlue = writeSGR reader_colorable $ SetColor Background Dull Blue
163 onMagenta = writeSGR reader_colorable $ SetColor Background Dull Magenta
164 onCyan = writeSGR reader_colorable $ SetColor Background Dull Cyan
165 onWhite = writeSGR reader_colorable $ SetColor Background Dull White
166 onBlacker = writeSGR reader_colorable $ SetColor Background Vivid Black
167 onRedder = writeSGR reader_colorable $ SetColor Background Vivid Red
168 onGreener = writeSGR reader_colorable $ SetColor Background Vivid Green
169 onYellower = writeSGR reader_colorable $ SetColor Background Vivid Yellow
170 onBluer = writeSGR reader_colorable $ SetColor Background Vivid Blue
171 onMagentaer = writeSGR reader_colorable $ SetColor Background Vivid Magenta
172 onCyaner = writeSGR reader_colorable $ SetColor Background Vivid Cyan
173 onWhiter = writeSGR reader_colorable $ SetColor Background Vivid White
174 instance Decorable Term where
175 decorable f = Term $ \ro -> unTerm (f (reader_decorable ro)) ro
176 withDecorable b t = Term $ \ro -> unTerm t ro{reader_decorable=b}
177 bold = writeSGR reader_decorable $ SetConsoleIntensity BoldIntensity
178 underline = writeSGR reader_decorable $ SetUnderlining SingleUnderline
179 italic = writeSGR reader_decorable $ SetItalicized True