on 4e45d7c help sign in
vty/src/Graphics/Vty/Output.hs
142 lines 6.5 kB history raw
1-- | Output interface.
2--
3-- Access to the current terminal or a specific terminal device.
4--
5-- See also:
6--
7-- 1. "Graphics.Vty.Output": This instantiates an abtract interface to the terminal interface based
8-- on the TERM and COLORTERM environment variables.
9--
10-- 2. Graphics.Vty.Output.Interface: Defines the generic interface all terminals need to implement.
11--
12-- 3. Graphics.Vty.Output.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.
15--
16-- 4. Graphics.Vty.Output.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
21module Graphics.Vty.Output ( module Graphics.Vty.Output
22 , Output(..) -- \todo hide constructors
23 , AssumedState(..)
24 , DisplayContext(..) -- \todo hide constructors
25 , output_picture
26 , display_context
27 )
28 where
31import Graphics.Vty.Prelude
33import Graphics.Vty.Output.Interface
34import Graphics.Vty.Output.MacOSX as MacOSX
35import Graphics.Vty.Output.XTermColor as XTermColor
36import Graphics.Vty.Output.TerminfoBased as TerminfoBased
38import Control.Exception ( SomeException, try )
39import Control.Monad.Trans
41import Data.List ( isPrefixOf )
43import GHC.IO.Handle
45import System.Environment
46import System.IO
48-- | Returns a `Output` for the current terminal.
49--
50-- The specific Output implementation used is hidden from the API user. All terminal
51-- implementations are assumed to perform more, or less, the same. Currently all implementations use
52-- terminfo for at least some terminal specific information. This is why platforms without terminfo
53-- are not supported. However, as mentioned before, any specifics about it being based on terminfo
54-- are hidden from the API user. If a terminal implementation is developed for a terminal for a
55-- platform without terminfo support then Vty should work as expected on that terminal.
56--
57-- Selection of a terminal is done as follows:
58--
59-- * If TERM == xterm
60-- then the terminal might be one of the Mac OS X .app terminals. Check if that might be
61-- the case and use MacOSX if so.
62-- otherwise use XTermColor.
63--
64-- * for any other TERM value TerminfoBased is used.
65--
66--
67-- The terminal has to be determined dynamically at runtime. To satisfy this requirement all
68-- terminals instances are lifted into an abstract terminal handle via existential qualification.
69-- This implies that the only equations that can used are those in the terminal class.
70--
71-- To differentiate between Mac OS X terminals this uses the TERM_PROGRAM environment variable.
72-- However, an xterm started by Terminal or iTerm *also* has TERM_PROGRAM defined since the
73-- environment variable is not reset/cleared by xterm. However a Terminal.app or iTerm.app started
74-- from an xterm under X11 on mac os x will likely be done via open. Since this does not propogate
75-- environment variables (I think?) this assumes that XTERM_VERSION will never be set for a true
76-- Terminal.app or iTerm.app session.
77--
78--
79-- The file descriptor used for output will a duplicate of the current stdout file descriptor.
80--
81-- todo: add an implementation for windows that does not depend on terminfo. Should be installable
82-- with only what is provided in the haskell platform. Use ansi-terminal
83current_terminal :: ( Applicative m, MonadIO m ) => m Output
84current_terminal = do
85 term_type <- liftIO $ getEnv "TERM"
86 out_handle <- liftIO $ hDuplicate stdout
87 terminal_with_name_and_io term_type out_handle
89terminal_with_name_and_io :: (Applicative m, MonadIO m) => String -> Handle -> m Output
90terminal_with_name_and_io term_type out_handle = do
91 t <- if "xterm" `isPrefixOf` term_type
92 then do
93 maybe_terminal_app <- get_env "TERM_PROGRAM"
94 case maybe_terminal_app of
95 Nothing
96 -> XTermColor.reserve_terminal term_type out_handle
97 Just v | v == "Apple_Terminal" || v == "iTerm.app"
98 -> do
99 maybe_xterm <- get_env "XTERM_VERSION"
100 case maybe_xterm of
101 Nothing -> MacOSX.reserve_terminal v out_handle
102 Just _ -> XTermColor.reserve_terminal term_type out_handle
103 -- Assume any other terminal that sets TERM_PROGRAM to not be an OS X terminal.app
104 -- like terminal?
105 _ -> XTermColor.reserve_terminal term_type out_handle
106 -- Not an xterm-like terminal. try for generic terminfo.
107 else TerminfoBased.reserve_terminal term_type out_handle
108 return t
109 where
110 get_env var = do
111 mv <- liftIO $ try $ getEnv var
112 case mv of
113 Left (_e :: SomeException) -> return $ Nothing
114 Right v -> return $ Just v
116-- | Sets the cursor position to the given output column and row.
117--
118-- This is not necessarially the same as the character position with the same coordinates.
119-- Characters can be a variable number of columns in width.
120--
121-- Currently, the only way to set the cursor position to a given character coordinate is to specify
122-- the coordinate in the Picture instance provided to output_picture or refresh.
123set_cursor_pos :: MonadIO m => Output -> Int -> Int -> m ()
124set_cursor_pos t x y = do
125 bounds <- display_bounds t
126 when (x >= 0 && x < region_width bounds && y >= 0 && y < region_height bounds) $ do
127 dc <- display_context t bounds
128 liftIO $ send_to_terminal t (move_cursor_required_bytes dc x y) (serialize_move_cursor dc x y)
130-- | Hides the cursor
131hide_cursor :: MonadIO m => Output -> m ()
132hide_cursor t = do
133 bounds <- display_bounds t
134 dc <- display_context t bounds
135 liftIO $ send_to_terminal t (hide_cursor_required_bytes dc) (serialize_hide_cursor dc)
137-- | Shows the cursor
138show_cursor :: MonadIO m => Output -> m ()
139show_cursor t = do
140 bounds <- display_bounds t
141 dc <- display_context t bounds
142 liftIO $ send_to_terminal t (show_cursor_required_bytes dc) (serialize_show_cursor dc)