Skip to content

Commit 21ba4be

Browse files
Replace StringA with simple types.
1 parent 04ad07d commit 21ba4be

5 files changed

Lines changed: 42 additions & 42 deletions

File tree

Core.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ start opts (Playlist folders music) = do
9292
, histSize = opts.histSize
9393
, miniFocused = False
9494
, status = Stopped
95-
, minibuffer = Fast mempty defaultSty
95+
, minibuffer = []
9696
, uptime = mempty
9797
}
9898

@@ -450,7 +450,7 @@ genericJumpToMatch re sw k sel = do
450450
case [ i | i <- l, match $ extract (fs ! i) ] of
451451
i:_ -> (st' { cursor = sel i st }, True)
452452
_ -> (st', False)
453-
unless found $ putMessage $ Fast "No match found." defaultSty
453+
unless found $ putMessage [plainSeg "No match found."]
454454

455455
------------------------------------------------------------------------
456456

@@ -536,14 +536,14 @@ loadConfig = do
536536
------------------------------------------------------------------------
537537
-- Set the minibuffer
538538

539-
putMessage :: StringA -> IO ()
539+
putMessage :: Line -> IO ()
540540
putMessage s = modifyHS_ \st -> st { minibuffer = s }
541541

542542
clearMessage :: IO ()
543-
clearMessage = putMessage $ Fast P.empty defaultSty
543+
clearMessage = putMessage []
544544

545545
warnA :: String -> IO ()
546546
warnA x = do
547547
sty <- getsHS (.uiStyle.warnings)
548-
putMessage $ Fast (P.pack x) sty
548+
putMessage [Seg sty (P.pack x)]
549549

Keymap.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Core
1818
import Elements (package)
1919
import Keyboard (unkey, charToKey, Key(..), historyKeys)
2020
import State (getsHS, modifyHS_, KeysHelp, Modal(..), HState(..))
21-
import Style (defaultSty, StringA(Fast))
21+
import Style (plainSeg)
2222
import Text (dropLastUTF8)
2323
import UI qualified (getKey, resetui)
2424

@@ -112,7 +112,7 @@ searchMode stype = step where
112112
leave = toggleFocus $> mainMode
113113

114114
renderSearch :: Char -> Zipper ByteString -> IO ()
115-
renderSearch prefix z = putMessage $ Fast (prefix `P.cons` z.cur) defaultSty
115+
renderSearch prefix z = putMessage [plainSeg $ prefix `P.cons` z.cur]
116116

117117
enter', delete' :: [Char]
118118
enter' = ['\n', '\r']

State.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Base
1111

1212
import Decoder (Status, Frame, Id3, Cmd, cmdToBS, mp3Tool)
1313
import Playlist (FileArray, DirArray)
14-
import Style (StringA(Fast), UIStyle(warnings))
14+
import Style (Line, Segment(Seg), UIStyle(warnings))
1515

1616
import Data.ByteString (hPut)
1717
import GHC.Records
@@ -38,7 +38,7 @@ data HState = HState
3838
, id3 :: !(Maybe Id3) -- maybe mp3 id3 info
3939
, info :: !(Maybe ByteString) -- mp3 info
4040
, status :: !Status
41-
, minibuffer :: !StringA -- contents of minibuffer
41+
, minibuffer :: !Line -- contents of minibuffer
4242
, modal :: !(Maybe Modal) -- modal visible
4343
, miniFocused :: !Bool -- is the mini buffer focused?
4444
, mode :: !Mode
@@ -115,9 +115,8 @@ sendMpg' c = do
115115
sendMpg :: Cmd -> IO ()
116116
sendMpg c = do
117117
ok <- sendMpg' c
118-
when (not ok) do
119-
modifyHS_ \st -> st { minibuffer =
120-
Fast (mp3Tool <> " process not running") st.uiStyle.warnings }
118+
when (not ok) $ modifyHS_ \st -> st { minibuffer =
119+
[Seg st.uiStyle.warnings (mp3Tool <> " process not running")] }
121120

122121
------------------------------------------------------------------------
123122
-- State accessor functions.

Style.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ data Hue = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White
4747
data Style = Style !Color !Color
4848
deriving stock (Eq,Ord)
4949

50-
-- | A list of styled UTF-8 ByteString segments making up one line.
51-
-- 'Fast' is the single-segment fast path; 'FancyS' is a multi-segment line.
52-
data StringA
53-
= Fast {-# UNPACK #-} !ByteString {-# UNPACK #-} !Style
54-
| FancyS ![(ByteString, Style)]
50+
-- | A styled UTF-8 ByteString segment.
51+
data Segment = Seg !Style !ByteString
52+
53+
-- | A line of segments.
54+
type Line = [Segment]
5555

5656
------------------------------------------------------------------------
5757
--
@@ -222,6 +222,9 @@ defaultSty = Style Default Default
222222
style :: String -> String -> Style
223223
style a b = let f = fromJust . stringToColor in Style (f a) (f b)
224224

225+
plainSeg :: ByteString -> Segment
226+
plainSeg = Seg defaultSty
227+
225228
------------------------------------------------------------------------
226229
--
227230
-- Support for runtime configuration

UI.hs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ data DrawData = DD { drawWidth :: Int, drawState :: HState }
145145
------------------------------------------------------------------------
146146

147147
-- | Info about the current track
148-
pPlaying :: DrawData -> StringA
149-
pPlaying dd = flip Fast defaultSty $ " " <> mconcat line where
148+
pPlaying :: DrawData -> Line
149+
pPlaying dd = pure $ plainSeg $ " " <> mconcat line where
150150
x = dd.drawWidth
151151
a = pId3 dd
152152
b = fromMaybe "" dd.drawState.info -- mp3 info
@@ -164,17 +164,17 @@ pId3 DD{drawState=st} = maybe (st.music ! st.current).fbase (.str) st.id3
164164
------------------------------------------------------------------------
165165

166166
-- | Show progress bar.
167-
progressBar :: DrawData -> StringA
168-
progressBar (DD w st) = FancyS [
169-
(" ", defaultSty), (spaces x, Style fg fg), (spaces (w'-x), sty)]
167+
progressBar :: DrawData -> Line
168+
progressBar (DD w st) = [
169+
plainSeg " ", Seg (Style fg fg) (spaces x), Seg sty (spaces (w'-x)) ]
170170
where
171171
w' = w - 4
172172
x = El.progress w' st.clock
173173
sty@(Style fg _) = st.uiStyle.progress
174174

175175
-- | Two lines showing clock.
176-
clockLines :: DrawData -> [StringA]
177-
clockLines dd@(DD w st) = [progressBar dd, Fast (El.pTimes w st.clock) defaultSty]
176+
clockLines :: DrawData -> [Line]
177+
clockLines dd@(DD w st) = [progressBar dd, [plainSeg (El.pTimes w st.clock)]]
178178

179179
------------------------------------------------------------------------
180180

@@ -206,19 +206,19 @@ playInfo DD{drawState=st} = mconcat
206206
numd = showInt $ length $ st.folders
207207

208208
-- | The top title bar: cursor position + play indicator + uptime + version.
209-
playTitle :: DrawData -> StringA
209+
playTitle :: DrawData -> Line
210210
playTitle dd@DD{drawWidth=w, drawState=st} =
211-
Fast (El.layoutLCR w (left, centerS, right)) st.uiStyle.titlebar
211+
[Seg st.uiStyle.titlebar $ El.layoutLCR w (left, centerS, right)]
212212
where
213213
left = " " <> playInfo dd
214214
centerS = pState dd ++ ' ' : pMode dd -- always 6 chars
215215
right = st.uptime <> " " <> El.pVersion <> " "
216216

217217
-- | The scrolling playlist (visible tracks).
218-
playList :: Int -> DrawData -> [StringA]
218+
playList :: Int -> DrawData -> [Line]
219219
playList buflen _ | buflen <= 0 = [] -- extra defense besides laziness
220220
playList buflen DD{ drawWidth=w, drawState=st } =
221-
list ++ replicate (buflen - length list) (Fast "" defaultSty)
221+
list ++ replicate (buflen - length list) []
222222

223223
where
224224
-- number of screens down, and then offset
@@ -253,13 +253,12 @@ playList buflen DD{ drawWidth=w, drawState=st } =
253253
where
254254
f sty = (sty, [s, spaces (w - indent - 1 - displayWidth s)])
255255

256-
drawIt :: (Maybe Int, (Style, [ByteString])) -> StringA
256+
drawIt :: (Maybe Int, (Style, [ByteString])) -> Line
257257
drawIt (Nothing, (sty, v)) =
258-
FancyS $ map (, sty) $ spaces (1 + indent) : v
259-
drawIt (Just i, (sty, v)) = FancyS
260-
$ (d, sty')
261-
: (spaces (indent + 1 - displayWidth d), sty')
262-
: map (, sty) v
258+
map (Seg sty) $ spaces (1 + indent) : v
259+
drawIt (Just i, (sty, v)) = Seg sty' d
260+
: Seg sty' (spaces (indent + 1 - displayWidth d))
261+
: map (Seg sty) v
263262
where
264263
sty' = if sty == sty2 || sty == sty3 then sty2 else sty1
265264
d = toMaxWidth (indent - 1) $ takeFileName (st.folders ! i).dname
@@ -282,7 +281,7 @@ renderModal st (h, w) mkr = do
282281
voffset = ((h - vislines) `div` 2) `max` 4
283282
Curses.wMove Curses.stdScr voffset hoffset
284283
for_ (take vislines modal') \t -> do
285-
drawLine $ Fast (toWidth mw t) st.uiStyle.modals
284+
drawSegment $ Seg st.uiStyle.modals (toWidth mw t)
286285
(y', _) <- Curses.getYX Curses.stdScr
287286
Curses.wMove Curses.stdScr (y'+1) hoffset
288287

@@ -309,25 +308,24 @@ redraw = Draw $ discardErrors do
309308
Curses.wMove Curses.stdScr (h-1) 0
310309
drawLine st.minibuffer
311310
when st.miniFocused do -- a fake cursor
312-
drawLine $ Fast " " st.uiStyle.blockcursor
311+
drawSegment $ Seg st.uiStyle.blockcursor " "
313312
fillLine
314313

315314
-- | Render whole lines without going below limit.
316-
drawFullLines :: Int -> Int -> [StringA] -> IO ()
315+
drawFullLines :: Int -> Int -> [Line] -> IO ()
317316
drawFullLines limit y ls =
318317
for_ (zip [y .. limit-1] ls) \ (y', t) -> do
319318
Curses.wMove Curses.stdScr y' 0
320319
drawLine t *> fillLine
321320

322321
------------------------------------------------------------------------
323322
-- | Draw a coloured (or not) string to the screen
324-
drawLine :: StringA -> IO ()
325-
drawLine (Fast ps sty) = drawSegment ps sty
326-
drawLine (FancyS ls) = traverse_ (uncurry drawSegment) ls
323+
drawLine :: Line -> IO ()
324+
drawLine = traverse_ drawSegment
327325

328326
-- | Write a single styled UTF-8 segment. Safe because C only reads the bytes.
329-
drawSegment :: ByteString -> Style -> IO ()
330-
drawSegment bs sty = withStyle sty $ void $
327+
drawSegment :: Segment -> IO ()
328+
drawSegment (Seg sty bs) = withStyle sty $ void $
331329
P.unsafeUseAsCStringLen bs \(cstr, len) ->
332330
waddnstr Curses.stdScr cstr (fromIntegral len)
333331

0 commit comments

Comments
 (0)