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
53 -- * Setting attribute colors
57 -- * Setting hyperlinks
61 , module Graphics.Vty.Attributes.Color
62 , module Graphics.Vty.Attributes.Color240
68#if !(MIN_VERSION_base(4,11,0))
71import Data.Text (Text)
75import Graphics.Vty.Attributes.Color
76import Graphics.Vty.Attributes.Color240
78-- | A display attribute defines the Color and Style of all the
79-- characters rendered after the attribute is applied.
81-- At most 256 colors, picked from a 240 and 16 color palette, are
82-- possible for the background and foreground. The 240 colors and
83-- 16 colors are points in different palettes. See Color for more
86 { attrStyle :: !(MaybeDefault Style)
87 , attrForeColor :: !(MaybeDefault Color)
88 , attrBackColor :: !(MaybeDefault Color)
89 , attrURL :: !(MaybeDefault Text)
90 } deriving ( Eq, Show, Read, Generic, NFData )
92-- This could be encoded into a single 32 bit word. The 32 bit word is
93-- first divided into 4 groups of 8 bits where: The first group codes
94-- what action should be taken with regards to the other groups.
97-- 00 => reset to default
100-- YY - foreground color action
101-- 00 => reset to default
104-- ZZ - background color action
105-- 00 => reset to default
110-- Next is the style flags
121-- Then the foreground color encoded into 8 bits.
122-- Then the background color encoded into 8 bits.
124instance Semigroup Attr where
126 Attr ( attrStyle attr0 <> attrStyle attr1 )
127 ( attrForeColor attr0 <> attrForeColor attr1 )
128 ( attrBackColor attr0 <> attrBackColor attr1 )
129 ( attrURL attr0 <> attrURL attr1 )
131instance Monoid Attr where
132 mempty = Attr mempty mempty mempty mempty
133#if !(MIN_VERSION_base(4,11,0))
137-- | Specifies the display attributes such that the final style and
138-- color values do not depend on the previously applied display
139-- attribute. The display attributes can still depend on the terminal's
140-- default colors (unfortunately).
141data FixedAttr = FixedAttr
142 { fixedStyle :: !Style
143 , fixedForeColor :: !(Maybe Color)
144 , fixedBackColor :: !(Maybe Color)
145 , fixedURL :: !(Maybe Text)
146 } deriving ( Eq, Show )
148-- | The style and color attributes can either be the terminal defaults.
149-- Or be equivalent to the previously applied style. Or be a specific
151data MaybeDefault v where
152 Default :: MaybeDefault v
153 KeepCurrent :: MaybeDefault v
154 SetTo :: forall v . ( Eq v, Show v, Read v ) => !v -> MaybeDefault v
156instance (NFData v) => NFData (MaybeDefault v) where
159 rnf (SetTo v) = rnf v
161deriving instance Eq v => Eq (MaybeDefault v)
162deriving instance Eq v => Show (MaybeDefault v)
163deriving instance (Eq v, Show v, Read v) => Read (MaybeDefault v)
165instance Eq v => Semigroup (MaybeDefault v) where
166 Default <> Default = Default
167 Default <> KeepCurrent = Default
168 Default <> SetTo v = SetTo v
169 KeepCurrent <> Default = Default
170 KeepCurrent <> KeepCurrent = KeepCurrent
171 KeepCurrent <> SetTo v = SetTo v
172 SetTo _v <> Default = Default
173 SetTo v <> KeepCurrent = SetTo v
174 SetTo _ <> SetTo v = SetTo v
176instance Eq v => Monoid ( MaybeDefault v ) where
178#if !(MIN_VERSION_base(4,11,0))
182-- | Styles are represented as an 8 bit word. Each bit in the word is 1
183-- if the style attribute assigned to that bit should be applied and 0
184-- if the style attribute should not be applied.
187-- | The 7 possible style attributes:
203-- (The invisible, protect, and altcharset display attributes some
204-- terminals support are not supported via VTY.)
205standout, underline, reverseVideo, blink, dim, bold, italic :: Style
214defaultStyleMask :: Style
215defaultStyleMask = 0x00
217styleMask :: Attr -> Word8
219 = case attrStyle attr of
224-- | true if the given Style value has the specified Style set.
225hasStyle :: Style -> Style -> Bool
226hasStyle s bitMask = ( s .&. bitMask ) /= 0
228-- | Set the foreground color of an `Attr'.
229withForeColor :: Attr -> Color -> Attr
230withForeColor attr c = attr { attrForeColor = SetTo c }
232-- | Set the background color of an `Attr'.
233withBackColor :: Attr -> Color -> Attr
234withBackColor attr c = attr { attrBackColor = SetTo c }
236-- | Add the given style attribute
237withStyle :: Attr -> Style -> Attr
238withStyle attr 0 = attr
239withStyle attr styleFlag = attr { attrStyle = SetTo $ styleMask attr .|. styleFlag }
241-- | Add a hyperlinked URL using the proposed [escape sequences for
243-- URLs](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda).
244-- These escape sequences are comparatively new and aren't widely
245-- supported in terminal emulators yet, but most terminal emulators
246-- that don't know about these sequences will ignore these sequences,
247-- and therefore this should fall back sensibly. In some cases they
248-- won't and this will result in garbage, so this is why hyperlinking is
249-- disabled by default, in which case this combinator has no observable
250-- effect. To enable it, enable 'Hyperlink' mode on your Vty output
252withURL :: Attr -> Text -> Attr
253withURL attr url = attr { attrURL = SetTo url }
255-- | Sets the style, background color and foreground color to the
256-- default values for the terminal. There is no easy way to determine
257-- what the default background and foreground colors are.
259defAttr = Attr Default Default Default Default
261-- | Keeps the style, background color and foreground color that was
262-- previously set. Used to override some part of the previous style.
264-- EG: current_style `withForeColor` brightMagenta
266-- Would be the currently applied style (be it underline, bold, etc) but
267-- with the foreground color set to brightMagenta.
269currentAttr = 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