1-- | Generic Terminal interface.
3-- Defines the common interface supported by all terminals.
7-- 1. Graphics.Vty.Terminal: This instantiates an abtract interface to the terminal interface based
8-- on the TERM and COLORTERM environment variables.
10-- 2. Graphics.Vty.Terminal.Generic: Defines the generic interface all terminals need to implement.
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.
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.
20-- Copyright 2009-2010 Corey O'Connor
21{-# LANGUAGE ScopedTypeVariables #-}
22module Graphics.Vty.Terminal ( module Graphics.Vty.Terminal
32import Graphics.Vty.Terminal.Generic
33import Graphics.Vty.Terminal.MacOSX as MacOSX
34import Graphics.Vty.Terminal.XTermColor as XTermColor
35import Graphics.Vty.Terminal.TerminfoBased as TerminfoBased
37import Control.Applicative
38import Control.Exception ( SomeException, try )
39import Control.Monad.Trans
41import Data.List ( isPrefixOf )
44import System.Environment
46-- | Returns a TerminalHandle (an abstract Terminal instance) for the current terminal.
48-- The specific Terminal implementation used is hidden from the API user. All terminal
49-- implementations are assumed to perform more, or less, the same. Currently all implementations use
50-- terminfo for at least some terminal specific information. This is why platforms without terminfo
51-- are not supported. However, as mentioned before, any specifics about it being based on terminfo
52-- are hidden from the API user. If a terminal implementation is developed for a terminal for a
53-- platform without terminfo support then Vty should work as expected on that terminal.
55-- Selection of a terminal is done as follows:
58-- then the terminal might be one of the Mac OS X .app terminals. Check if that might be
59-- the case and use MacOSX if so.
60-- otherwise use XTermColor.
62-- * for any other TERM value TerminfoBased is used.
65-- The terminal has to be determined dynamically at runtime. To satisfy this requirement all
66-- terminals instances are lifted into an abstract terminal handle via existential qualification.
67-- This implies that the only equations that can used are those in the terminal class.
69-- To differentiate between Mac OS X terminals this uses the TERM_PROGRAM environment variable.
70-- However, an xterm started by Terminal or iTerm *also* has TERM_PROGRAM defined since the
71-- environment variable is not reset/cleared by xterm. However a Terminal.app or iTerm.app started
72-- from an xterm under X11 on mac os x will likely be done via open. Since this does not propogate
73-- environment variables (I think?) this assumes that XTERM_VERSION will never be set for a true
74-- Terminal.app or iTerm.app session.
77-- The file descriptor used for output will a duplicate of the current stdout file descriptor.
79-- todo: add an implementation for windows that does not depend on terminfo. Should be installable
80-- with only what is provided in the haskell platform.
82-- todo: The Terminal interface does not provide any input support.
83terminal_handle :: ( Applicative m, MonadIO m ) => m TerminalHandle
85 term_type <- liftIO $ getEnv "TERM"
86 t <- if "xterm" `isPrefixOf` term_type
88 maybe_terminal_app <- get_env "TERM_PROGRAM"
89 case maybe_terminal_app of
91 -> XTermColor.terminal_instance term_type >>= new_terminal_handle
92 Just v | v == "Apple_Terminal" || v == "iTerm.app"
94 maybe_xterm <- get_env "XTERM_VERSION"
96 Nothing -> MacOSX.terminal_instance v >>= new_terminal_handle
97 Just _ -> XTermColor.terminal_instance term_type >>= new_terminal_handle
98 -- Assume any other terminal that sets TERM_PROGRAM to not be an OS X terminal.app
100 _ -> XTermColor.terminal_instance term_type >>= new_terminal_handle
101 -- Not an xterm-like terminal. try for generic terminfo.
102 else TerminfoBased.terminal_instance term_type >>= new_terminal_handle
106 mv <- liftIO $ try $ getEnv var
108 Left (_e :: SomeException) -> return $ Nothing
109 Right v -> return $ Just v
111-- | Sets the cursor position to the given output column and row.
113-- This is not necessarially the same as the character position with the same coordinates.
114-- Characters can be a variable number of columns in width.
116-- Currently, the only way to set the cursor position to a given character coordinate is to specify
117-- the coordinate in the Picture instance provided to output_picture or refresh.
118set_cursor_pos :: MonadIO m => TerminalHandle -> Word -> Word -> m ()
119set_cursor_pos t x y = do
120 bounds <- display_bounds t
121 d <- display_context t bounds
122 liftIO $ marshall_to_terminal t (move_cursor_required_bytes d x y) (serialize_move_cursor d x y)
125hide_cursor :: MonadIO m => TerminalHandle -> m ()
127 bounds <- display_bounds t
128 d <- display_context t bounds
129 liftIO $ marshall_to_terminal t (hide_cursor_required_bytes d) (serialize_hide_cursor d)
132show_cursor :: MonadIO m => TerminalHandle -> m ()
134 bounds <- display_bounds t
135 d <- display_context t bounds
136 liftIO $ marshall_to_terminal t (show_cursor_required_bytes d) (serialize_show_cursor d)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list