summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Plugins/Monitors/Common/Parsers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Xmobar/Plugins/Monitors/Common/Parsers.hs')
-rw-r--r--src/Xmobar/Plugins/Monitors/Common/Parsers.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Xmobar/Plugins/Monitors/Common/Parsers.hs b/src/Xmobar/Plugins/Monitors/Common/Parsers.hs
index 7a813e5..d814349 100644
--- a/src/Xmobar/Plugins/Monitors/Common/Parsers.hs
+++ b/src/Xmobar/Plugins/Monitors/Common/Parsers.hs
@@ -1,3 +1,6 @@
+{-#LANGUAGE RecordWildCards#-}
+{-#LANGUAGE ScopedTypeVariables#-}
+
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.Plugins.Monitors.Parsers
@@ -25,6 +28,10 @@ module Xmobar.Plugins.Monitors.Common.Parsers ( runP
, parseTemplate
, parseTemplate'
, parseOptsWith
+ , templateParser
+ , runExportParser
+ , runTemplateParser
+ , pureParseTemplate
) where
import Xmobar.Plugins.Monitors.Common.Types
@@ -34,6 +41,38 @@ import qualified Data.Map as Map
import System.Console.GetOpt (ArgOrder(Permute), OptDescr, getOpt)
import Text.ParserCombinators.Parsec
+runTemplateParser :: PureConfig -> IO [(String, String, String)]
+runTemplateParser PureConfig{..} = runP templateParser pTemplate
+
+runExportParser :: [String] -> IO [(String, [(String, String,String)])]
+runExportParser [] = pure []
+runExportParser (x:xs) = do
+ s <- runP templateParser x
+ rem <- runExportParser xs
+ pure $ (x,s):rem
+
+pureParseTemplate :: PureConfig -> TemplateInput -> IO String
+pureParseTemplate PureConfig{..} TemplateInput{..} =
+ do let t = pTemplate
+ e = pExport
+ w = pMaxTotalWidth
+ let m = let expSnds :: [([(String, String, String)], String)] = zip (map snd temAllTemplate) temMonitorValues
+ in Map.fromList . zip (map fst temAllTemplate) $ expSnds
+ s <- minCombine m temInputTemplate
+ let (n, s') = if w > 0 && length s > w
+ then trimTo (w - length pMaxTotalWidthEllipsis) "" s
+ else (1, s)
+ return $ if n > 0 then s' else s' ++ pMaxTotalWidthEllipsis
+
+minCombine :: Map.Map String ([(String, String, String)], String) -> [(String, String, String)] -> IO String
+minCombine _ [] = return []
+minCombine m ((s,ts,ss):xs) =
+ do next <- minCombine m xs
+ str <- case Map.lookup ts m of
+ Nothing -> return $ "<" ++ ts ++ ">"
+ Just (s,r) -> let f "" = r; f n = n; in f <$> minCombine m s
+ pure $ s ++ str ++ ss ++ next
+
runP :: Parser [a] -> String -> IO [a]
runP p i =
case parse p "" i of