on bd0f87a help sign in
vty/src/Graphics/Vty/DisplayAttributes.hs
104 lines 3.8 kB history raw
1{-# LANGUAGE BangPatterns #-}
2module Graphics.Vty.DisplayAttributes
3 where
5import Graphics.Vty.Attributes
7import Data.Bits ( (.&.) )
8import Data.Monoid ( Monoid(..), mconcat )
10-- | Given the previously applied display attributes as a FixedAttr and the current display
11-- attributes as an Attr produces a FixedAttr that represents the current display attributes. This
12-- is done by using the previously applied display attributes to remove the "KeepCurrent"
13-- abstraction.
14fix_display_attr :: FixedAttr -> Attr -> FixedAttr
15fix_display_attr fattr attr
16 = FixedAttr ( fix_style (fixed_style fattr) (attr_style attr) )
17 ( fix_color (fixed_fore_color fattr) (attr_fore_color attr) )
18 ( fix_color (fixed_back_color fattr) (attr_back_color attr) )
19 where
20 fix_style _s Default = default_style_mask
21 fix_style s KeepCurrent = s
22 fix_style _s (SetTo new_style) = new_style
23 fix_color _c Default = Nothing
24 fix_color c KeepCurrent = c
25 fix_color _c (SetTo c) = Just c
27data DisplayAttrDiff = DisplayAttrDiff
28 { style_diffs :: [ StyleStateChange ]
29 , fore_color_diff :: DisplayColorDiff
30 , back_color_diff :: DisplayColorDiff
31 }
33instance Monoid DisplayAttrDiff where
34 mempty = DisplayAttrDiff [] NoColorChange NoColorChange
35 mappend d_0 d_1 =
36 let ds = simplify_style_diffs ( style_diffs d_0 ) ( style_diffs d_1 )
37 fcd = simplify_color_diffs ( fore_color_diff d_0 ) ( fore_color_diff d_1 )
38 bcd = simplify_color_diffs ( back_color_diff d_0 ) ( back_color_diff d_1 )
39 in DisplayAttrDiff ds fcd bcd
41simplify_style_diffs :: [ StyleStateChange ] -> [ StyleStateChange ] -> [ StyleStateChange ]
42simplify_style_diffs cs_0 cs_1 = cs_0 `mappend` cs_1
44simplify_color_diffs _cd ColorToDefault = ColorToDefault
45simplify_color_diffs cd NoColorChange = cd
46simplify_color_diffs _cd ( SetColor !c ) = SetColor c
48data DisplayColorDiff
49 = ColorToDefault
50 | NoColorChange
51 | SetColor !Color
52 deriving Eq
54data StyleStateChange
55 = ApplyStandout
56 | RemoveStandout
57 | ApplyUnderline
58 | RemoveUnderline
59 | ApplyReverseVideo
60 | RemoveReverseVideo
61 | ApplyBlink
62 | RemoveBlink
63 | ApplyDim
64 | RemoveDim
65 | ApplyBold
66 | RemoveBold
68display_attr_diffs :: FixedAttr -> FixedAttr -> DisplayAttrDiff
69display_attr_diffs attr attr' = DisplayAttrDiff
70 { style_diffs = diff_styles ( fixed_style attr ) ( fixed_style attr' )
71 , fore_color_diff = diff_color ( fixed_fore_color attr ) ( fixed_fore_color attr' )
72 , back_color_diff = diff_color ( fixed_back_color attr ) ( fixed_back_color attr' )
73 }
75diff_color :: Maybe Color -> Maybe Color -> DisplayColorDiff
76diff_color Nothing (Just c') = SetColor c'
77diff_color (Just c) (Just c')
78 | c == c' = NoColorChange
79 | otherwise = SetColor c'
80diff_color Nothing Nothing = NoColorChange
81diff_color (Just _) Nothing = ColorToDefault
83diff_styles :: Style -> Style -> [StyleStateChange]
84diff_styles prev cur
85 = mconcat
86 [ style_diff standout ApplyStandout RemoveStandout
87 , style_diff underline ApplyUnderline RemoveUnderline
88 , style_diff reverse_video ApplyReverseVideo RemoveReverseVideo
89 , style_diff blink ApplyBlink RemoveBlink
90 , style_diff dim ApplyDim RemoveDim
91 , style_diff bold ApplyBold RemoveBold
92 ]
93 where
94 style_diff s sm rm
95 = case ( 0 == prev .&. s, 0 == cur .&. s ) of
96 -- not set in either
97 ( True, True ) -> []
98 -- set in both
99 ( False, False ) -> []
100 -- now set
101 ( True, False) -> [ sm ]
102 -- now unset
103 ( False, True) -> [ rm ]