1 module Symantic.Document.Term.Dimension
2 ( module Symantic.Document.Sym
3 , module 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(..))
17 import Text.Show (Show(..))
19 import 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 = wx + wfy in Dim (max v wy) h v wly
36 (_, 0) -> let v = wlx + wy in Dim (max v wx) h wfx v
37 _ -> Dim (max wx wy) h wfx wly
38 instance Monoid Dim where
45 { dimInh_indent :: !Indent -- ^ Current indentation level, used by 'newline'.
46 , dimInh_newline :: Dimension -- ^ How to display 'newline'.
47 , dimInh_breakable :: !(Maybe Column) -- ^ 'Column' after which to break, or 'Nothing'
48 , dimInh_colorable :: !Bool -- ^ Whether colors are activated or not.
49 , dimInh_decorable :: !Bool -- ^ Whether decorations are activated or not.
52 -- | Default 'DimInh'.
56 , dimInh_newline = newlineWithIndent
57 , dimInh_breakable = Nothing
58 , dimInh_colorable = True
59 , dimInh_decorable = True
63 type DimState = Column
65 defDimState :: DimState
69 newtype Dimension = Dimension
70 { unDimension :: DimInh -> DimState ->
71 (DimState -> Dim -> Dim) -> -- normal continuation
72 (DimState -> Dim -> Dim) -> -- should-break continuation
75 dim :: Dimension -> Dim
76 dim (Dimension p) = p defDimInh defDimState oko oko
79 instance IsList Dimension where
80 type Item Dimension = Dimension
83 instance Semigroup Dimension where
84 x <> y = Dimension $ \ro st ok ko ->
86 (\sx tx -> unDimension y ro sx
87 (\sy ty -> ok sy (tx<>ty))
88 (\sy ty -> ko sy (tx<>ty)))
89 (\sx tx -> unDimension y ro sx
90 (\sy ty -> ko sy (tx<>ty))
91 (\sy ty -> ko sy (tx<>ty)))
92 instance Monoid Dimension where
95 instance IsString Dimension where
98 writeH :: Column -> Dimension
100 Dimension $ \ro currCol ok ko ->
101 let newCol = currCol + len in
102 (case dimInh_breakable ro of
103 Just breakCol | breakCol < newCol -> ko
108 , dim_width_last = len
109 , dim_width_first = len
112 instance Textable Dimension where
113 empty = Dimension $ \_ro st ok _ko -> ok st mempty
115 stringH = writeH . length
116 textH = writeH . length
117 ltextH = writeH . length
118 newline = Dimension $ \ro -> unDimension (dimInh_newline ro) ro
119 instance Indentable Dimension where
120 align p = Dimension $ \ro st -> unDimension p ro{dimInh_indent=st} st
121 withNewline nl p = Dimension $ \ro -> unDimension p ro{dimInh_newline=nl}
122 withIndent ind p = Dimension $ \ro -> unDimension p ro{dimInh_indent=ind}
123 incrIndent ind p = Dimension $ \ro -> unDimension p ro{dimInh_indent=dimInh_indent ro + ind}
124 column f = Dimension $ \ro st -> unDimension (f st) ro st
125 indent f = Dimension $ \ro -> unDimension (f (dimInh_indent ro)) ro
126 newlineWithoutIndent = Dimension $ \_ro _st ok _ko ->
130 , dim_width_first = 0
133 newlineWithIndent = Dimension $ \ro _st ok _ko ->
134 let ind = dimInh_indent ro in
138 , dim_width_first = 0
139 , dim_width_last = ind
142 instance Breakable Dimension where
143 breakable f = Dimension $ \ro -> unDimension (f (dimInh_breakable ro)) ro
144 withBreakable col p = Dimension $ \ro -> unDimension p ro{dimInh_breakable=col}
145 ifBreak y x = Dimension $ \ro st ok ko ->
146 unDimension x ro st ok $
147 case dimInh_breakable ro of
149 Just{} -> (\_sx _tx -> unDimension y ro st ok ko)
150 breakpoint onNoBreak onBreak t = Dimension $ \ro st ok ko ->
151 unDimension (onNoBreak <> t) ro st ok $
152 case dimInh_breakable ro of
154 Just{} -> (\_sp _tp -> unDimension (onBreak <> t) ro st ok ko)
155 instance Colorable Dimension where
156 colorable f = Dimension $ \ro -> unDimension (f (dimInh_colorable ro)) ro
157 withColorable b t = Dimension $ \ro -> unDimension t ro{dimInh_colorable=b}
191 instance Decorable Dimension where
192 decorable f = Dimension $ \ro -> unDimension (f (dimInh_decorable ro)) ro
193 withDecorable b t = Dimension $ \ro -> unDimension t ro{dimInh_decorable=b}