Skip to content

Commit 830d294

Browse files
Configurable history size; 61 selectable. (#126)
1 parent d7b9894 commit 830d294

7 files changed

Lines changed: 20 additions & 12 deletions

File tree

Base.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Data.Functor as X hiding (unzip)
2121
import Data.IORef as X
2222
import Data.List as X hiding ((!?))
2323
import Data.Maybe as X
24+
import Data.Sequence as X (Seq, (<|), (|>))
2425
import Data.String as X
2526
import Data.Traversable as X
2627
import Data.Version as X

Core.hs

Lines changed: 3 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+
, optHistSize :: Int -- ^ history size
6465
}
6566

6667
-- | Sets up state, spawns sub-threads, and starts player.
@@ -108,6 +109,7 @@ start opts (Playlist folders music) = do
108109
, modal = Nothing
109110
, playHist = mempty
110111
, searchHist = []
112+
, histSize = optHistSize opts
111113
, miniFocused = False
112114
, exiting = False
113115
, status = Stopped
@@ -414,7 +416,7 @@ runPlayOp op = do
414416
{ current = new
415417
, status = Playing
416418
, cursor = if current == cursor then new else cursor
417-
, playHist = Seq.take 36 $ (now, new) Seq.<| playHist
419+
, playHist = Seq.take histSize $ (now, new) <| playHist
418420
, id3 = Nothing
419421
}
420422
pure f

Keyboard.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
-- Copyright (c) 2019, 2023-2026 Galen Huntington
44
-- SPDX-License-Identifier: GPL-2.0-or-later
55

6-
module Keyboard (unkey, charToKey, Key(..)) where
6+
module Keyboard (unkey, charToKey, Key(..), historyKeys) where
77

88
import Base
99

1010
import Data.Map.Strict qualified as M
11+
import Data.Sequence qualified as Seq
1112
import UI.HSCurses.Curses (Key(..), decodeKey)
1213

1314
------------------------------------------------------------------------
@@ -29,3 +30,6 @@ keyCharMap = M.fromList [(charToKey c, c) | c <- ['\0' .. '\500']]
2930
unkey :: Key -> Char
3031
unkey k = fromMaybe '\0' $ M.lookup k keyCharMap
3132

33+
historyKeys :: Seq Char
34+
historyKeys = Seq.fromList $ ['0'..'9'] ++ ['a'..'z'] ++ filter (/='H') ['A'..'Z']
35+

Keymap.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Base
1616

1717
import Core
1818
import Config (package)
19-
import Keyboard (unkey, charToKey, Key(..))
19+
import Keyboard (unkey, charToKey, Key(..), historyKeys)
2020
import State (getsHS, modifyHS_, KeysHelp, Modal(..), HState(..))
2121
import Style (defaultSty, StringA(Fast))
2222
import UI qualified (getKey, resetui)
@@ -70,7 +70,7 @@ mainMode = KeyMap \c -> getsHS modal >>= \case
7070

7171

7272
historyKeyMap :: M.Map Char Int
73-
historyKeyMap = M.fromList $ zip (['0'..'9'] ++ ['a'..'z']) [0..]
73+
historyKeyMap = M.fromList $ zip (toList historyKeys) [0..]
7474

7575

7676
------------------------------------------------------------------------

State.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Playlist (FileArray, DirArray)
1414
import Style (StringA, UIStyle)
1515

1616
import Data.ByteString (hPut)
17-
import Data.Sequence (Seq)
1817
import System.Clock (TimeSpec(..))
1918
import System.IO (hFlush)
2019
import System.Process (ProcessHandle)
@@ -50,6 +49,7 @@ data HState = HState
5049
, searchHist :: ![String]
5150
, exiting :: !Bool -- let mpg123 die?
5251
, playHist :: !(Seq (TimeSpec, Int))
52+
, histSize :: Int
5353
, config :: !UIStyle
5454
}
5555

UI.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Syntax
2727
import Config
2828
import Width (displayWidth, toMaxWidth, toWidth)
2929
import UI.HSCurses.Curses qualified as Curses
30-
import Keyboard (unkey, charToKey)
30+
import Keyboard (unkey, charToKey, historyKeys)
3131

3232
import Data.Array ((!), bounds, Array)
3333
import Data.Array.Base (unsafeAt)
@@ -235,13 +235,15 @@ helpModal help swd = (wd, map showLine help) where
235235
------------------------------------------------------------------------
236236

237237
histModal :: HistDisplay -> ModalMaker
238+
histModal [] _ = let s = " No history " in (P.length s, [s])
238239
histModal hist swd = do
239240
let wd = commonModalWidth swd
240-
mtlen = maximum $ 0 : map (displayWidth . fst) hist
241+
mtlen = maximum $ map (displayWidth . fst) hist
241242
tlen = min (mtlen + 1) $ wd `div` 3
242-
(wd,) $ flip map (zip (['0'..'9']++['a'..'z']) hist) \ (c, (time, (_, song))) ->
243+
(wd, [
243244
let tstr = toMaxWidth tlen $ P.replicate (tlen - displayWidth time) ' ' <> time
244245
in mconcat [" ", P.singleton c, " ", tstr, " ", song]
246+
| (c, (time, (_, song))) <- zip (toList historyKeys ++ repeat ' ') hist ])
245247

246248
------------------------------------------------------------------------
247249

@@ -484,9 +486,7 @@ renderModals st sz =
484486
ExitModal -> exitModal
485487

486488
------------------------------------------------------------------------
487-
--
488489
-- | Draw the screen
489-
--
490490
redraw :: Draw
491491
redraw = Draw $ discardErrors {- TODO what errors are discarded? -} do
492492
st <- getsHS id -- another refresh could be triggered?
@@ -516,9 +516,7 @@ redraw = Draw $ discardErrors {- TODO what errors are discarded? -} do
516516
-- todo rendering bug here when deleting backwards in minibuffer
517517

518518
------------------------------------------------------------------------
519-
--
520519
-- | Draw a coloured (or not) string to the screen
521-
--
522520
drawLine :: StringA -> IO ()
523521
drawLine (Fast ps sty) = drawSegment ps sty
524522
drawLine (FancyS ls) = traverse_ (uncurry drawSegment) ls

app/Main.hs

Lines changed: 3 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+
<*> option auto (
58+
long "history" <> short 'h' <> metavar "NUM" <> value 61
59+
<> help "Size of play history, up to 61 selectable" <> showDefault)
5760
files = some $ argument (UTF8.fromString <$> str) (metavar "FILE|DIR...")
5861

5962
parserInfo :: ParserInfo (Options, [ByteString])

0 commit comments

Comments
 (0)