1-- Copyright 2009-2010 Corey O'Connor
2{-# LANGUAGE ForeignFunctionInterface, BangPatterns, UnboxedTuples #-}
5-- Good sources of documentation for terminal programming are:
6-- vt100 control sequences: http://vt100.net/docs/vt100-ug/chapter3.html#S3.3.3
7-- Xterm control sequences: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
9module Graphics.Vty ( Vty(..)
12 , module Graphics.Vty.Terminal
13 , module Graphics.Vty.Picture
14 , module Graphics.Vty.DisplayRegion
23import Graphics.Vty.Terminal
24import Graphics.Vty.Picture
25import Graphics.Vty.DisplayRegion
26import Graphics.Vty.LLInput
30import qualified System.Console.Terminfo as Terminfo
32-- | The main object. At most one should be created.
33-- An alternative is to use unsafePerformIO to automatically create a singleton Vty instance when
36-- This does not assure any thread safety. In theory, as long as an update action is not executed
37-- when another update action is already then it's safe to call this on multiple threads.
39-- todo: Once the Terminal interface encompasses input this interface will be deprecated.
40-- Currently, just using the Terminal interface there is no support for input events.
42 { -- | Outputs the given Picture. Equivalent to output_picture applied to a display context
43 -- implicitly managed by Vty.
44 update :: Picture -> IO ()
45 -- | Get one Event object, blocking if necessary.
46 , next_event :: IO Event
47 -- | Handle to the terminal interface. See `Terminal`
49 -- The use of Vty typically follows this process:
53 -- 1. use the update equation of Vty to display a picture
59 -- todo: provide a similar abstraction to Graphics.Vty.Terminal for input. Use haskeline's
60 -- input backend for implementation.
62 -- todo: remove explicit `shutdown` requirement.
63 , terminal :: TerminalHandle
64 -- | Refresh the display. Normally the library takes care of refreshing. Nonetheless, some
65 -- other program might output to the terminal and mess the display. In that case the user
66 -- might want to force a refresh.
68 -- | Clean up after vty.
69 -- The above methods will throw an exception if executed after this is executed.
73-- | Set up the state object for using vty. At most one state object should be
76mkVty = mkVtyEscDelay 0
78-- | Set up the state object for using vty. At most one state object should be
79-- created at a time. The delay, in microseconds, specifies the period of time to wait for a key
80-- following reading ESC from the terminal before considering the ESC key press as a discrete event.
81mkVtyEscDelay :: Int -> IO Vty
82mkVtyEscDelay escDelay = do
83 term_info <- Terminfo.setupTermFromEnv
86 (kvar, endi) <- initTermInput escDelay term_info
87 intMkVty kvar ( endi >> release_display t >> release_terminal t ) t
89intMkVty :: IO Event -> IO () -> TerminalHandle -> IO Vty
90intMkVty kvar fend t = do
91 last_pic_ref <- newIORef Nothing
92 last_update_ref <- newIORef Nothing
94 let inner_update in_pic = do
96 let DisplayRegion w h = b
97 cursor = pic_cursor in_pic
98 in_pic' = case cursor of
102 _ | x >= 0x80000000 -> 0
106 _ | y >= 0x80000000 -> 0
109 in in_pic { pic_cursor = Cursor x' y' }
111 mlast_update <- readIORef last_update_ref
112 update_data <- case mlast_update of
114 d <- display_context t b
115 output_picture d in_pic'
117 Just (last_bounds, last_context) -> do
120 d <- display_context t b
121 output_picture d in_pic'
124 output_picture last_context in_pic'
125 return (b, last_context)
126 writeIORef last_update_ref $ Just update_data
127 writeIORef last_pic_ref $ Just in_pic'
130 = writeIORef last_update_ref Nothing
131 >> readIORef last_pic_ref
132 >>= maybe ( return () ) ( \pic -> inner_update pic )
134 let gkey = do k <- kvar
136 (EvResize _ _) -> inner_refresh
138 >>= return . ( \(DisplayRegion w h)
139 -> EvResize (fromEnum w) (fromEnum h)
143 return $ Vty { update = inner_update
146 , refresh = inner_refresh
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