@@ -1,3 +1,25 @@
1+-- | The inline module provides a limited interface to changing the style of terminal output. The
2+-- intention is for this interface to be used inline with other output systems.
4+-- The changes specified by the InlineM monad are applied to the terminals display attributes. These
5+-- display attributes effect the display of all following text output to the terminal file
8+-- For example, in an IO monad the following code with print the text \"Not styled. \" Followed by the
9+-- text \" Styled! \" drawn over a red background and underlined.
12+-- t <- terminal_handle
13+-- putStr \"Not styled. \"
14+-- put_attr_change t $ do
16+-- apply_style underline
17+-- putStr \" Styled! \"
18+-- put_attr_change t $ default_all
19+-- putStrLn \"Not styled.\"
1-- Copyright 2009-2010 Corey O'Connor
23-- Copyright 2009-2010 Corey O'Connor
2{-# LANGUAGE BangPatterns #-}
24{-# LANGUAGE BangPatterns #-}
3module Graphics.Vty.Inline ( module Graphics.Vty.Inline
25module Graphics.Vty.Inline ( module Graphics.Vty.Inline
@@ -20,15 +42,24 @@ import System.IO
21type InlineM v = State Attr v
43type InlineM v = State Attr v
45+-- | Set the background color to the provided 'Color'
23back_color :: Color -> InlineM ()
46back_color :: Color -> InlineM ()
24back_color c = modify $ flip mappend ( current_attr `with_back_color` c )
47back_color c = modify $ flip mappend ( current_attr `with_back_color` c )
49+-- | Set the foreground color to the provided 'Color'
26fore_color :: Color -> InlineM ()
50fore_color :: Color -> InlineM ()
27fore_color c = modify $ flip mappend ( current_attr `with_fore_color` c )
51fore_color c = modify $ flip mappend ( current_attr `with_fore_color` c )
53+-- | Attempt to change the 'Style' of the following text.
55+-- If the terminal does not support the style change no error is produced. The style can still be
29apply_style :: Style -> InlineM ()
57apply_style :: Style -> InlineM ()
30apply_style s = modify $ flip mappend ( current_attr `with_style` s )
58apply_style s = modify $ flip mappend ( current_attr `with_style` s )
60+-- | Attempt to remove the specified 'Style' from the display of the following text.
62+-- This will fail if apply_style for the given style has not been previously called.
32remove_style :: Style -> InlineM ()
63remove_style :: Style -> InlineM ()
33remove_style s_mask = modify $ \attr ->
64remove_style s_mask = modify $ \attr ->
34 let style' = case attr_style attr of
65 let style' = case attr_style attr of
@@ -37,9 +68,13 @@ remove_style s_mask = modify $ \attr ->
37 SetTo s -> s .&. complement s_mask
68 SetTo s -> s .&. complement s_mask
38 in attr { attr_style = SetTo style' }
69 in attr { attr_style = SetTo style' }
71+-- | Reset the display attributes
40default_all :: InlineM ()
72default_all :: InlineM ()
41default_all = put def_attr
73default_all = put def_attr
75+-- | Apply the provided display attribute changes to the terminal.
77+-- This also flushes the 'stdout' handle.
43put_attr_change :: ( Applicative m, MonadIO m ) => TerminalHandle -> InlineM () -> m ()
78put_attr_change :: ( Applicative m, MonadIO m ) => TerminalHandle -> InlineM () -> m ()
44put_attr_change t c = do
79put_attr_change t c = do
45 bounds <- display_bounds t
80 bounds <- display_bounds t
@@ -60,10 +95,3 @@ put_attr_change t c = do
60 liftIO $ modifyIORef ( state_ref t ) $ \s -> s { known_fattr = Just fattr' }
95 liftIO $ modifyIORef ( state_ref t ) $ \s -> s { known_fattr = Just fattr' }
61 inline_hack d
96 inline_hack d
63−put_string :: ( Applicative m, MonadIO m ) => TerminalHandle -> String -> m ()
65− let s_utf8 = UTF8.fromString s
66− liftIO $ hFlush stdout
67− liftIO $ marshall_to_terminal t ( utf8_text_required_bytes s_utf8)
68− ( serialize_utf8_text s_utf8 )