@@ -1,3 +1,4 @@
1+{-# LANGUAGE BangPatterns #-}
12module Graphics.Vty.AttributeChange ( module Graphics.Vty.AttributeChange
23 )
34 where
@@ -8,18 +9,63 @@ import Graphics.Vty.Terminal.Generic
89
910import Control.Monad.State.Strict
1011
12+import Data.Bits ( (.&.), complement )
13+import Data.Monoid ( mappend )
14+
1115type AttrChange v = State DisplayAttrDiff v
1216
1317back_color :: Color -> AttrChange ()
14−back_color c = modify $ \ChangeState cs -> cs `mappend` [ DisplayAttrDiff [] NoColorChange ( SetTo c ) ]
18+back_color c = modify $ flip mappend ( DisplayAttrDiff [] NoColorChange ( SetColor c ) )
19+
20+fore_color :: Color -> AttrChange ()
21+fore_color c = modify $ flip mappend ( DisplayAttrDiff [] ( SetColor c ) NoColorChange )
22+
23+apply_style :: Style -> AttrChange ()
24+apply_style s = modify $ flip mappend ( DisplayAttrDiff ( diff_for_apply_styles s )
25+ NoColorChange
26+ NoColorChange
27+ )
1528
16−style :: Style -> AttrChange ()
17−style s = modify $ \ChangeState cs -> cs `mappend` [ DisplayAttrDiff NoColorChange NoColorChange ]
29+remove_style :: Style -> AttrChange ()
30+remove_style s = modify $ flip mappend ( DisplayAttrDiff ( diff_for_remove_styles s )
31+ NoColorChange
32+ NoColorChange
33+ )
1834
19−diff_styles :: Style -> Style -> [StyleStateChange]
35+diff_for_apply_styles :: Style -> [ StyleStateChange ]
36+diff_for_apply_styles !s
37+ | s == 0 = [ ]
38+ | ( s .&. standout ) /= 0 = ApplyStandout : ( diff_for_apply_styles $! s .&. ( complement standout ) )
39+ | ( s .&. underline ) /= 0 = ApplyUnderline : ( diff_for_apply_styles $! s .&. ( complement underline ) )
40+ | ( s .&. reverse_video ) /= 0 = ApplyReverseVideo : ( diff_for_apply_styles $! s .&. ( complement reverse_video ) )
41+ | ( s .&. blink ) /= 0 = ApplyBlink : ( diff_for_apply_styles $! s .&. ( complement blink ) )
42+ | ( s .&. dim ) /= 0 = ApplyDim : ( diff_for_apply_styles $! s .&. ( complement dim ) )
43+ | ( s .&. bold ) /= 0 = ApplyBold : ( diff_for_apply_styles $! s .&. ( complement bold ) )
44+ | otherwise = error "diff_for_apply_styles: impossible style mask"
45+
46+diff_for_remove_styles :: Style -> [ StyleStateChange ]
47+diff_for_remove_styles !s
48+ | s == 0 = [ ]
49+ | ( s .&. standout ) /= 0 = RemoveStandout : ( diff_for_remove_styles $! s .&. ( complement standout ) )
50+ | ( s .&. underline ) /= 0 = RemoveUnderline : ( diff_for_remove_styles $! s .&. ( complement underline ) )
51+ | ( s .&. reverse_video ) /= 0 = RemoveReverseVideo : ( diff_for_remove_styles $! s .&. ( complement reverse_video ) )
52+ | ( s .&. blink ) /= 0 = RemoveBlink : ( diff_for_remove_styles $! s .&. ( complement blink ) )
53+ | ( s .&. dim ) /= 0 = RemoveDim : ( diff_for_remove_styles $! s .&. ( complement dim ) )
54+ | ( s .&. bold ) /= 0 = RemoveBold : ( diff_for_remove_styles $! s .&. ( complement bold ) )
55+ | otherwise = error "diff_for_remove_styles: impossible style mask"
2056
2157default_all :: AttrChange ()
22−default_all = modify $ \ChangeState cs -> cs `mappend` [ DisplayAttrDiff remove_all_styles ColorToDefault ColorToDefault ]
58+default_all = modify $ flip mappend ( DisplayAttrDiff remove_all_styles ColorToDefault ColorToDefault )
59+
60+remove_all_styles :: [ StyleStateChange ]
61+remove_all_styles =
62+ [ RemoveStandout
63+ , RemoveUnderline
64+ , RemoveReverseVideo
65+ , RemoveBlink
66+ , RemoveDim
67+ , RemoveBold
68+ ]
2369
2470put_attr_change :: TerminalHandle -> AttrChange () -> IO ()
2571put_attr_change t c = do