diff options
author | slotThe <soliditsallgood@tuta.io> | 2020-01-04 21:20:56 +0100 |
---|---|---|
committer | jao <jao@gnu.org> | 2020-01-04 21:21:40 +0000 |
commit | 940be3bb32ed1f11c46ede98d51516998b17e128 (patch) | |
tree | 6d0c942c10efc64514dd419f746f631aec19c935 /src/Xmobar | |
parent | 2ec513d2e193998958ad5bf4a5f7280f595792e9 (diff) | |
download | xmobar-940be3bb32ed1f11c46ede98d51516998b17e128.tar.gz xmobar-940be3bb32ed1f11c46ede98d51516998b17e128.tar.bz2 |
Replace parseOpts with a generic function
Diffstat (limited to 'src/Xmobar')
-rw-r--r-- | src/Xmobar/Plugins/MBox.hs | 9 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Mail.hs | 10 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Alsa.hs | 13 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Batt.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Bright.hs | 9 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Common/Parsers.hs | 13 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Cpu.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Mem.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/MultiCoreTemp.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/MultiCpu.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Net.hs | 10 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Volume.hs | 8 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Weather.hs | 16 | ||||
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Wireless.hs | 8 |
14 files changed, 33 insertions, 103 deletions
diff --git a/src/Xmobar/Plugins/MBox.hs b/src/Xmobar/Plugins/MBox.hs index 311f26e..dc30972 100644 --- a/src/Xmobar/Plugins/MBox.hs +++ b/src/Xmobar/Plugins/MBox.hs @@ -19,6 +19,7 @@ import Prelude import Xmobar.Run.Exec #ifdef INOTIFY +import Xmobar.Plugins.Monitors.Common (parseOptsWith) import Xmobar.System.Utils (changeLoop, expandHome) import Control.Monad (when) @@ -63,12 +64,6 @@ options = , Option "s" ["suffix"] (ReqArg (\x o -> o { oSuffix = x }) "") "" ] -parseOptions :: [String] -> IO Options -parseOptions args = - case getOpt Permute options args of - (o, _, []) -> return $ foldr id defaults o - (_, _, errs) -> ioError . userError $ concat errs - #else import System.IO #endif @@ -86,7 +81,7 @@ instance Exec MBox where " but the MBox plugin requires it" #else start (MBox boxes args _) cb = do - opts <- parseOptions args + opts <- parseOptsWith options defaults args let showAll = oAll opts prefix = oPrefix opts suffix = oSuffix opts diff --git a/src/Xmobar/Plugins/Mail.hs b/src/Xmobar/Plugins/Mail.hs index ee4a119..36e85b8 100644 --- a/src/Xmobar/Plugins/Mail.hs +++ b/src/Xmobar/Plugins/Mail.hs @@ -18,6 +18,7 @@ module Xmobar.Plugins.Mail(Mail(..),MailX(..)) where import Xmobar.Run.Exec #ifdef INOTIFY +import Xmobar.Plugins.Monitors.Common (parseOptsWith) import Xmobar.System.Utils (expandHome, changeLoop) import Control.Monad @@ -64,13 +65,6 @@ options = , Option "s" ["suffix"] (ReqArg (\x o -> o { oSuffix = x }) "") "" ] -parseOptions :: [String] -> IO MOptions -parseOptions args = - case getOpt Permute options args of - (o, _, []) -> return $ foldr id defaults o - (_, _, errs) -> ioError . userError $ concat errs - - -- | A list of mail box names and paths to maildirs. data Mail = Mail [(String, FilePath)] String deriving (Read, Show) @@ -92,7 +86,7 @@ instance Exec MailX where #else start (MailX ms args _) cb = do vs <- mapM (const $ newTVarIO S.empty) ms - opts <- parseOptions args + opts <- parseOptsWith options defaults args let prefix = oPrefix opts suffix = oSuffix opts dir = oDir opts diff --git a/src/Xmobar/Plugins/Monitors/Alsa.hs b/src/Xmobar/Plugins/Monitors/Alsa.hs index 00f7353..dfc7329 100644 --- a/src/Xmobar/Plugins/Monitors/Alsa.hs +++ b/src/Xmobar/Plugins/Monitors/Alsa.hs @@ -57,17 +57,12 @@ options = where modifyVolumeOpts f o = o { aoVolumeOpts = f (aoVolumeOpts o) } -parseOpts :: [String] -> IO AlsaOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - +-- | Drop generic Monitor args first, then apply 'parseOptsWith' in order to +-- parse everything. parseOptsIncludingMonitorArgs :: [String] -> IO AlsaOpts parseOptsIncludingMonitorArgs args = - -- Drop generic Monitor args first case getOpt Permute [] args of - (_, args', _) -> parseOpts args' + (_, args', _) -> parseOptsWith options defaultOpts args' startAlsaPlugin :: String -> String -> [String] -> (String -> IO ()) -> IO () startAlsaPlugin mixerName controlName args cb = do @@ -80,7 +75,7 @@ startAlsaPlugin mixerName controlName args cb = do -- it would have to inline 'runMBD', 'doArgs' and 'parseOpts' to see -- it, which probably isn't going to happen with the default -- optimization settings). - opts2 <- io $ parseOpts args2 + opts2 <- io $ parseOptsWith options defaultOpts args2 Volume.runVolumeWith (aoVolumeOpts opts2) mixerName controlName withMonitorWaiter mixerName (aoAlsaCtlPath opts) cb $ \wait_ -> diff --git a/src/Xmobar/Plugins/Monitors/Batt.hs b/src/Xmobar/Plugins/Monitors/Batt.hs index 3f407b6..8cd196d 100644 --- a/src/Xmobar/Plugins/Monitors/Batt.hs +++ b/src/Xmobar/Plugins/Monitors/Batt.hs @@ -103,12 +103,6 @@ options = , Option "" ["highs"] (ReqArg (\x o -> o { highString = x }) "") "" ] -parseOpts :: [String] -> IO BattOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - data Status = Charging | Discharging | Full | Idle | Unknown deriving (Read, Eq) data Result = Result Float Float Float Status | NA @@ -245,7 +239,7 @@ runBatt = runBatt' ["BAT", "BAT0", "BAT1", "BAT2"] runBatt' :: [String] -> [String] -> Monitor String runBatt' bfs args = do - opts <- io $ parseOpts args + opts <- io $ parseOptsWith options defaultOpts args let sp = incPerc opts c <- io $ readBatteries opts =<< mapM batteryFiles bfs suffix <- getConfigValue useSuffix diff --git a/src/Xmobar/Plugins/Monitors/Bright.hs b/src/Xmobar/Plugins/Monitors/Bright.hs index fe72219..5f0cd0d 100644 --- a/src/Xmobar/Plugins/Monitors/Bright.hs +++ b/src/Xmobar/Plugins/Monitors/Bright.hs @@ -44,13 +44,6 @@ options = [ Option "D" ["device"] (ReqArg (\x o -> o { subDir = x }) "") "" o { curBrightIconPattern = Just $ parseIconPattern x }) "") "" ] --- from Batt.hs -parseOpts :: [String] -> IO BrightOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - sysDir :: FilePath sysDir = "/sys/class/backlight/" @@ -75,7 +68,7 @@ brightFiles opts = do runBright :: [String] -> Monitor String runBright args = do - opts <- io $ parseOpts args + opts <- io $ parseOptsWith options defaultOpts args f <- io $ brightFiles opts c <- io $ readBright f case f of diff --git a/src/Xmobar/Plugins/Monitors/Common/Parsers.hs b/src/Xmobar/Plugins/Monitors/Common/Parsers.hs index 4b87a10..7a813e5 100644 --- a/src/Xmobar/Plugins/Monitors/Common/Parsers.hs +++ b/src/Xmobar/Plugins/Monitors/Common/Parsers.hs @@ -24,12 +24,14 @@ module Xmobar.Plugins.Monitors.Common.Parsers ( runP , skipTillString , parseTemplate , parseTemplate' + , parseOptsWith ) where import Xmobar.Plugins.Monitors.Common.Types import Control.Applicative ((<$>)) import qualified Data.Map as Map +import System.Console.GetOpt (ArgOrder(Permute), OptDescr, getOpt) import Text.ParserCombinators.Parsec runP :: Parser [a] -> String -> IO [a] @@ -150,3 +152,14 @@ combine m ((s,ts,ss):xs) = Nothing -> return $ "<" ++ ts ++ ">" Just r -> let f "" = r; f n = n; in f <$> parseTemplate' r m return $ s ++ str ++ ss ++ next + +-- | Try to parse arguments from the config file and apply them to Options. +parseOptsWith + :: [OptDescr (opts -> opts)] -- ^ Options that are specifiable + -> opts -- ^ Default options to use as a fallback + -> [String] -- ^ Actual arguments given + -> IO opts +parseOptsWith options defaultOpts argv = + case getOpt Permute options argv of + (o, _, [] ) -> pure $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs diff --git a/src/Xmobar/Plugins/Monitors/Cpu.hs b/src/Xmobar/Plugins/Monitors/Cpu.hs index 6befe7d..157631d 100644 --- a/src/Xmobar/Plugins/Monitors/Cpu.hs +++ b/src/Xmobar/Plugins/Monitors/Cpu.hs @@ -35,12 +35,6 @@ options = o { loadIconPattern = Just $ parseIconPattern x }) "") "" ] -parseOpts :: [String] -> IO CpuOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - cpuConfig :: IO MConfig cpuConfig = mkMConfig "Cpu: <total>%" @@ -77,7 +71,7 @@ formatCpu opts xs = do runCpu :: CpuDataRef -> [String] -> Monitor String runCpu cref argv = do c <- io (parseCpu cref) - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv l <- formatCpu opts c parseTemplate l diff --git a/src/Xmobar/Plugins/Monitors/Mem.hs b/src/Xmobar/Plugins/Monitors/Mem.hs index d69921b..91ed3e6 100644 --- a/src/Xmobar/Plugins/Monitors/Mem.hs +++ b/src/Xmobar/Plugins/Monitors/Mem.hs @@ -41,12 +41,6 @@ options = o { availableIconPattern = Just $ parseIconPattern x }) "") "" ] -parseOpts :: [String] -> IO MemOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - memConfig :: IO MConfig memConfig = mkMConfig "Mem: <usedratio>% (<cache>M)" -- template @@ -91,6 +85,6 @@ formatMem _ _ = replicate 10 `fmap` getConfigValue naString runMem :: [String] -> Monitor String runMem argv = do m <- io parseMEM - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv l <- formatMem opts m parseTemplate l diff --git a/src/Xmobar/Plugins/Monitors/MultiCoreTemp.hs b/src/Xmobar/Plugins/Monitors/MultiCoreTemp.hs index 90fd6d0..d8b7a3e 100644 --- a/src/Xmobar/Plugins/Monitors/MultiCoreTemp.hs +++ b/src/Xmobar/Plugins/Monitors/MultiCoreTemp.hs @@ -60,12 +60,6 @@ options = [ Option [] ["max-icon-pattern"] "" ] --- | Parse Arguments and apply them to Options. -parseOpts :: [String] -> IO CTOpts -parseOpts argv = case getOpt Permute options argv of - (opts , _ , [] ) -> return $ foldr id defaultOpts opts - (_ , _ , errs) -> ioError . userError $ concat errs - -- | Generate Config with a default template and options. cTConfig :: IO MConfig cTConfig = mkMConfig cTTemplate cTOptions @@ -157,7 +151,7 @@ formatCT opts cTs = do let CTOpts { mintemp = minT runCT :: [String] -> Monitor String runCT argv = do cTs <- io parseCT - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv l <- formatCT opts cTs parseTemplate l diff --git a/src/Xmobar/Plugins/Monitors/MultiCpu.hs b/src/Xmobar/Plugins/Monitors/MultiCpu.hs index 5e5c273..936bb2e 100644 --- a/src/Xmobar/Plugins/Monitors/MultiCpu.hs +++ b/src/Xmobar/Plugins/Monitors/MultiCpu.hs @@ -47,12 +47,6 @@ options = , Option "" ["contiguous-icons"] (NoArg (\o -> o {contiguous = True})) "" ] -parseOpts :: [String] -> IO MultiCpuOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - variables :: [String] variables = ["bar", "vbar","ipat","total","user","nice","system","idle"] vNum :: Int @@ -122,7 +116,7 @@ formatAutoCpus opts xs = runMultiCpu :: CpuDataRef -> [String] -> Monitor String runMultiCpu cref argv = do c <- io $ parseCpuData cref - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv l <- formatMultiCpus opts c a <- formatAutoCpus opts l parseTemplate $ a ++ l diff --git a/src/Xmobar/Plugins/Monitors/Net.hs b/src/Xmobar/Plugins/Monitors/Net.hs index 6acec62..db11724 100644 --- a/src/Xmobar/Plugins/Monitors/Net.hs +++ b/src/Xmobar/Plugins/Monitors/Net.hs @@ -68,12 +68,6 @@ options = o { onlyDevList = Just $ parseDevList x }) "") "" ] -parseOpts :: [String] -> IO NetOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - data UnitPerSec = Bs | KBs | MBs | GBs deriving (Eq,Enum,Ord) data NetValue = NetValue Float UnitPerSec deriving (Eq,Show) @@ -188,7 +182,7 @@ parseNet nref nd = do runNet :: NetDevRef -> String -> [String] -> Monitor String runNet nref i argv = do dev <- io $ parseNet nref i - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv printNet opts dev parseNets :: [(NetDevRef, String)] -> IO [NetDevRate] @@ -196,7 +190,7 @@ parseNets = mapM $ uncurry parseNet runNets :: [(NetDevRef, String)] -> [String] -> Monitor String runNets refs argv = do - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv dev <- io $ parseActive $ filterRefs opts refs printNet opts dev where parseActive refs' = fmap selectActive (parseNets refs') diff --git a/src/Xmobar/Plugins/Monitors/Volume.hs b/src/Xmobar/Plugins/Monitors/Volume.hs index b9bdbc0..de07aa6 100644 --- a/src/Xmobar/Plugins/Monitors/Volume.hs +++ b/src/Xmobar/Plugins/Monitors/Volume.hs @@ -101,12 +101,6 @@ options = , Option "h" ["highs"] (ReqArg (\x o -> o { highString = x }) "") "" ] -parseOpts :: [String] -> IO VolumeOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - percent :: Integer -> Integer -> Integer -> Float percent v' lo' hi' = (v - lo) / (hi - lo) where v = fromIntegral v' @@ -173,7 +167,7 @@ formatDb opts dbi = do runVolume :: String -> String -> [String] -> Monitor String runVolume mixerName controlName argv = do - opts <- io $ parseOpts argv + opts <- io $ parseOptsWith options defaultOpts argv runVolumeWith opts mixerName controlName runVolumeWith :: VolumeOpts -> String -> String -> Monitor String diff --git a/src/Xmobar/Plugins/Monitors/Weather.hs b/src/Xmobar/Plugins/Monitors/Weather.hs index b2a3dd4..07d8cc4 100644 --- a/src/Xmobar/Plugins/Monitors/Weather.hs +++ b/src/Xmobar/Plugins/Monitors/Weather.hs @@ -26,12 +26,7 @@ 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 - ) +import System.Console.GetOpt (ArgDescr(ReqArg), OptDescr(Option)) -- | Options the user may specify. @@ -51,13 +46,6 @@ 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 "<station>: <tempC>C, rh <rh>% (<hour>)" -- template @@ -260,7 +248,7 @@ runWeather = runWeather' [] runWeather' :: [(String, String)] -> [String] -> Monitor String runWeather' sks args = do d <- io $ getData $ head args - o <- io $ parseOpts args + o <- io $ parseOptsWith options defaultOpts args i <- io $ runP parseData d formatWeather o sks i diff --git a/src/Xmobar/Plugins/Monitors/Wireless.hs b/src/Xmobar/Plugins/Monitors/Wireless.hs index 545f6bc..f2b1e6a 100644 --- a/src/Xmobar/Plugins/Monitors/Wireless.hs +++ b/src/Xmobar/Plugins/Monitors/Wireless.hs @@ -34,12 +34,6 @@ options = opts { qualityIconPattern = Just $ parseIconPattern d }) "") "" ] -parseOpts :: [String] -> IO WirelessOpts -parseOpts argv = - case getOpt Permute options argv of - (o, _, []) -> return $ foldr id defaultOpts o - (_, _, errs) -> ioError . userError $ concat errs - wirelessConfig :: IO MConfig wirelessConfig = mkMConfig "<essid> <quality>" @@ -47,7 +41,7 @@ wirelessConfig = runWireless :: String -> [String] -> Monitor String runWireless iface args = do - opts <- io $ parseOpts args + opts <- io $ parseOptsWith options defaultOpts args iface' <- if "" == iface then io findInterface else return iface wi <- io $ getWirelessInfo iface' na <- getConfigValue naString |