module Xmobar.Run.Template(parseTemplate, splitTemplate) where
import qualified Data.Map as Map
import Text.ParserCombinators.Parsec
import Xmobar.Run.Exec
import Xmobar.Run.Command
import Xmobar.Run.Runnable
defaultAlign :: String
defaultAlign = "}{"
allTillSep :: String -> Parser String
allTillSep = many . noneOf
templateStringParser :: String -> Parser (String,String,String)
templateStringParser sepChar = do
s <- allTillSep sepChar
com <- templateCommandParser sepChar
ss <- allTillSep sepChar
return (com, s, ss)
templateCommandParser :: String -> Parser String
templateCommandParser sepChar =
let chr = char (head sepChar) in between chr chr (allTillSep sepChar)
templateParser :: String -> Parser [(String,String,String)]
templateParser s = many $ templateStringParser s
parseTemplate :: [Runnable] -> String -> String -> IO [(Runnable,String,String)]
parseTemplate c sepChar s =
do str <- case parse (templateParser sepChar) "" s of
Left _ -> return [("", s, "")]
Right x -> return x
let cl = map alias c
m = Map.fromList $ zip cl c
return $ combine c m str
combine :: [Runnable] -> Map.Map String Runnable -> [(String, String, String)]
-> [(Runnable,String,String)]
combine _ _ [] = []
combine c m ((ts,s,ss):xs) = (com, s, ss) : combine c m xs
where com = Map.findWithDefault dflt ts m
dflt = Run $ Com ts [] [] 10
splitTemplate :: String -> String -> [String]
splitTemplate alignSep template =
case break (==l) template of
(le,_:re) -> case break (==r) re of
(ce,_:ri) -> [le, ce, ri]
_ -> def
_ -> def
where [l, r] = if length alignSep == 2 then alignSep else defaultAlign
def = [template, "", ""]