summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2018-11-25 21:48:15 +0000
committerjao <jao@gnu.org>2018-11-25 21:48:15 +0000
commitbc27a68eb346b5e73c530f0f9c3e62e56517e225 (patch)
treed89fb49ee864ce0594d9ba2eff8836abd1e7a564 /src
parente04d4c6eb84d5adfe62b6a538e7c4008974424b2 (diff)
downloadxmobar-bc27a68eb346b5e73c530f0f9c3e62e56517e225.tar.gz
xmobar-bc27a68eb346b5e73c530f0f9c3e62e56517e225.tar.bz2
Xmobar.App and small refactorings
Diffstat (limited to 'src')
-rw-r--r--src/Xmobar.hs18
-rw-r--r--src/Xmobar/App/EventLoop.hs (renamed from src/Xmobar/Run/EventLoop.hs)2
-rw-r--r--src/Xmobar/Run/Template.hs57
3 files changed, 39 insertions, 38 deletions
diff --git a/src/Xmobar.hs b/src/Xmobar.hs
index b4543f6..be20dbe 100644
--- a/src/Xmobar.hs
+++ b/src/Xmobar.hs
@@ -47,7 +47,6 @@ import Control.Exception (bracket)
import Xmobar.Config
import Xmobar.Run.Runnable
import Xmobar.Run.Template
-import Xmobar.Run.EventLoop (startLoop, startCommand)
import Xmobar.System.Signal (setupSignalHandler, withDeferSignals)
import Xmobar.X11.Types
import Xmobar.X11.XUtil
@@ -69,19 +68,7 @@ import Xmobar.Plugins.Monitors
import Xmobar.Plugins.PipeReader
import Xmobar.Plugins.StdinReader
import Xmobar.Plugins.XMonadLog
-
-
-splitTemplate :: Config -> [String]
-splitTemplate conf =
- case break (==l) t of
- (le,_:re) -> case break (==r) re of
- (ce,_:ri) -> [le, ce, ri]
- _ -> def
- _ -> def
- where [l, r] = alignSep
- (if length (alignSep conf) == 2 then conf else defaultConfig)
- t = template conf
- def = [t, "", ""]
+import Xmobar.App.EventLoop (startLoop, startCommand)
xmobar :: Config -> IO ()
xmobar conf = withDeferSignals $ do
@@ -89,7 +76,8 @@ xmobar conf = withDeferSignals $ do
d <- openDisplay ""
fs <- initFont d (font conf)
fl <- mapM (initFont d) (additionalFonts conf)
- cls <- mapM (parseCommands conf) (splitTemplate conf)
+ cls <- mapM (parseTemplate (commands conf) (sepChar conf))
+ (splitTemplate (alignSep conf) (template conf))
sig <- setupSignalHandler
bracket (mapM (mapM $ startCommand sig) cls)
cleanupThreads
diff --git a/src/Xmobar/Run/EventLoop.hs b/src/Xmobar/App/EventLoop.hs
index a4385d1..8f0b2dc 100644
--- a/src/Xmobar/Run/EventLoop.hs
+++ b/src/Xmobar/App/EventLoop.hs
@@ -17,7 +17,7 @@
------------------------------------------------------------------------------
-module Xmobar.Run.EventLoop (startLoop, startCommand) where
+module Xmobar.App.EventLoop (startLoop, startCommand) where
import Prelude hiding (lookup)
import Graphics.X11.Xlib hiding (textExtents, textWidth)
diff --git a/src/Xmobar/Run/Template.hs b/src/Xmobar/Run/Template.hs
index 5bada89..a544724 100644
--- a/src/Xmobar/Run/Template.hs
+++ b/src/Xmobar/Run/Template.hs
@@ -15,51 +15,64 @@
------------------------------------------------------------------------------
-module Xmobar.Run.Template(parseCommands) where
+module Xmobar.Run.Template(parseTemplate, splitTemplate) where
import qualified Data.Map as Map
import Text.ParserCombinators.Parsec
import Xmobar.Run.Commands
import Xmobar.Run.Runnable
-import Xmobar.Config
+
+defaultAlign :: String
+defaultAlign = "}{"
+
+allTillSep :: String -> Parser String
+allTillSep = many . noneOf
-- | Parses the output template string
-templateStringParser :: Config -> Parser (String,String,String)
-templateStringParser c = do
- s <- allTillSep c
- com <- templateCommandParser c
- ss <- allTillSep c
+templateStringParser :: String -> Parser (String,String,String)
+templateStringParser sepChar = do
+ s <- allTillSep sepChar
+ com <- templateCommandParser sepChar
+ ss <- allTillSep sepChar
return (com, s, ss)
-- | Parses the command part of the template string
-templateCommandParser :: Config -> Parser String
-templateCommandParser c =
- let chr = char . head . sepChar
- in between (chr c) (chr c) (allTillSep c)
+templateCommandParser :: String -> Parser String
+templateCommandParser sepChar =
+ let chr = char (head sepChar) in between chr chr (allTillSep sepChar)
-- | Combines the template parsers
-templateParser :: Config -> Parser [(String,String,String)]
-templateParser = many . templateStringParser
+templateParser :: String -> Parser [(String,String,String)]
+templateParser s = many $ templateStringParser s
-- | Actually runs the template parsers
-parseCommands :: Config -> String -> IO [(Runnable,String,String)]
-parseCommands c s =
- do str <- case parse (templateParser c) "" s of
+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 (commands c)
- m = Map.fromList $ zip cl (commands c)
+ let cl = map alias c
+ m = Map.fromList $ zip cl c
return $ combine c m str
-- | Given a finite "Map" and a parsed template produce the resulting
-- output string.
-combine :: Config -> Map.Map String Runnable
- -> [(String, String, String)] -> [(Runnable,String,String)]
+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
-allTillSep :: Config -> Parser String
-allTillSep = many . noneOf . sepChar
+-- | Given an two-char alignment separator and a template string,
+-- splits it into its segments, that can then be parsed via parseCommands
+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, "", ""]