From f655873269ccd8992c5f5610327a24f78c449403 Mon Sep 17 00:00:00 2001 From: Todd Lunter Date: Sun, 26 May 2013 13:23:59 -0400 Subject: Made parseTemplate recursively replace template variables. --- src/Plugins/Monitors/Batt.hs | 5 ++--- src/Plugins/Monitors/Common.hs | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs index eddec06..4c0232f 100644 --- a/src/Plugins/Monitors/Batt.hs +++ b/src/Plugins/Monitors/Batt.hs @@ -163,9 +163,8 @@ runBatt' bfs args = do case c of Result x w t s -> do l <- fmtPercent x - let ts = [fmtTime $ floor t, fmtWatts w opts suffix d] - s' <- parseTemplate' (s ++ "") (l ++ "":ts) - parseTemplate (l ++ s':ts) + let ts = [fmtTime $ floor t, fmtWatts w opts suffix d] + parseTemplate (l ++ s:ts) NA -> return "N/A" where fmtPercent :: Float -> Monitor [String] fmtPercent x = do diff --git a/src/Plugins/Monitors/Common.hs b/src/Plugins/Monitors/Common.hs index 881d679..5caaa85 100644 --- a/src/Plugins/Monitors/Common.hs +++ b/src/Plugins/Monitors/Common.hs @@ -302,28 +302,26 @@ templateParser = many templateStringParser --"%") parseTemplate :: [String] -> Monitor String parseTemplate l = do t <- getConfigValue template - s <- io $ runP templateParser t e <- getConfigValue export let m = Map.fromList . zip e $ l - return $ combine m s + parseTemplate' t m -- | Works like parseTemplate, but using the given template string. -parseTemplate' :: String -> [String] -> Monitor String -parseTemplate' t l = do - t' <- getConfigValue template - setConfigValue t template - r <- parseTemplate l - setConfigValue t' template - return r +parseTemplate' :: String -> Map.Map String String -> Monitor String +parseTemplate' t m = + do s <- io $ runP templateParser t + combine m s -- | Given a finite "Map" and a parsed templatet produces the -- | resulting output string. -combine :: Map.Map String String -> [(String, String, String)] -> String -combine _ [] = [] +combine :: Map.Map String String -> [(String, String, String)] -> Monitor String +combine _ [] = return [] combine m ((s,ts,ss):xs) = - s ++ str ++ ss ++ combine m xs - where str = Map.findWithDefault err ts m - err = "<" ++ ts ++ " not found!>" + do next <- combine m xs + let str = Map.findWithDefault err ts m + err = "<" ++ ts ++ " not found!>" + nstr <- parseTemplate' str m + return $ s ++ (if nstr == [] then str else nstr) ++ ss ++ next -- $strings -- cgit v1.2.3