on master help sign in
more documentation updates
c0faecacoreyoconnor
src/Graphics/Vty/Inline.hs+35−7
@@ -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.
3+--
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
6+-- descriptor.
7+--
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.
10+--
11+-- @
12+-- t <- terminal_handle
13+-- putStr \"Not styled. \"
14+-- put_attr_change t $ do
15+-- back_color red
16+-- apply_style underline
17+-- putStr \" Styled! \"
18+-- put_attr_change t $ default_all
19+-- putStrLn \"Not styled.\"
20+-- release_terminal t
21+-- @
22+--
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
20
42
21type InlineM v = State Attr v
43type InlineM v = State Attr v
22
44
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 )
25
48
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 )
28
52
53+-- | Attempt to change the 'Style' of the following text.
54+--
55+-- If the terminal does not support the style change no error is produced. The style can still be
56+-- removed.
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 )
31
59
60+-- | Attempt to remove the specified 'Style' from the display of the following text.
61+--
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' }
39
70
71+-- | Reset the display attributes
40default_all :: InlineM ()
72default_all :: InlineM ()
41default_all = put def_attr
73default_all = put def_attr
42
74
75+-- | Apply the provided display attribute changes to the terminal.
76+--
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
62
97
63−put_string :: ( Applicative m, MonadIO m ) => TerminalHandle -> String -> m ()
64−put_string t s = do
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 )
69−
src/Graphics/Vty/Terminal.hs+21−12
@@ -1,17 +1,23 @@
1−-- Copyright 2009-2010 Corey O'Connor
1+-- | Generic Terminal interface.
2+--
3+-- Defines the common interface supported by all terminals.
4+--
5+-- See also:
6+--
2−-- * Graphics.Vty.Terminal: This instantiates an abtract interface to the terminal interface
7+-- 1. Graphics.Vty.Terminal: This instantiates an abtract interface to the terminal interface based
3−-- based on the TERM and COLORTERM environment variables.
8+-- on the TERM and COLORTERM environment variables.
9+--
4−-- * Graphics.Vty.Terminal.Generic: Defines the generic interface all terminals need to implement.
10+-- 2. Graphics.Vty.Terminal.Generic: Defines the generic interface all terminals need to implement.
5−-- * Graphics.Vty.Terminal.TerminfoBased: Defines a terminal instance that uses terminfo for all
6−-- control strings.
7−-- - No attempt is made to change the character set to UTF-8 for these terminals. I don't
8−-- know a way to reliably determine if that is required or how to do so.
9−-- * Graphics.Vty.Terminal.XTermColor: This module contains an interface suitable for
10−-- xterm-like terminals. These are the terminals where TERM == xterm. This does use terminfo
11−-- for as many control codes as possible. H: This should derive functionality from the
12−-- TerminfoBased terminal.
13--
11--
12+-- 3. Graphics.Vty.Terminal.TerminfoBased: Defines a terminal instance that uses terminfo for all
13+-- control strings. No attempt is made to change the character set to UTF-8 for these terminals.
14+-- I don't know a way to reliably determine if that is required or how to do so.
14--
15--
16+-- 4. Graphics.Vty.Terminal.XTermColor: This module contains an interface suitable for xterm-like
17+-- terminals. These are the terminals where TERM == xterm. This does use terminfo for as many
18+-- control codes as possible.
19+--
20+-- Copyright 2009-2010 Corey O'Connor
15{-# LANGUAGE ScopedTypeVariables #-}
21{-# LANGUAGE ScopedTypeVariables #-}
16module Graphics.Vty.Terminal ( module Graphics.Vty.Terminal
22module Graphics.Vty.Terminal ( module Graphics.Vty.Terminal
17 , Terminal(..)
23 , Terminal(..)
@@ -67,6 +73,9 @@ import System.Environment
67-- environment variables (I think?) this assumes that XTERM_VERSION will never be set for a true
73-- environment variables (I think?) this assumes that XTERM_VERSION will never be set for a true
68-- Terminal.app or iTerm.app session.
74-- Terminal.app or iTerm.app session.
69--
75--
76+--
77+-- The file descriptor used for output will a duplicate of the current stdout file descriptor.
78+--
70-- todo: add an implementation for windows that does not depend on terminfo. Should be installable
79-- todo: add an implementation for windows that does not depend on terminfo. Should be installable
71-- with only what is provided in the haskell platform.
80-- with only what is provided in the haskell platform.
72--
81--
src/Graphics/Vty/Terminal/TerminfoBased.hs+1−1
@@ -171,7 +171,7 @@ instance Terminal Term where
171 ( w, h ) | w < 0 || h < 0 -> fail $ "getwinsize returned < 0 : " ++ show raw_size
171 ( w, h ) | w < 0 || h < 0 -> fail $ "getwinsize returned < 0 : " ++ show raw_size
172 | otherwise -> return $ DisplayRegion (toEnum w) (toEnum h)
172 | otherwise -> return $ DisplayRegion (toEnum w) (toEnum h)
173
173
174− -- Output the byte buffer of the specified size to the terminal device.
174+ -- | Output the byte buffer of the specified size to the terminal device.
175 output_byte_buffer t out_ptr out_byte_count = do
175 output_byte_buffer t out_ptr out_byte_count = do
176 -- if the out fd is actually the same as stdout's then a
176 -- if the out fd is actually the same as stdout's then a
177 -- flush is required *before* the c_output_byte_buffer call
177 -- flush is required *before* the c_output_byte_buffer call