~pvsr/mpd-status

eaaddd4ec188c39cfa9d2e23a40e8d13f94cee7d — Peter Rice 1 year, 3 months ago 29db5be
hacky album shuffling
3 files changed, 30 insertions(+), 2 deletions(-)

M Operation.hs
A Shuffle.hs
M mpd-status.cabal
M Operation.hs => Operation.hs +5 -2
@@ 6,9 6,12 @@ import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Data.List (nub)

import Control.Monad.Trans (liftIO)
import Network.MPD
import Network.MPD.Commands.Extensions

import Shuffle

data Operation = Toggle
               | Stop
               | VolumeUp Int


@@ 29,8 32,8 @@ op (VolumeDown volStep) = status >>= maybe (return ()) (setVolume . dec volStep)
op Mute = setVolume 0
op Previous = previous
op Next = next
op AlbumShuffle = clear >> consume True >> random False >> listPlaylistInfo "album-shuffle" >>= mapM_ findAdd . (queries . uniqAlbums) >> play Nothing
  -- TODO shuffle the results of uniqAlbums
-- really pining for the elegance of `mpc playlist -f %album% album-shuffle | uniq | sort -R` here
op AlbumShuffle = clear >> consume True >> random False >> listPlaylistInfo "album-shuffle" >>= (liftIO . Shuffle.shuffle . (queries . uniqAlbums)) >>= mapM_ findAdd >> play Nothing
  where uniqAlbums = nub . concat . mapMaybe (M.lookup Album . sgTags)
        queries = map (Album =?)


A Shuffle.hs => Shuffle.hs +21 -0
@@ 0,0 1,21 @@
module Shuffle where

import System.Random
import Data.Array.IO
import Control.Monad

-- this is so stupid
-- copied from https://wiki.haskell.org/Random_shuffle
shuffle :: [a] -> IO [a]
shuffle xs = do
        ar <- newArray n xs
        forM [1..n] $ \i -> do
            j <- randomRIO (i,n)
            vi <- readArray ar i
            vj <- readArray ar j
            writeArray ar j vi
            return vj
  where
    n = length xs
    newArray :: Int -> [a] -> IO (IOArray Int a)
    newArray n xs =  newListArray (1, n) xs

M mpd-status.cabal => mpd-status.cabal +4 -0
@@ 25,11 25,15 @@ executable mpd-status
  other-modules:       Click
                       Config
                       Operation
                       Shuffle

  build-depends:       base >=4.11 && <5.0
                     , array >=0.5.0.0 && <0.6.0.0
                     , aeson >=1.4 && <1.5
                     , bytestring >=0.10 && <0.11
                     , containers >=0.5 && <0.7
                     , libmpd >=0.9 && <0.10
                     , mtl >2.2.0 && <2.3.0
                     , random >=1.1 && <1.2
                     , text >=1.2 && <1.3
  default-language:    Haskell2010