2{-# LANGUAGE StandaloneDeriving #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE DeriveGeneric #-}
6{-# LANGUAGE DeriveAnyClass #-}
10-- Attributes have three components: a foreground color, a background
11-- color, and a style mask. The simplest attribute is the default
12-- attribute, or 'defAttr'. Attributes can be modified with
13-- 'withForeColor', 'withBackColor', and 'withStyle', e.g.,
16-- defAttr \`withForeColor\` red
19-- 'Image' constructors often require an 'Attr' to indicate the
20-- attributes used in the image, e.g.,
23-- string (defAttr \`withForeColor\` red) "this text will be red"
26-- The appearance of 'Image's using 'defAttr' is determined by the The
27-- terminal, so this is not something VTY can control. The user is free
28-- to The define the color scheme of the terminal as they see fit.
30-- The value 'currentAttr' will keep the attributes of whatever was
32module Graphics.Vty.Attributes
33 ( module Graphics.Vty.Attributes.Color
55 -- * Setting attribute colors
59 -- * Setting hyperlinks
66#if !(MIN_VERSION_base(4,11,0))
69import Data.Text (Text)
73import Graphics.Vty.Attributes.Color
75-- | A display attribute defines the Color and Style of all the
76-- characters rendered after the attribute is applied.
78-- At most 256 colors, picked from a 240 and 16 color palette, are
79-- possible for the background and foreground. The 240 colors and
80-- 16 colors are points in different palettes. See Color for more
83 { attrStyle :: !(MaybeDefault Style)
84 , attrForeColor :: !(MaybeDefault Color)
85 , attrBackColor :: !(MaybeDefault Color)
86 , attrURL :: !(MaybeDefault Text)
87 } deriving ( Eq, Show, Read, Generic, NFData )
89-- This could be encoded into a single 32 bit word. The 32 bit word is
90-- first divided into 4 groups of 8 bits where: The first group codes
91-- what action should be taken with regards to the other groups.
94-- 00 => reset to default
97-- YY - foreground color action
98-- 00 => reset to default
101-- ZZ - background color action
102-- 00 => reset to default
107-- Next is the style flags
118-- Then the foreground color encoded into 8 bits.
119-- Then the background color encoded into 8 bits.
121instance Semigroup Attr where
123 Attr ( attrStyle attr0 <> attrStyle attr1 )
124 ( attrForeColor attr0 <> attrForeColor attr1 )
125 ( attrBackColor attr0 <> attrBackColor attr1 )
126 ( attrURL attr0 <> attrURL attr1 )
128instance Monoid Attr where
129 mempty = Attr mempty mempty mempty mempty
130#if !(MIN_VERSION_base(4,11,0))
134-- | Specifies the display attributes such that the final style and
135-- color values do not depend on the previously applied display
136-- attribute. The display attributes can still depend on the terminal's
137-- default colors (unfortunately).
138data FixedAttr = FixedAttr
139 { fixedStyle :: !Style
140 , fixedForeColor :: !(Maybe Color)
141 , fixedBackColor :: !(Maybe Color)
142 , fixedURL :: !(Maybe Text)
143 } deriving ( Eq, Show )
145-- | The style and color attributes can either be the terminal defaults.
146-- Or be equivalent to the previously applied style. Or be a specific
148data MaybeDefault v where
149 Default :: MaybeDefault v
150 KeepCurrent :: MaybeDefault v
151 SetTo :: forall v . ( Eq v, Show v, Read v ) => !v -> MaybeDefault v
153instance (NFData v) => NFData (MaybeDefault v) where
156 rnf (SetTo v) = rnf v
158deriving instance Eq v => Eq (MaybeDefault v)
159deriving instance Eq v => Show (MaybeDefault v)
160deriving instance (Eq v, Show v, Read v) => Read (MaybeDefault v)
162instance Eq v => Semigroup (MaybeDefault v) where
163 Default <> Default = Default
164 Default <> KeepCurrent = Default
165 Default <> SetTo v = SetTo v
166 KeepCurrent <> Default = Default
167 KeepCurrent <> KeepCurrent = KeepCurrent
168 KeepCurrent <> SetTo v = SetTo v
169 SetTo _v <> Default = Default
170 SetTo v <> KeepCurrent = SetTo v
171 SetTo _ <> SetTo v = SetTo v
173instance Eq v => Monoid ( MaybeDefault v ) where
175#if !(MIN_VERSION_base(4,11,0))
179-- | Styles are represented as an 8 bit word. Each bit in the word is 1
180-- if the style attribute assigned to that bit should be applied and 0
181-- if the style attribute should not be applied.
184-- | The 7 possible style attributes:
200-- (The invisible, protect, and altcharset display attributes some
201-- terminals support are not supported via VTY.)
202standout, underline, reverseVideo, blink, dim, bold, italic :: Style
211defaultStyleMask :: Style
212defaultStyleMask = 0x00
214styleMask :: Attr -> Word8
216 = case attrStyle attr of
221-- | true if the given Style value has the specified Style set.
222hasStyle :: Style -> Style -> Bool
223hasStyle s bitMask = ( s .&. bitMask ) /= 0
225-- | Set the foreground color of an `Attr'.
226withForeColor :: Attr -> Color -> Attr
227withForeColor attr c = attr { attrForeColor = SetTo c }
229-- | Set the background color of an `Attr'.
230withBackColor :: Attr -> Color -> Attr
231withBackColor attr c = attr { attrBackColor = SetTo c }
233-- | Add the given style attribute
234withStyle :: Attr -> Style -> Attr
235withStyle attr 0 = attr
236withStyle attr styleFlag = attr { attrStyle = SetTo $ styleMask attr .|. styleFlag }
238-- | Add a hyperlinked URL using the proposed [escape sequences for
240-- URLs](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda).
241-- These escape sequences are comparatively new and aren't widely
242-- supported in terminal emulators yet, but most terminal emulators
243-- that don't know about these sequences will ignore these sequences,
244-- and therefore this should fall back sensibly. In some cases they
245-- won't and this will result in garbage, so this is why hyperlinking is
246-- disabled by default, in which case this combinator has no observable
247-- effect. To enable it, enable 'Hyperlink' mode on your Vty output
249withURL :: Attr -> Text -> Attr
250withURL attr url = attr { attrURL = SetTo url }
252-- | Sets the style, background color and foreground color to the
253-- default values for the terminal. There is no easy way to determine
254-- what the default background and foreground colors are.
256defAttr = Attr Default Default Default Default
258-- | Keeps the style, background color and foreground color that was
259-- previously set. Used to override some part of the previous style.
261-- EG: current_style `withForeColor` brightMagenta
263-- Would be the currently applied style (be it underline, bold, etc) but
264-- with the foreground color set to brightMagenta.
266currentAttr = Attr KeepCurrent KeepCurrent KeepCurrent KeepCurrent
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