on fe55cd4 help sign in
vty/src/Graphics/Vty/Attributes.hs
206 lines 6.8 kB history raw
1-- Copyright 2009 Corey O'Connor
2-- Display attributes
3--
4-- For efficiency this can be, in the future, encoded into a single 32 bit word. The 32 bit word is
5-- first divided into 4 groups of 8 bits where:
6-- The first group codes what action should be taken with regards to the other groups.
7-- XXYYZZ__
8-- XX - style action
9-- 00 => reset to default
10-- 01 => unchanged
11-- 10 => set
12-- YY - foreground color action
13-- 00 => reset to default
14-- 01 => unchanged
15-- 10 => set
16-- ZZ - background color action
17-- 00 => reset to default
18-- 01 => unchanged
19-- 10 => set
20-- __ - unused
21--
22-- Next is the style flags
23-- SURBDO__
24-- S - standout
25-- U - underline
26-- R - reverse video
27-- B - blink
28-- D - dim
29-- O - bold
30-- __ - unused
31--
32-- Then the foreground color encoded into 8 bits.
33-- Then the background color encoded into 8 bits.
34--
35module Graphics.Vty.Attributes
36 where
38import Data.Bits
40import Data.Word
42-- | A display attribute defines the Color and Style of all the characters rendered after the
43-- attribute is applied.
44--
45-- At most 256 colors, picked from a 240 and 16 color palette, are possible for the background and
46-- foreground. The 240 colors and 16 colors are points in different palettes. See Color for more
47-- information.
48data Attr = Attr
49 { style :: !(MaybeDefault Style)
50 , fore_color :: !(MaybeDefault Color)
51 , back_color :: !(MaybeDefault Color)
52 } deriving ( Eq, Show )
54-- | Specifies the display attributes such that the final style and color values do not depend on
55-- the previously applied display attribute. The display attributes can still depend on the
56-- terminal's default colors (unfortunately).
57data FixedAttr = FixedAttr
58 { fixed_style :: !Style
59 , fixed_fore_color :: !(Maybe Color)
60 , fixed_back_color :: !(Maybe Color)
61 } deriving ( Eq, Show )
63-- | The style and color attributes can either be the terminal defaults. Or be equivalent to the
64-- previously applied style. Or be a specific value.
65data Eq v => MaybeDefault v = Default | KeepCurrent | SetTo !v
66 deriving ( Eq, Show )
68-- | Abstract data type representing a color.
69--
70-- Currently the foreground and background color are specified as points in either a:
71--
72-- * 16 color palette. Where the first 8 colors are equal to the 8 colors of the ISO 6429 (ANSI) 8
73-- color palette and the second 8 colors are bright/vivid versions of the first 8 colors.
74--
75-- * 240 color palette. This palette is a regular sampling of the full RGB colorspace.
76--
77-- The 8 ISO 6429 (ANSI) colors are as follows:
78--
79-- 0. black
80--
81-- 1. red
82--
83-- 2. green
84--
85-- 3. yellow
86--
87-- 4. blue
88--
89-- 5. magenta
90--
91-- 6. cyan
92--
93-- 7. white
94--
95-- The mapping from points in the 240 color palette to colors actually displayable by the terminal
96-- depends on the number of colors the terminal claims to support. Which is usually determined by
97-- the terminfo "colors" property. If this property is not being accurately reported then the color
98-- reproduction will be incorrect.
99--
100-- If the terminal reports <= 16 colors then the 240 color palette points are only mapped to the 8
101-- color pallete. I'm not sure of the RGB points for the "bright" colors which is why they are not
102-- addressable via the 240 color palette.
103--
104-- If the terminal reports > 16 colors then the 240 color palette points are mapped to the nearest
105-- points in a ("color count" - 16) subsampling of the 240 color palette.
106--
107-- All of this assumes the terminals are behaving similarly to xterm and rxvt when handling colors.
108-- And that the individual colors have not been remapped by the user. There may be a way to verify
109-- this through terminfo but I don't know it.
110--
111-- Seriously, terminal color support is INSANE.
112data Color = ISOColor !Word8 | Color240 !Word8
113 deriving ( Eq, Show )
115-- | Standard 8-color ANSI terminal color codes.
116black, red, green, yellow, blue, magenta, cyan, white :: Color
117black = ISOColor 0
118red = ISOColor 1
119green = ISOColor 2
120yellow = ISOColor 3
121blue = ISOColor 4
122magenta= ISOColor 5
123cyan = ISOColor 6
124white = ISOColor 7
126-- | Bright/Vivid variants of the standard 8-color ANSI
127bright_black, bright_red, bright_green, bright_yellow :: Color
128bright_blue, bright_magenta, bright_cyan, bright_white :: Color
129bright_black = ISOColor 8
130bright_red = ISOColor 9
131bright_green = ISOColor 10
132bright_yellow = ISOColor 11
133bright_blue = ISOColor 12
134bright_magenta= ISOColor 13
135bright_cyan = ISOColor 14
136bright_white = ISOColor 15
138-- | Styles are represented as an 8 bit word. Each bit in the word is 1 if the style attribute
139-- assigned to that bit should be applied and 0 if the style attribute should not be applied.
140type Style = Word8
142-- | The 6 possible style attributes:
143--
144-- * standout
145--
146-- * underline
147--
148-- * reverse_video
149--
150-- * blink
151--
152-- * dim
153--
154-- * bold/bright
155--
156-- ( The invisible, protect, and altcharset display attributes some terminals support are not
157-- supported via VTY.)
158standout, underline, reverse_video, blink, dim, bold :: Style
159standout = 0x01
160underline = 0x02
161reverse_video = 0x04
162blink = 0x08
163dim = 0x10
164bold = 0x20
166default_style_mask :: Style
167default_style_mask = 0x00
169style_mask :: Attr -> Word8
170style_mask attr
171 = case style attr of
172 Default -> 0
173 KeepCurrent -> 0
174 SetTo v -> v
176-- | true if the given Style value has the specified Style set.
177has_style :: Style -> Style -> Bool
178has_style s bit_mask = ( s .&. bit_mask ) /= 0
180-- | Set the foreground color of an `Attr'.
181with_fore_color :: Attr -> Color -> Attr
182with_fore_color attr c = attr { fore_color = SetTo c }
184-- | Set the background color of an `Attr'.
185with_back_color :: Attr -> Color -> Attr
186with_back_color attr c = attr { back_color = SetTo c }
188-- | Add the given style attribute
189with_style :: Attr -> Style -> Attr
190with_style attr style_flag = attr { style = SetTo $ style_mask attr .|. style_flag }
192-- | Sets the style, background color and foreground color to the default values for the terminal.
193-- There is no easy way to determine what the default background and foreground colors are.
194def_attr :: Attr
195def_attr = Attr Default Default Default
197-- | Keeps the style, background color and foreground color that was previously set. Used to
198-- override some part of the previous style.
199--
200-- EG: current_style `with_fore_color` bright_magenta
201--
202-- Would be the currently applied style (be it underline, bold, etc) but with the foreground color
203-- set to bright_magenta.
204current_attr :: Attr
205current_attr = Attr KeepCurrent KeepCurrent KeepCurrent