1-- Copyright 2009 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.
72-- | Set up the state object for using vty. At most one state object should be
75mkVty = mkVtyEscDelay 0
77mkVtyEscDelay :: Int -> IO Vty
78mkVtyEscDelay escDelay = do
79 term_info <- Terminfo.setupTermFromEnv
82 (kvar, endi) <- initTermInput escDelay term_info
83 intMkVty kvar ( endi >> release_display t >> release_terminal t ) t
85intMkVty :: IO Event -> IO () -> TerminalHandle -> IO Vty
86intMkVty kvar fend t = do
87 last_pic_ref <- newIORef undefined
88 last_update_ref <- newIORef Nothing
90 let inner_update in_pic = do
92 let DisplayRegion w h = b
93 cursor = pic_cursor in_pic
94 in_pic' = case cursor of
98 _ | x >= 0x80000000 -> 0
102 _ | y >= 0x80000000 -> 0
105 in in_pic { pic_cursor = Cursor x' y' }
107 mlast_update <- readIORef last_update_ref
108 update_data <- case mlast_update of
110 d <- display_context t b
111 output_picture d in_pic'
113 Just (last_bounds, last_context) -> do
116 d <- display_context t b
117 output_picture d in_pic'
120 output_picture last_context in_pic'
121 return (b, last_context)
122 writeIORef last_update_ref $ Just update_data
123 writeIORef last_pic_ref $ Just in_pic'
126 = writeIORef last_update_ref Nothing
127 >> readIORef last_pic_ref
128 >>= maybe ( return () ) ( \pic -> inner_update pic )
130 let gkey = do k <- kvar
132 (EvResize _ _) -> inner_refresh
134 >>= return . ( \(DisplayRegion w h)
135 -> EvResize (fromEnum w) (fromEnum h)
139 return $ Vty { update = inner_update
142 , 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