-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyle.hs
340 lines (304 loc) · 11.5 KB
/
Style.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
--
-- Copyright (c) 2004-2008 Don Stewart - http://www.cse.unsw.edu.au/~dons
-- Copyright (c) 2019-2021 Galen Huntington
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
--
--
-- | Color manipulation
--
module Style where
import Base
import qualified UI.HSCurses.Curses as Curses
import qualified Data.Map as M (fromList, empty, lookup, Map)
------------------------------------------------------------------------
-- | User-configurable colours
-- Each component of this structure corresponds to a fg\/bg colour pair
-- for an item in the ui
data UIStyle = UIStyle {
window :: !Style -- default window colour
, modals :: !Style -- help screen
, titlebar :: !Style -- titlebar of window
, selected :: !Style -- currently playing track
, cursors :: !Style -- the scrolling cursor line
, combined :: !Style -- the style to use when the cursor is on the current track
, warnings :: !Style -- style for warnings
, blockcursor :: !Style -- style for the block cursor when typing text
, progress :: !Style -- style for the progress bar
}
------------------------------------------------------------------------
-- | Colors
data Color
= RGB {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
| Default
| Reverse
deriving stock (Eq,Ord)
-- | Foreground and background color pairs
data Style = Style !Color !Color
deriving stock (Eq,Ord)
-- | Can hold an optimized ByteString or a Unicode String.
data AmbiString = B !ByteString | U !String
-- | A list of such values (the representation is optimised)
data StringA
= Fast {-# UNPACK #-} !ByteString {-# UNPACK #-} !Style
| FancyS ![(AmbiString,Style)] -- one line made up of segments
------------------------------------------------------------------------
--
-- | Some simple colours (derivied from proxima\/src\/common\/CommonTypes.hs)
--
-- But we don't have a light blue?
--
black, grey, darkred, red, darkgreen, green, brown, yellow :: Color
darkblue, blue, purple, magenta, darkcyan, cyan, white, brightwhite :: Color
black = RGB 0 0 0
grey = RGB 128 128 128
darkred = RGB 139 0 0
red = RGB 255 0 0
darkgreen = RGB 0 100 0
green = RGB 0 128 0
brown = RGB 165 42 42
yellow = RGB 255 255 0
darkblue = RGB 0 0 139
blue = RGB 0 0 255
purple = RGB 128 0 128
magenta = RGB 255 0 255
darkcyan = RGB 0 139 139
cyan = RGB 0 255 255
white = RGB 165 165 165
brightwhite = RGB 255 255 255
defaultfg, defaultbg, reversefg, reversebg :: Color
defaultfg = Default
defaultbg = Default
reversefg = Reverse
reversebg = Reverse
--
-- | map strings to colors
--
stringToColor :: String -> Maybe Color
stringToColor s = case map toLower s of
"black" -> Just black
"grey" -> Just grey
"darkred" -> Just darkred
"red" -> Just red
"darkgreen" -> Just darkgreen
"green" -> Just green
"brown" -> Just brown
"yellow" -> Just yellow
"darkblue" -> Just darkblue
"blue" -> Just blue
"purple" -> Just purple
"magenta" -> Just magenta
"darkcyan" -> Just darkcyan
"cyan" -> Just cyan
"white" -> Just white
"brightwhite" -> Just brightwhite
"default" -> Just Default
"reverse" -> Just Reverse
_ -> Nothing
------------------------------------------------------------------------
--
-- | Set some colours, perform an action, and then reset the colours
--
withStyle :: Style -> IO () -> IO ()
withStyle sty fn = uiAttr sty >>= setAttribute >> fn >> reset
{-# INLINE withStyle #-}
--
-- | manipulate the current attributes of the standard screen
-- Only set attr if it's different to the current one?
--
setAttribute :: (Curses.Attr, Curses.Pair) -> IO ()
setAttribute = uncurry Curses.attrSet
{-# INLINE setAttribute #-}
--
-- | Reset the screen to normal values
--
reset :: IO ()
reset = setAttribute (Curses.attr0, Curses.Pair 0)
{-# INLINE reset #-}
--
-- | And turn on the colours
--
initcolours :: UIStyle -> IO ()
initcolours sty = do
let ls = [modals sty, warnings sty, window sty,
selected sty, titlebar sty, progress sty,
blockcursor sty, cursors sty, combined sty ]
(Style fg bg) = progress sty -- bonus style
pairs <- initUiColors (ls ++ [Style bg bg, Style fg fg])
writeIORef pairMap pairs
-- set the background
uiAttr (window sty) >>= \(_,p) -> Curses.bkgrndSet nullA p
------------------------------------------------------------------------
--
-- | Set up the ui attributes, given a ui style record
--
-- Returns an association list of pairs for foreground and bg colors,
-- associated with the terminal color pair that has been defined for
-- those colors.
--
initUiColors :: [Style] -> IO PairMap
initUiColors stys = do
ls <- sequence [ uncurry fn m | m <- zip stys [1..] ]
pure (M.fromList ls)
where
fn :: Style -> Int -> IO (Style, (Curses.Attr,Curses.Pair))
fn sty p = do
let (CColor (a,fgc),CColor (b,bgc)) = style2curses sty
discardErrors $ Curses.initPair (Curses.Pair p) fgc bgc
pure (sty, (a `Curses.attrPlus` b, Curses.Pair p))
------------------------------------------------------------------------
--
-- | Getting from nice abstract colours to ncurses-settable values
-- 20% of allocss occur here! But there's only 3 or 4 colours :/
-- Every call to uiAttr
--
uiAttr :: Style -> IO (Curses.Attr, Curses.Pair)
uiAttr sty = do
m <- readIORef pairMap
pure $ lookupPair m sty
{-# INLINE uiAttr #-}
-- | Given a curses color pair, find the Curses.Pair (i.e. the pair
-- curses thinks these colors map to) from the state
lookupPair :: PairMap -> Style -> (Curses.Attr, Curses.Pair)
lookupPair m s =
fromMaybe (Curses.attr0, Curses.Pair 0) (M.lookup s m)
{-# INLINE lookupPair #-}
-- | Keep a map of nice style defs to underlying curses pairs, created at init time
type PairMap = M.Map Style (Curses.Attr, Curses.Pair)
-- | map of Curses.Color pairs to ncurses terminal Pair settings
pairMap :: IORef PairMap
pairMap = unsafePerformIO $ newIORef M.empty
{-# NOINLINE pairMap #-}
------------------------------------------------------------------------
--
-- Basic (ncurses) colours.
--
defaultColor :: Curses.Color
defaultColor = fromJust $ Curses.color "default"
cblack, cred, cgreen, cyellow, cblue, cmagenta, ccyan, cwhite :: Curses.Color
cblack = fromJust $ Curses.color "black"
cred = fromJust $ Curses.color "red"
cgreen = fromJust $ Curses.color "green"
cyellow = fromJust $ Curses.color "yellow"
cblue = fromJust $ Curses.color "blue"
cmagenta = fromJust $ Curses.color "magenta"
ccyan = fromJust $ Curses.color "cyan"
cwhite = fromJust $ Curses.color "white"
--
-- Combine attribute with another attribute
--
setBoldA, setReverseA :: Curses.Attr -> Curses.Attr
setBoldA = flip Curses.setBold True
setReverseA = flip Curses.setReverse True
--
-- | Some attribute constants
--
boldA, nullA, reverseA :: Curses.Attr
nullA = Curses.attr0
boldA = setBoldA nullA
reverseA = setReverseA nullA
------------------------------------------------------------------------
newtype CColor = CColor (Curses.Attr, Curses.Color)
--
-- | Map Style rgb rgb colours to ncurses pairs
-- TODO a generic way to turn an rgb into the nearest curses color
--
style2curses :: Style -> (CColor, CColor)
style2curses (Style fg bg) = (fgCursCol fg, bgCursCol bg)
{-# INLINE style2curses #-}
fgCursCol :: Color -> CColor
fgCursCol c = case c of
RGB 0 0 0 -> CColor (nullA, cblack)
RGB 128 128 128 -> CColor (boldA, cblack)
RGB 139 0 0 -> CColor (nullA, cred)
RGB 255 0 0 -> CColor (boldA, cred)
RGB 0 100 0 -> CColor (nullA, cgreen)
RGB 0 128 0 -> CColor (boldA, cgreen)
RGB 165 42 42 -> CColor (nullA, cyellow)
RGB 255 255 0 -> CColor (boldA, cyellow)
RGB 0 0 139 -> CColor (nullA, cblue)
RGB 0 0 255 -> CColor (boldA, cblue)
RGB 128 0 128 -> CColor (nullA, cmagenta)
RGB 255 0 255 -> CColor (boldA, cmagenta)
RGB 0 139 139 -> CColor (nullA, ccyan)
RGB 0 255 255 -> CColor (boldA, ccyan)
RGB 165 165 165 -> CColor (nullA, cwhite)
RGB 255 255 255 -> CColor (boldA, cwhite)
Default -> CColor (nullA, defaultColor)
Reverse -> CColor (reverseA, defaultColor)
_ -> CColor (nullA, cblack) -- NB
bgCursCol :: Color -> CColor
bgCursCol c = case c of
RGB 0 0 0 -> CColor (nullA, cblack)
RGB 128 128 128 -> CColor (nullA, cblack)
RGB 139 0 0 -> CColor (nullA, cred)
RGB 255 0 0 -> CColor (nullA, cred)
RGB 0 100 0 -> CColor (nullA, cgreen)
RGB 0 128 0 -> CColor (nullA, cgreen)
RGB 165 42 42 -> CColor (nullA, cyellow)
RGB 255 255 0 -> CColor (nullA, cyellow)
RGB 0 0 139 -> CColor (nullA, cblue)
RGB 0 0 255 -> CColor (nullA, cblue)
RGB 128 0 128 -> CColor (nullA, cmagenta)
RGB 255 0 255 -> CColor (nullA, cmagenta)
RGB 0 139 139 -> CColor (nullA, ccyan)
RGB 0 255 255 -> CColor (nullA, ccyan)
RGB 165 165 165 -> CColor (nullA, cwhite)
RGB 255 255 255 -> CColor (nullA, cwhite)
Default -> CColor (nullA, defaultColor)
Reverse -> CColor (reverseA, defaultColor)
_ -> CColor (nullA, cwhite) -- NB
defaultSty :: Style
defaultSty = Style Default Default
------------------------------------------------------------------------
--
-- Support for runtime configuration
-- We choose a simple strategy, read/showable record types, with strings
-- to represent colors
--
-- The fields must map to UIStyle
--
-- It is this data type that is stored in 'show' format in style.conf
--
data Config = Config {
hmp3_window :: (String,String)
, hmp3_modals :: (String,String)
, hmp3_titlebar :: (String,String)
, hmp3_selected :: (String,String)
, hmp3_cursors :: (String,String)
, hmp3_combined :: (String,String)
, hmp3_warnings :: (String,String)
, hmp3_blockcursor :: (String,String)
, hmp3_progress :: (String,String)
} deriving stock (Show,Read)
--
-- | Read style.conf, and construct a UIStyle from it, to insert into
--
buildStyle :: Config -> UIStyle
buildStyle bs = UIStyle {
window = f $ hmp3_window bs
, modals = f $ hmp3_modals bs
, titlebar = f $ hmp3_titlebar bs
, selected = f $ hmp3_selected bs
, cursors = f $ hmp3_cursors bs
, combined = f $ hmp3_combined bs
, warnings = f $ hmp3_warnings bs
, blockcursor = f $ hmp3_blockcursor bs
, progress = f $ hmp3_progress bs
}
where
f (x,y) = Style (g x) (g y)
g x = fromMaybe Default $ stringToColor x