22-- Copyright (c) 2019-2022, 2026 Galen Huntington
33-- SPDX-License-Identifier: GPL-2.0-or-later
44
5- --
65-- | Color manipulation
7- --
86
97module Style where
108
@@ -48,17 +46,15 @@ data Style = Style !Color !Color
4846 deriving stock (Eq ,Ord )
4947
5048-- | A styled UTF-8 ByteString segment.
51- data Segment = Seg {- # UNPACK # -} !Style {- # UNPACK #-} !ByteString
49+ data Segment = Seg ! Style {- # UNPACK #-} !ByteString
5250
5351-- | A line of segments.
5452type Line = [Segment ]
5553
5654------------------------------------------------------------------------
57- --
5855-- | Named colors for the config file and the built-in styles. The
5956-- \"dark\" name of each pair is the normal-intensity hue; the plain name
6057-- is its bright variant (so @red@ is bright, @darkred@ is normal).
61- --
6258stringToColor :: String -> Maybe Color
6359stringToColor s = case map toLower s of
6460 " black" -> Just $ Color Normal Black
@@ -82,31 +78,21 @@ stringToColor s = case map toLower s of
8278 _ -> Nothing
8379
8480------------------------------------------------------------------------
85- --
8681-- | Set some colours, perform an action, and then reset the colours
87- --
8882withStyle :: Style -> IO () -> IO ()
8983withStyle sty fn = uiAttr sty >>= setAttribute >> fn >> reset
9084{-# INLINE withStyle #-}
9185
92- --
9386-- | manipulate the current attributes of the standard screen
9487-- Only set attr if it's different to the current one?
95- --
9688setAttribute :: (Curses. Attr , Curses. Pair ) -> IO ()
9789setAttribute = uncurry Curses. attrSet
98- {-# INLINE setAttribute #-}
9990
100- --
10191-- | Reset the screen to normal values
102- --
10392reset :: IO ()
10493reset = setAttribute (Curses. attr0, Curses. Pair 0 )
105- {-# INLINE reset #-}
10694
107- --
10895-- | And turn on the colours
109- --
11096initcolours :: UIStyle -> IO ()
11197initcolours sty = do
11298 let ls = [sty. modals, sty. warnings, sty. window,
@@ -119,7 +105,6 @@ initcolours sty = do
119105 uiAttr sty. window >>= \ (_,p) -> Curses. bkgrndSet nullA p
120106
121107------------------------------------------------------------------------
122- --
123108-- | Set up the ui attributes, given a ui style record
124109--
125110-- Returns an association list of pairs for foreground and bg colors,
@@ -138,24 +123,20 @@ initUiColors stys = do
138123 pure (sty, (a `Curses.attrPlus` b, Curses. Pair p))
139124
140125------------------------------------------------------------------------
141- --
142126-- | Getting from nice abstract colours to ncurses-settable values
143127
144128-- 20% of allocss occur here! But there's only 3 or 4 colours :/
145129-- Every call to uiAttr
146- --
147130uiAttr :: Style -> IO (Curses. Attr , Curses. Pair )
148131uiAttr sty = do
149132 m <- readIORef pairMap
150133 pure $ lookupPair m sty
151- {-# INLINE uiAttr #-}
152134
153135-- | Given a curses color pair, find the Curses.Pair (i.e. the pair
154136-- curses thinks these colors map to) from the state
155137lookupPair :: PairMap -> Style -> (Curses. Attr , Curses. Pair )
156138lookupPair m s =
157139 fromMaybe (Curses. attr0, Curses. Pair 0 ) (M. lookup s m)
158- {-# INLINE lookupPair #-}
159140
160141-- | Keep a map of nice style defs to underlying curses pairs, created at init time
161142type PairMap = M. Map Style (Curses. Attr , Curses. Pair )
@@ -166,22 +147,17 @@ pairMap = unsafePerformIO $ newIORef M.empty
166147{-# NOINLINE pairMap #-}
167148
168149------------------------------------------------------------------------
169- --
170150-- Basic (ncurses) colours.
171- --
151+
172152defaultColor :: Curses. Color
173153defaultColor = fromJust $ Curses. color " default"
174154
175- --
176155-- Combine attribute with another attribute
177- --
178156setBoldA , setReverseA :: Curses. Attr -> Curses. Attr
179157setBoldA = flip Curses. setBold True
180158setReverseA = flip Curses. setReverse True
181159
182- --
183160-- | Some attribute constants
184- --
185161boldA , nullA , reverseA :: Curses. Attr
186162nullA = Curses. attr0
187163boldA = setBoldA nullA
@@ -194,7 +170,6 @@ newtype CColor = CColor (Curses.Attr, Curses.Color)
194170-- | Map an abstract 'Style' to its ncurses foreground/background pair.
195171style2curses :: Style -> (CColor , CColor )
196172style2curses (Style fg bg) = (fgCursCol fg, bgCursCol bg)
197- {-# INLINE style2curses #-}
198173
199174-- | The ncurses color for each ANSI hue.
200175hueColor :: Hue -> Curses. Color
@@ -226,7 +201,6 @@ plainSeg :: ByteString -> Segment
226201plainSeg = Seg defaultSty
227202
228203------------------------------------------------------------------------
229- --
230204-- Support for runtime configuration
231205-- We choose a simple strategy, read/showable record types, with strings
232206-- to represent colors
@@ -260,7 +234,6 @@ buildStyle bs = UIStyle {
260234 , blockcursor = f bs. hmp3_blockcursor
261235 , progress = f bs. hmp3_progress
262236 }
263-
264237 where
265238 f (x,y) = Style (g x) (g y)
266239 g x = fromMaybe Default $ stringToColor x
0 commit comments