From d843df76d2fbf6c4dcdcb044d91fc3b94e0341a7 Mon Sep 17 00:00:00 2001 From: slotThe Date: Thu, 2 Jan 2020 22:07:47 +0100 Subject: Add 'WeatherOpts' type and first option to weather plugin The first option applied here is a default value for a field that's not always reported to be there, namely the 'weather' field. It now defaults to saying "normal" instead of displaying an empty string. --- src/Xmobar/Plugins/Monitors.hs | 4 +-- src/Xmobar/Plugins/Monitors/Weather.hs | 52 ++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Xmobar/Plugins/Monitors.hs b/src/Xmobar/Plugins/Monitors.hs index cac4635..f79b406 100644 --- a/src/Xmobar/Plugins/Monitors.hs +++ b/src/Xmobar/Plugins/Monitors.hs @@ -164,8 +164,8 @@ instance Exec Monitors where start (TopProc a r) = startTop a r start (TopMem a r) = runM a topMemConfig runTopMem r #ifdef WEATHER - start (Weather s a r) = runMD (a ++ [s]) weatherConfig runWeather r weatherReady - start (WeatherX s c a r) = runMD (a ++ [s]) weatherConfig (runWeather' c) r weatherReady + start (Weather s a r) = runMD (s : a) weatherConfig runWeather r weatherReady + start (WeatherX s c a r) = runMD (s : a) weatherConfig (runWeather' c) r weatherReady #endif start (Thermal z a r) = runM (a ++ [z]) thermalConfig runThermal r start (ThermalZone z a r) = diff --git a/src/Xmobar/Plugins/Monitors/Weather.hs b/src/Xmobar/Plugins/Monitors/Weather.hs index 6ac1f4b..e31c3c6 100644 --- a/src/Xmobar/Plugins/Monitors/Weather.hs +++ b/src/Xmobar/Plugins/Monitors/Weather.hs @@ -26,6 +26,37 @@ import qualified Data.ByteString.Lazy.Char8 as B import Data.Char (toLower) import Text.ParserCombinators.Parsec +import System.Console.GetOpt + ( ArgDescr(ReqArg) + , ArgOrder(Permute) + , OptDescr(Option) + , getOpt + ) + + +-- | Options the user may specify. +data WeatherOpts = WeatherOpts + { weatherString :: String + } deriving Show + +-- | Default values for options. +defaultOpts :: WeatherOpts +defaultOpts = WeatherOpts + { weatherString = "" + } + +-- | Apply options. +options :: [OptDescr (WeatherOpts -> WeatherOpts)] +options = + [ Option "w" ["weathers"] (ReqArg (\s o -> o { weatherString = s }) "") "" + ] + +-- | Try to parse arguments from the config file and apply them. +parseOpts :: [String] -> IO WeatherOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, [] ) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs weatherConfig :: IO MConfig weatherConfig = mkMConfig @@ -203,15 +234,25 @@ formatSk :: Eq p => [(p, p)] -> p -> p formatSk ((a,b):sks) sk = if a == sk then b else formatSk sks sk formatSk [] sk = sk -formatWeather :: [(String,String)] -> [WeatherInfo] -> Monitor String -formatWeather sks [WI st ss y m d h (WindInfo wc wa wm wk wkh wms) v sk we tC tF dC dF r p] = +formatWeather + :: WeatherOpts -- ^ Formatting options from the cfg file + -> [(String,String)] -- ^ 'SkyConditionS' for 'WeatherX' + -> [WeatherInfo] -- ^ The actual weather info + -> Monitor String +formatWeather opts sks [WI st ss y m d h (WindInfo wc wa wm wk wkh wms) v sk we tC tF dC dF r p] = do cel <- showWithColors show tC far <- showWithColors show tF let sk' = formatSk sks (map toLower sk) + we' = showWeather (weatherString opts) we parseTemplate [st, ss, y, m, d, h, wc, wa, wm, wk, wkh - , wms, v, sk, sk', we, cel, far + , wms, v, sk, sk', we', cel, far , show dC, show dF, show r , show p ] -formatWeather _ _ = getConfigValue naString +formatWeather _ _ _ = getConfigValue naString + +-- | Show the 'weather' field with a default string in case it was empty. +showWeather :: String -> String -> String +showWeather "" d = d +showWeather s _ = s runWeather :: [String] -> Monitor String runWeather = runWeather' [] @@ -219,8 +260,9 @@ runWeather = runWeather' [] runWeather' :: [(String, String)] -> [String] -> Monitor String runWeather' sks args = do d <- io $ getData $ head args + o <- io $ parseOpts args i <- io $ runP parseData d - formatWeather sks i + formatWeather o sks i weatherReady :: [String] -> Monitor Bool weatherReady str = do -- cgit v1.2.3