From 3852d6b5e354b9b03b30f04803a87b2224aeb85c Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 25 Nov 2018 03:49:15 +0000 Subject: Xmobar.System.Environment --- src/lib/Xmobar/Environment.hs | 49 ---------------------------- src/lib/Xmobar/Plugins/BufferedPipeReader.hs | 4 +-- src/lib/Xmobar/Plugins/MarqueePipeReader.hs | 2 +- src/lib/Xmobar/Plugins/PipeReader.hs | 2 +- src/lib/Xmobar/System/Environment.hs | 49 ++++++++++++++++++++++++++++ xmobar.cabal | 6 ++-- 6 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 src/lib/Xmobar/Environment.hs create mode 100644 src/lib/Xmobar/System/Environment.hs diff --git a/src/lib/Xmobar/Environment.hs b/src/lib/Xmobar/Environment.hs deleted file mode 100644 index 8a9223a..0000000 --- a/src/lib/Xmobar/Environment.hs +++ /dev/null @@ -1,49 +0,0 @@ ------------------------------------------------------------------------------ --- | --- Module : XMobar.Environment --- Copyright : (c) William Song --- License : BSD-style (see LICENSE) --- --- Maintainer : Will Song --- Stability : stable --- Portability : portable --- --- A function to expand environment variables in strings --- ------------------------------------------------------------------------------ -module Xmobar.Environment(expandEnv) where - -import Control.Applicative ((<$>)) -import Data.Maybe (fromMaybe) -import System.Environment (lookupEnv) - -expandEnv :: String -> IO String -expandEnv "" = return "" -expandEnv (c:s) = case c of - '$' -> do - envVar <- fromMaybe "" <$> lookupEnv e - remainder <- expandEnv s' - return $ envVar ++ remainder - where (e, s') = getVar s - getVar "" = ("", "") - getVar ('{':s'') = (takeUntil "}" s'', drop 1 . dropUntil "}" $ s'') - getVar s'' = (takeUntil filterstr s'', dropUntil filterstr s'') - filterstr = ",./? \t;:\"'~`!@#$%^&*()<>-+=\\|" - takeUntil f = takeWhile (not . flip elem f) - dropUntil f = dropWhile (not . flip elem f) - - '\\' -> case s == "" of - True -> return "\\" - False -> do - remainder <- expandEnv $ drop 1 s - return $ escString s ++ remainder - where escString s' = let (cc:_) = s' in - case cc of - 't' -> "\t" - 'n' -> "\n" - '$' -> "$" - _ -> [cc] - - _ -> do - remainder <- expandEnv s - return $ c : remainder diff --git a/src/lib/Xmobar/Plugins/BufferedPipeReader.hs b/src/lib/Xmobar/Plugins/BufferedPipeReader.hs index ce6a783..13f64ac 100644 --- a/src/lib/Xmobar/Plugins/BufferedPipeReader.hs +++ b/src/lib/Xmobar/Plugins/BufferedPipeReader.hs @@ -20,10 +20,10 @@ import Control.Concurrent.STM import System.IO import System.IO.Unsafe(unsafePerformIO) -import Xmobar.Environment import Xmobar.Plugins import Xmobar.Utils(hGetLineSafe) import Xmobar.System.Signal +import Xmobar.System.Environment data BufferedPipeReader = BufferedPipeReader String [(Int, Bool, String)] deriving (Read, Show) @@ -45,7 +45,7 @@ instance Exec BufferedPipeReader where writer chan str rst where - initV :: IO ( TChan (Int, Bool, String), TVar (Maybe String), TVar Bool ) + initV :: IO (TChan (Int, Bool, String), TVar (Maybe String), TVar Bool) initV = atomically $ do tc <- newTChan ts <- newTVar Nothing diff --git a/src/lib/Xmobar/Plugins/MarqueePipeReader.hs b/src/lib/Xmobar/Plugins/MarqueePipeReader.hs index ad1ae40..48f5355 100644 --- a/src/lib/Xmobar/Plugins/MarqueePipeReader.hs +++ b/src/lib/Xmobar/Plugins/MarqueePipeReader.hs @@ -15,7 +15,7 @@ module Xmobar.Plugins.MarqueePipeReader where import System.IO (openFile, IOMode(ReadWriteMode), Handle) -import Xmobar.Environment +import Xmobar.System.Environment import Xmobar.Plugins (tenthSeconds, Exec(alias, start)) import Xmobar.Utils(hGetLineSafe) import System.Posix.Files (getFileStatus, isNamedPipe) diff --git a/src/lib/Xmobar/Plugins/PipeReader.hs b/src/lib/Xmobar/Plugins/PipeReader.hs index 593a4a7..5e22408 100644 --- a/src/lib/Xmobar/Plugins/PipeReader.hs +++ b/src/lib/Xmobar/Plugins/PipeReader.hs @@ -16,8 +16,8 @@ module Xmobar.Plugins.PipeReader where import System.IO import Xmobar.Plugins(Exec(..)) -import Xmobar.Environment(expandEnv) import Xmobar.Utils(hGetLineSafe) +import Xmobar.System.Environment(expandEnv) import System.Posix.Files import Control.Concurrent(threadDelay) import Control.Exception diff --git a/src/lib/Xmobar/System/Environment.hs b/src/lib/Xmobar/System/Environment.hs new file mode 100644 index 0000000..86197db --- /dev/null +++ b/src/lib/Xmobar/System/Environment.hs @@ -0,0 +1,49 @@ +----------------------------------------------------------------------------- +-- | +-- Module : XMobar.Environment +-- Copyright : (c) William Song +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Will Song +-- Stability : stable +-- Portability : portable +-- +-- A function to expand environment variables in strings +-- +----------------------------------------------------------------------------- +module Xmobar.System.Environment(expandEnv) where + +import Control.Applicative ((<$>)) +import Data.Maybe (fromMaybe) +import System.Environment (lookupEnv) + +expandEnv :: String -> IO String +expandEnv "" = return "" +expandEnv (c:s) = case c of + '$' -> do + envVar <- fromMaybe "" <$> lookupEnv e + remainder <- expandEnv s' + return $ envVar ++ remainder + where (e, s') = getVar s + getVar "" = ("", "") + getVar ('{':s'') = (takeUntil "}" s'', drop 1 . dropUntil "}" $ s'') + getVar s'' = (takeUntil filterstr s'', dropUntil filterstr s'') + filterstr = ",./? \t;:\"'~`!@#$%^&*()<>-+=\\|" + takeUntil f = takeWhile (not . flip elem f) + dropUntil f = dropWhile (not . flip elem f) + + '\\' -> case s == "" of + True -> return "\\" + False -> do + remainder <- expandEnv $ drop 1 s + return $ escString s ++ remainder + where escString s' = let (cc:_) = s' in + case cc of + 't' -> "\t" + 'n' -> "\n" + '$' -> "$" + _ -> [cc] + + _ -> do + remainder <- expandEnv s + return $ c : remainder diff --git a/xmobar.cabal b/xmobar.cabal index e0cc2e8..ee58013 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -132,6 +132,7 @@ library other-modules: Xmobar.Parsers, Xmobar.Utils, Xmobar.System.StatFS, + Xmobar.System.Environment, Xmobar.System.Localize, Xmobar.System.Signal, Xmobar.X11.Types, @@ -140,8 +141,7 @@ library Xmobar.X11.EventLoop, Xmobar.X11.ColorCache, Xmobar.X11.Window, - Xmobar.X11.Draw, - Xmobar.Environment + Xmobar.X11.Draw extra-libraries: Xrandr Xrender @@ -310,6 +310,6 @@ test-suite XmobarTest Xmobar.Commands Xmobar.Plugins Xmobar.Plugins.Monitors.Common - Xmobar.Signal + Xmobar.System.Signal Xmobar.Utils cpp-options: -DALSA -- cgit v1.2.3