@@ -12,64 +12,51 @@ import Control.Monad.State.Strict
12import Control.Monad.Trans
12import Control.Monad.Trans
14import Data.Bits ( (.&.), complement )
14import Data.Bits ( (.&.), complement )
15import Data.Monoid ( mappend )
16import Data.Monoid ( mappend )
17−type AttrChange v = State DisplayAttrDiff v
18+type AttrChange v = State Attr v
19back_color :: Color -> AttrChange ()
20back_color :: Color -> AttrChange ()
20−back_color c = modify $ flip mappend ( DisplayAttrDiff [] NoColorChange ( SetColor c ) )
21+back_color c = modify $ flip mappend ( current_attr `with_back_color` c )
22fore_color :: Color -> AttrChange ()
23fore_color :: Color -> AttrChange ()
23−fore_color c = modify $ flip mappend ( DisplayAttrDiff [] ( SetColor c ) NoColorChange )
24+fore_color c = modify $ flip mappend ( current_attr `with_fore_color` c )
25apply_style :: Style -> AttrChange ()
26apply_style :: Style -> AttrChange ()
26−apply_style s = modify $ flip mappend ( DisplayAttrDiff ( diff_for_apply_styles s )
27+apply_style s = modify $ flip mappend ( current_attr `with_style` s )
31remove_style :: Style -> AttrChange ()
29remove_style :: Style -> AttrChange ()
32−remove_style s = modify $ flip mappend ( DisplayAttrDiff ( diff_for_remove_styles s )
30+remove_style s_mask = modify $ \attr ->
33− NoColorChange
31+ let style' = case attr_style attr of
34− NoColorChange
32+ Default -> error $ "Graphics.Vty.Inline: Cannot remove_style if apply_style never used."
35− )
33+ KeepCurrent -> error $ "Graphics.Vty.Inline: Cannot remove_style if apply_style never used."
36−
34+ SetTo s -> s .&. complement s_mask
37−diff_for_apply_styles :: Style -> [ StyleStateChange ]
35+ in attr { attr_style = SetTo style' }
38−diff_for_apply_styles !s
36+
39− | s == 0 = [ ]
37+put_default_attr :: ( Applicative m, MonadIO m ) => TerminalHandle -> m ()
40− | ( s .&. standout ) /= 0 = ApplyStandout : ( diff_for_apply_styles $! s .&. ( complement standout ) )
38+put_default_attr t = do
41− | ( s .&. underline ) /= 0 = ApplyUnderline : ( diff_for_apply_styles $! s .&. ( complement underline ) )
39+ bounds <- display_bounds t
42− | ( s .&. reverse_video ) /= 0 = ApplyReverseVideo : ( diff_for_apply_styles $! s .&. ( complement reverse_video ) )
40+ d <- display_context t bounds
43− | ( s .&. blink ) /= 0 = ApplyBlink : ( diff_for_apply_styles $! s .&. ( complement blink ) )
41+ marshall_to_terminal t ( default_attr_required_bytes d )
44− | ( s .&. dim ) /= 0 = ApplyDim : ( diff_for_apply_styles $! s .&. ( complement dim ) )
42+ ( serialize_default_attr d )
45− | ( s .&. bold ) /= 0 = ApplyBold : ( diff_for_apply_styles $! s .&. ( complement bold ) )
43+ liftIO $ modifyIORef ( state_ref t ) $ \s -> s { known_fattr = Just $ FixedAttr default_style_mask Nothing Nothing }
46− | otherwise = error "diff_for_apply_styles: impossible style mask"
44+
48−diff_for_remove_styles :: Style -> [ StyleStateChange ]
49−diff_for_remove_styles !s
51− | ( s .&. standout ) /= 0 = RemoveStandout : ( diff_for_remove_styles $! s .&. ( complement standout ) )
52− | ( s .&. underline ) /= 0 = RemoveUnderline : ( diff_for_remove_styles $! s .&. ( complement underline ) )
53− | ( s .&. reverse_video ) /= 0 = RemoveReverseVideo : ( diff_for_remove_styles $! s .&. ( complement reverse_video ) )
54− | ( s .&. blink ) /= 0 = RemoveBlink : ( diff_for_remove_styles $! s .&. ( complement blink ) )
55− | ( s .&. dim ) /= 0 = RemoveDim : ( diff_for_remove_styles $! s .&. ( complement dim ) )
56− | ( s .&. bold ) /= 0 = RemoveBold : ( diff_for_remove_styles $! s .&. ( complement bold ) )
57− | otherwise = error "diff_for_remove_styles: impossible style mask"
59−default_all :: AttrChange ()
60−default_all = modify $ flip mappend ( DisplayAttrDiff remove_all_styles ColorToDefault ColorToDefault )
62−remove_all_styles :: [ StyleStateChange ]
72−put_attr_change :: MonadIO m => TerminalHandle -> AttrChange () -> m ()
45+put_attr_change :: ( Applicative m, MonadIO m ) => TerminalHandle -> AttrChange () -> m ()
73put_attr_change t c = do
46put_attr_change t c = do
74− return ()
47+ bounds <- display_bounds t
48+ d <- display_context t bounds
49+ mfattr <- liftIO $ known_fattr <$> readIORef ( state_ref t )
50+ fattr <- case mfattr of
52+ marshall_to_terminal t (default_attr_required_bytes d) (serialize_default_attr d)
53+ return $ FixedAttr default_style_mask Nothing Nothing
55+ let attr = execState c current_attr
56+ attr' = limit_attr_for_display d attr
57+ fattr' = fix_display_attr fattr attr'
58+ diffs = display_attr_diffs fattr fattr'
59+ marshall_to_terminal t ( attr_required_bytes d fattr attr' diffs )
60+ ( serialize_set_attr d fattr attr' diffs )
61+ liftIO $ modifyIORef ( state_ref t ) $ \s -> s { known_fattr = Just fattr' }