1 module Language.Symantic.Document.Term.Dimension
2 ( module Language.Symantic.Document.Sym
3 , module Language.Symantic.Document.Term.Dimension
6 import Control.Applicative (Applicative(..))
8 import Data.Eq (Eq(..))
9 import Data.Function (($), (.), id)
10 import Data.Maybe (Maybe(..))
11 import Data.Monoid (Monoid(..))
12 import Data.Ord (Ord(..))
13 import Data.Semigroup (Semigroup(..))
14 import Data.String (IsString(..))
15 import GHC.Exts (IsList(..))
16 import Prelude ((+), pred)
17 import Text.Show (Show(..))
19 import Language.Symantic.Document.Sym
24 { dim_width :: Nat -- ^ Maximun line length.
25 , dim_height :: Nat -- ^ Number of newlines.
26 , dim_width_first :: Nat -- ^ Nat of the first line.
27 , dim_width_last :: Nat -- ^ Nat of the last line.
29 instance Semigroup Dim where
30 Dim{dim_width=wx, dim_height=hx, dim_width_first=wfx, dim_width_last=wlx} <>
31 Dim{dim_width=wy, dim_height=hy, dim_width_first=wfy, dim_width_last=wly} =
34 (0, 0) -> let w = wx + wy in Dim w h w w
35 (0, _) -> let v = wfx + wfy in Dim (max v (wx + wy)) h v wly
36 (_, 0) -> let v = wlx + wfy in Dim (max v (wx + wy)) h wfx v
37 _ -> Dim (max wx wy) h wfx wly
38 instance Monoid Dim where
45 { reader_indent :: !Indent -- ^ Current indentation level, used by 'newline'.
46 , reader_newline :: Dimension -- ^ How to display 'newline'.
47 , reader_breakable :: !(Maybe Column) -- ^ 'Column' after which to break, or 'Nothing'
48 , reader_colorable :: !Bool -- ^ Whether colors are activated or not.
49 , reader_decorable :: !Bool -- ^ Whether decorations are activated or not.
52 -- | Default 'Reader'.
56 , reader_newline = newlineWithIndent
57 , reader_breakable = Just $ Nat 80
58 , reader_colorable = True
59 , reader_decorable = True
71 { unDimension :: Reader ->
73 (State -> Dim -> Dim) -> -- normal continuation
74 (State -> Dim -> Dim) -> -- should-break continuation
77 dim :: Dimension -> Dim
78 dim (Dimension p) = p defReader defState oko oko
81 instance IsList Dimension where
82 type Item Dimension = Dimension
85 instance Semigroup Dimension where
86 x <> y = Dimension $ \ro st ok ko ->
88 (\sx tx -> unDimension y ro sx
89 (\sy ty -> ok sy (tx<>ty))
90 (\sy ty -> ko sy (tx<>ty)))
91 (\sx tx -> unDimension y ro sx
92 (\sy ty -> ko sy (tx<>ty))
93 (\sy ty -> ko sy (tx<>ty)))
94 instance Monoid Dimension where
97 instance IsString Dimension where
100 writeH :: Column -> Dimension
102 Dimension $ \ro col ok ko ->
103 let newCol = col + len in
104 (case reader_breakable ro of
105 Just breakCol | breakCol < newCol -> ko
110 , dim_width_last = newCol
111 , dim_width_first = newCol
114 instance Textable Dimension where
115 empty = Dimension $ \_ro st ok _ko -> ok st mempty
117 stringH = writeH . length
118 textH = writeH . length
119 ltextH = writeH . length
121 integer = stringH . show
122 replicate cnt p | cnt <= 0 = empty
123 | otherwise = p <> replicate (pred cnt) p
124 newline = Dimension $ \ro -> unDimension (reader_newline ro) ro
125 instance Indentable Dimension where
126 align p = Dimension $ \ro st -> unDimension p ro{reader_indent=st} st
127 withNewline nl p = Dimension $ \ro -> unDimension p ro{reader_newline=nl}
128 withIndent ind p = Dimension $ \ro -> unDimension p ro{reader_indent=ind}
129 incrIndent ind p = Dimension $ \ro -> unDimension p ro{reader_indent=reader_indent ro + ind}
130 column f = Dimension $ \ro st -> unDimension (f st) ro st
131 indent f = Dimension $ \ro -> unDimension (f (reader_indent ro)) ro
132 newlineWithoutIndent = Dimension $ \_ro _st ok _ko ->
136 , dim_width_first = 0
139 newlineWithIndent = Dimension $ \ro _st ok _ko ->
140 let ind = reader_indent ro in
144 , dim_width_first = 0
145 , dim_width_last = ind
148 instance Breakable Dimension where
149 breakable f = Dimension $ \ro -> unDimension (f (reader_breakable ro)) ro
150 withBreakable col p = Dimension $ \ro -> unDimension p ro{reader_breakable=col}
151 ifBreak y x = Dimension $ \ro st ok ko ->
152 unDimension x ro st ok $
153 case reader_breakable ro of
155 Just{} -> (\_sx _tx -> unDimension y ro st ok ko)
156 breakpoint onNoBreak onBreak t = Dimension $ \ro st ok ko ->
157 case reader_breakable ro of
158 Nothing -> unDimension t ro st ok ko
160 unDimension (onNoBreak <> t) ro st ok
161 (\_sp _tp -> unDimension (onBreak <> t) ro st ok ko)
162 instance Colorable Dimension where
163 colorable f = Dimension $ \ro -> unDimension (f (reader_colorable ro)) ro
164 withColorable b t = Dimension $ \ro -> unDimension t ro{reader_colorable=b}
198 instance Decorable Dimension where
199 decorable f = Dimension $ \ro -> unDimension (f (reader_decorable ro)) ro
200 withDecorable b t = Dimension $ \ro -> unDimension t ro{reader_decorable=b}