From 2dec7b2ff2d064918b57c73879eb83a9ed93e4be Mon Sep 17 00:00:00 2001 From: jao Date: Tue, 21 Apr 2020 04:19:37 +0100 Subject: MPD: catch errors earlier so that reconnections are allowed --- src/Xmobar/Plugins/Monitors/MPD.hs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Xmobar/Plugins/Monitors/MPD.hs b/src/Xmobar/Plugins/Monitors/MPD.hs index 3274c42..3bbae5d 100644 --- a/src/Xmobar/Plugins/Monitors/MPD.hs +++ b/src/Xmobar/Plugins/Monitors/MPD.hs @@ -20,14 +20,17 @@ import Xmobar.Plugins.Monitors.Common import System.Console.GetOpt import qualified Network.MPD as M import Control.Concurrent (threadDelay) +import Control.Monad.Except (catchError) + +templateVars :: [String] +templateVars = [ "bar", "vbar", "ipat", "state", "statei", "volume", "length" + , "lapsed", "remaining", "plength", "ppos", "flags", "file" + , "name", "artist", "composer", "performer" + , "album", "title", "track", "genre", "date" + ] mpdConfig :: IO MConfig -mpdConfig = mkMConfig "MPD: " - [ "bar", "vbar", "ipat", "state", "statei", "volume", "length" - , "lapsed", "remaining", "plength", "ppos", "flags", "file" - , "name", "artist", "composer", "performer" - , "album", "title", "track", "genre", "date" - ] +mpdConfig = mkMConfig "MPD: " templateVars data MOpts = MOpts { mPlaying :: String @@ -60,7 +63,8 @@ options = ] withMPD :: MOpts -> M.MPD a -> IO (M.Response a) -withMPD opts = M.withMPD_ (mHost opts) (mPort opts) +withMPD opts a = + M.withMPD_ (mHost opts) (mPort opts) a `catchError` (\_ -> return (Left M.NoMPD)) runMPD :: [String] -> Monitor String runMPD args = do @@ -74,7 +78,7 @@ mpdWait :: IO () mpdWait = do status <- M.withMPD $ M.idle [M.PlayerS, M.MixerS, M.OptionsS] case status of - Left _ -> threadDelay 10000000 + Left _ -> threadDelay 5000 _ -> return () mpdReady :: [String] -> Monitor Bool @@ -91,7 +95,7 @@ mpdReady args = do parseMPD :: M.Response M.Status -> M.Response (Maybe M.Song) -> MOpts -> Monitor [String] -parseMPD (Left e) _ _ = return $ show e:replicate 19 "" +parseMPD (Left _) _ _ = return $ "N/A": repeat "" parseMPD (Right st) song opts = do songData <- parseSong song bar <- showPercentBar (100 * b) b -- cgit v1.2.3