on 175c235 help sign in
vty/src/Graphics/Vty/Output.hs
110 lines 4.0 kB history raw
1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE NamedFieldPuns #-}
3-- | Output functions.
4--
5-- Access to the current terminal or a specific terminal device.
6--
7-- See also:
8--
9-- 1. "Graphics.Vty.Output": This instantiates an abtract interface to
10-- the terminal interface based on the TERM and COLORTERM environment
11-- variables.
12--
13-- 2. "Graphics.Vty.Output.Interface": Defines the generic interface all
14-- terminals need to implement.
15--
16-- 3. "Graphics.Vty.Output.TerminfoBased": Defines a terminal instance
17-- that uses terminfo for all control strings. No attempt is made to
18-- change the character set to UTF-8 for these terminals. I don't know a
19-- way to reliably determine if that is required or how to do so.
20--
21-- 4. "Graphics.Vty.Output.XTermColor": This module contains an
22-- interface suitable for xterm-like terminals. These are the terminals
23-- where TERM == xterm. This does use terminfo for as many control codes
24-- as possible.
25module Graphics.Vty.Output
26 ( outputForConfig
27 , setCursorPos
28 , hideCursor
29 , showCursor
30 )
31where
33import Control.Monad (when)
35import Graphics.Vty.Config
36import Graphics.Vty.Image (regionWidth, regionHeight)
37import Graphics.Vty.Output.Interface
38import Graphics.Vty.Output.XTermColor as XTermColor
39import Graphics.Vty.Output.TerminfoBased as TerminfoBased
41import Blaze.ByteString.Builder (writeToByteString)
43import Control.Monad.Trans
45import Data.List (isPrefixOf)
46import Data.Monoid ((<>))
48-- | Returns a `Output` for the terminal specified in `Config`
49--
50-- The specific Output implementation used is hidden from the API user.
51-- All terminal implementations are assumed to perform more, or less,
52-- the same. Currently, all implementations use terminfo for at least
53-- some terminal specific information.
54--
55-- Specifics about it being based on terminfo are hidden from the API
56-- user. If a terminal implementation is developed for a terminal
57-- without terminfo support then Vty should work as expected on that
58-- terminal.
59--
60-- Selection of a terminal is done as follows:
61--
62-- * If TERM contains "xterm" or "screen", use XTermColor.
63-- * otherwise use the TerminfoBased driver.
64outputForConfig :: Config -> IO Output
65outputForConfig Config{ outputFd = Just fd, termName = Just termName, .. } = do
66 t <- if "xterm" `isPrefixOf` termName || "screen" `isPrefixOf` termName
67 then XTermColor.reserveTerminal termName fd
68 -- Not an xterm-like terminal. try for generic terminfo.
69 else TerminfoBased.reserveTerminal termName fd
71 case mouseMode of
72 Just s -> setMode t Mouse s
73 Nothing -> return ()
75 case bracketedPasteMode of
76 Just s -> setMode t BracketedPaste s
77 Nothing -> return ()
79 return t
80outputForConfig config = (<> config) <$> standardIOConfig >>= outputForConfig
82-- | Sets the cursor position to the given output column and row.
83--
84-- This is not necessarially the same as the character position with the
85-- same coordinates. Characters can be a variable number of columns in
86-- width.
87--
88-- Currently, the only way to set the cursor position to a given
89-- character coordinate is to specify the coordinate in the Picture
90-- instance provided to outputPicture or refresh.
91setCursorPos :: MonadIO m => Output -> Int -> Int -> m ()
92setCursorPos t x y = do
93 bounds <- displayBounds t
94 when (x >= 0 && x < regionWidth bounds && y >= 0 && y < regionHeight bounds) $ do
95 dc <- displayContext t bounds
96 liftIO $ outputByteBuffer t $ writeToByteString $ writeMoveCursor dc x y
98-- | Hides the cursor
99hideCursor :: MonadIO m => Output -> m ()
100hideCursor t = do
101 bounds <- displayBounds t
102 dc <- displayContext t bounds
103 liftIO $ outputByteBuffer t $ writeToByteString $ writeHideCursor dc
105-- | Shows the cursor
106showCursor :: MonadIO m => Output -> m ()
107showCursor t = do
108 bounds <- displayBounds t
109 dc <- displayContext t bounds
110 liftIO $ outputByteBuffer t $ writeToByteString $ writeShowCursor dc