Skip to content

Commit 95dc5b5

Browse files
Add option for initial play mode. (#128)
1 parent 7ee8ca9 commit 95dc5b5

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

Core.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ mp3Tool = "mpg123"
6161
data Options = Options
6262
{ optPaused :: !Bool -- ^ start in a paused state
6363
, optConfigPath :: !(Maybe FilePath) -- ^ override the style.conf location
64+
, optPlayMode :: Maybe Mode -- ^ play mode
6465
, optHistSize :: Int -- ^ history size
6566
}
6667

@@ -75,7 +76,7 @@ start opts (Playlist folders music) = do
7576
exitImmediately $ ExitFailure 1
7677
bootTime <- getMonoTime
7778
let size = length music
78-
mode <- readState
79+
mode <- maybe readState pure (optPlayMode opts)
7980
gen <- newStdGen
8081
let (current, randomGen) =
8182
if mode == Random then randomR (0, size-1) gen else (0, gen)

UI.hs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ type ModalMaker = Int -> (Int, [ByteString])
170170

171171
------------------------------------------------------------------------
172172

173-
-- | The three lines of the play-mode widget.
173+
-- | The three lines of the play info widget.
174174
playScreen :: DrawData -> [StringA]
175175
playScreen dd = [pPlaying dd, progressBar dd, pTimes dd]
176176

@@ -306,27 +306,19 @@ pVersion = P.pack versinfo
306306
pTime :: DrawData -> ByteString
307307
pTime = uptime . drawState
308308

309-
-- | Play mode
310-
pMode :: DrawData -> String
311-
pMode dd = case status (drawState dd) of
309+
-- | Play state
310+
pState :: DrawData -> String
311+
pState dd = case status (drawState dd) of
312312
Stopped -> ""
313313
Paused -> ""
314314
Playing -> ""
315315

316-
-- | Loop, normal, or random
317-
pMode2 :: DrawData -> String
318-
pMode2 dd = case mode (drawState dd) of
319-
Random -> "rand"
320-
Loop -> "loop"
321-
Once -> "once"
322-
Single -> "sing"
316+
-- | Play mode
317+
pMode :: DrawData -> String
318+
pMode dd = take 4 $ map toLower $ show $ mode $ drawState dd
323319

324320
------------------------------------------------------------------------
325321

326-
-- | The two play-mode glyphs (e.g. "▶ rand") rendered together.
327-
playModes :: DrawData -> String
328-
playModes dd = pMode dd ++ ' ' : pMode2 dd
329-
330322
-- | "x/n dir(s) y/m file(s)" cursor position read-out.
331323
playInfo :: DrawData -> ByteString
332324
playInfo dd = mconcat
@@ -348,32 +340,31 @@ playInfo dd = mconcat
348340
curd = tobs $ 1 + mydir
349341
numd = tobs $ 1 + snd (bounds $ folders st)
350342

351-
-- | The top title bar: cursor position + play modes + uptime + version.
343+
-- | The top title bar: cursor position + play indicator + uptime + version.
352344
playTitle :: DrawData -> StringA
353345
playTitle dd =
354346
FancyS $ map (, hl)
355347
if gap >= 2
356-
then [mconcat [" ", inf, spaces gapl], modesBS,
348+
then [mconcat [" ", inf, spaces gapl], indic,
357349
mconcat [spaces gapr, time, " ", ver, " "]]
358-
else let gap' = x - modlen; gapl' = gap' `div` 2
350+
else let gap' = x - indicl; gapl' = gap' `div` 2
359351
in if gap' >= 2
360-
then [spaces gapl', modesBS, spaces $ gap' - gapl']
361-
else [" ", u $ take (x-2) modes, " "]
352+
then [spaces gapl', indic, spaces $ gap' - gapl']
353+
else [" ", P.take (x-2) indic, " "]
362354
where
363355
inf = playInfo dd
364356
time = pTime dd
365-
modes = playModes dd
357+
indic = u $ pState dd ++ ' ' : pMode dd
366358
ver = pVersion
367-
modesBS = u modes
368359

369360
x = sizeW $ drawSize dd
370361
lsize = 1 + P.length inf
371362
rsize = 2 + P.length time + P.length ver
372-
side = (x - modlen) `div` 2
373-
gap = x - modlen - lsize - rsize
363+
side = (x - indicl) `div` 2
364+
gap = x - indicl - lsize - rsize
374365
gapl = 1 `max` ((side - lsize) `min` (gap - 1))
375366
gapr = gap - gapl
376-
modlen = 6 -- length modes
367+
indicl = 6 -- length indic
377368
hl = titlebar . config $ drawState dd
378369

379370
-- | The scrolling playlist (title + visible tracks + minibuffer).

app/Main.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ invocation = (,) <$> opts <*> files
5454
<*> optional (strOption -- temporarily internal since feature needs work
5555
(long "config" <> short 'c' <> metavar "FILE" <> internal
5656
<> help "Read this config file instead of the XDG default"))
57+
<*> optional (option (maybeReader prefixMatch) (
58+
long "mode" <> short 'm' <> metavar "MODE"
59+
<> help "Initial play mode (default: last selected, or once)"))
5760
<*> option auto (
5861
long "history" <> short 'h' <> metavar "NUM" <> value 61
5962
<> help "Size of play history, up to 61 selectable" <> showDefault)
@@ -68,6 +71,14 @@ parserInfo = info (invocation <**> versionOpt <**> helper) $
6871
versionOpt = infoOption Config.versinfo
6972
(hidden <> long "version" <> short 'V' <> help "Show version information")
7073

74+
-- XXX should this have tests?
75+
prefixMatch :: (Enum a, Bounded a, Show a) => String -> Maybe a
76+
prefixMatch s =
77+
case [ x | x <- [minBound .. maxBound], s' `isPrefixOf` map toLower (show x) ] of
78+
[x] -> Just x
79+
_ -> Nothing
80+
where s' = map toLower s
81+
7182
------------------------------------------------------------------------
7283

7384
main :: IO ()

0 commit comments

Comments
 (0)