From 075955f7253d1f3c0287f825ff4159f221a07630 Mon Sep 17 00:00:00 2001 From: Peter Rice Date: Mon, 9 Sep 2019 12:27:27 -0400 Subject: [PATCH] create skeleton of config system --- Config.hs | 10 ++++++++++ I3blocks/ButtonMap.hs | 33 +++++++++++++++++++++++++++++++++ I3blocks/Config.hs | 31 ------------------------------- I3blocks/Main.hs | 18 ++++++++++++++++-- README.md | 4 ++-- mpd-status.cabal | 7 ++++--- 6 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 Config.hs create mode 100644 I3blocks/ButtonMap.hs delete mode 100644 I3blocks/Config.hs diff --git a/Config.hs b/Config.hs new file mode 100644 index 0000000..f8b3710 --- /dev/null +++ b/Config.hs @@ -0,0 +1,10 @@ +module Config + ( Config(..) + ) +where + +import Network.MPD ( PlaylistName ) + +data Config = Config + { confVolStep :: Int + , confAlbumShufflePlaylist :: Maybe PlaylistName } diff --git a/I3blocks/ButtonMap.hs b/I3blocks/ButtonMap.hs new file mode 100644 index 0000000..6cc3472 --- /dev/null +++ b/I3blocks/ButtonMap.hs @@ -0,0 +1,33 @@ +module I3blocks.ButtonMap + ( buttonToOp + ) +where + +import Config +import Operation +import I3blocks.Click + +import Control.Monad.Reader ( Reader + , asks + ) + +buttonToOp :: Button -> Maybe (Reader Config Operation) +buttonToOp = albumShuffleButtons + +defaultButtons :: Button -> Maybe (Reader Config Operation) +defaultButtons ScrollUp = Just $ VolumeUp <$> asks confVolStep +defaultButtons ScrollDown = Just $ VolumeDown <$> asks confVolStep +defaultButtons b = pure <$> simpleButtons b + where + simpleButtons LeftClick = Just Toggle + simpleButtons MiddleClick = Just AllRandom + simpleButtons RightClick = Just Stop + simpleButtons Back = Just Previous + simpleButtons Forward = Just Next + simpleButtons _ = Nothing + +albumShuffleButtons :: Button -> Maybe (Reader Config Operation) +albumShuffleButtons MiddleClick = + Just $ AlbumShuffle <$> asks confAlbumShufflePlaylist +albumShuffleButtons Forward = Just $ pure NextAlbum +albumShuffleButtons b = defaultButtons b diff --git a/I3blocks/Config.hs b/I3blocks/Config.hs deleted file mode 100644 index 688555f..0000000 --- a/I3blocks/Config.hs +++ /dev/null @@ -1,31 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module I3blocks.Config - ( buttonToOp - , volStep - ) -where - -import Operation -import I3blocks.Click - -import Network.MPD ( PlaylistName(..) ) - -volStep :: Int -volStep = 5 - -buttonToOp :: Button -> Maybe Operation -buttonToOp LeftClick = Just Toggle ---buttonToOp MiddleClick = Just AllRandom -buttonToOp RightClick = Just Stop -buttonToOp ScrollUp = Just $ VolumeUp volStep -buttonToOp ScrollDown = Just $ VolumeDown volStep -buttonToOp Back = Just Previous ---buttonToOp Forward = Just Next - --- TODO maybe there should be some notion of album mode vs single mode -buttonToOp MiddleClick = - Just $ AlbumShuffle (Just $ PlaylistName "album-shuffle") -buttonToOp Forward = Just NextAlbum - -buttonToOp _ = Nothing diff --git a/I3blocks/Main.hs b/I3blocks/Main.hs index f6498fa..9217cf1 100644 --- a/I3blocks/Main.hs +++ b/I3blocks/Main.hs @@ -9,6 +9,9 @@ import System.IO ( stdout , BufferMode(LineBuffering) ) +import Control.Monad.Reader ( Reader + , runReader + ) import Data.Aeson ( decodeStrict ) import qualified Data.ByteString as B ( getLine ) @@ -18,9 +21,11 @@ import qualified Data.Text.IO as T ( putStrLn ) import Network.MPD +import Config import Operation import I3blocks.Block -import I3blocks.Config +import I3blocks.ButtonMap +import I3blocks.Click main :: IO () main = do @@ -30,7 +35,16 @@ main = do tid <- forkIO . forever . run $ idle [PlayerS] >> block b <- decodeStrict <$> B.getLine killThread tid - withMPD . maybe (return ()) op $ b >>= buttonToOp + withMPD . maybe (return ()) op $ toOp b + where + toOp :: Maybe Button -> Maybe Operation + toOp b = conf <$> (b >>= buttonToOp) + conf :: Reader Config Operation -> Operation + conf = flip + runReader + Config { confVolStep = 5 + , confAlbumShufflePlaylist = Just $ PlaylistName "album-shuffle" + } run :: MPD T.Text -> IO () run = withMPD >=> either print T.putStrLn diff --git a/README.md b/README.md index 177e4dc..7e58569 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ By default, left click plays and pauses, right click stops, the scroll wheel changes the volume, middle click shuffles all albums, and, if your mouse has them, the back button returns to the previous song and the forward button skips to the next album (this can be changed to go to the next song by editing -`I3blocks/Config.hs` to have `buttonToOp Forward = Just Next` instead of `Just -NextAlbum`). +`I3blocks/ButtonMap.hs` and changing `buttonToOp = albumShuffleButtons` to +`buttonToOp = defaultButtons`). ### Cool things about `mpd-status` * Automatically updates when the song or volume change, whether the change is diff --git a/mpd-status.cabal b/mpd-status.cabal index 8646464..999c211 100644 --- a/mpd-status.cabal +++ b/mpd-status.cabal @@ -22,11 +22,12 @@ executable mpd-status other-extensions: LambdaCase ghc-options: -Wall main-is: I3blocks/Main.hs - other-modules: I3blocks.Block - I3blocks.Click - I3blocks.Config + other-modules: Config Operation Shuffle + I3blocks.Block + I3blocks.ButtonMap + I3blocks.Click build-depends: base >=4.11 && <5.0 , array >=0.5.0.0 && <0.6.0.0 -- 2.45.2