From 0b7b43ef83a2ab6fd894c20e028b9d69372c7266 Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 25 Nov 2018 00:58:21 +0000 Subject: Plugins.Utils -> Utils --- src/app/Configuration.hs | 26 +++---------- src/lib/Xmobar.hs | 3 +- src/lib/Xmobar/Commands.hs | 2 +- src/lib/Xmobar/Config.hs | 16 ++++++++ src/lib/Xmobar/Plugins.hs | 2 - src/lib/Xmobar/Plugins/BufferedPipeReader.hs | 1 + src/lib/Xmobar/Plugins/CommandReader.hs | 1 + src/lib/Xmobar/Plugins/MBox.hs | 2 +- src/lib/Xmobar/Plugins/Mail.hs | 2 +- src/lib/Xmobar/Plugins/MarqueePipeReader.hs | 3 +- src/lib/Xmobar/Plugins/PipeReader.hs | 5 ++- src/lib/Xmobar/Plugins/StdinReader.hs | 1 + src/lib/Xmobar/Plugins/Utils.hs | 43 --------------------- src/lib/Xmobar/Signal.hs | 2 +- src/lib/Xmobar/Utils.hs | 57 ++++++++++++++++++++++++++++ src/lib/Xmobar/XUtil.hs | 15 +------- xmobar.cabal | 6 +-- 17 files changed, 96 insertions(+), 91 deletions(-) delete mode 100644 src/lib/Xmobar/Plugins/Utils.hs create mode 100644 src/lib/Xmobar/Utils.hs diff --git a/src/app/Configuration.hs b/src/app/Configuration.hs index e1e3c24..1cf3ebf 100644 --- a/src/app/Configuration.hs +++ b/src/app/Configuration.hs @@ -24,12 +24,8 @@ import Text.ParserCombinators.Parsec.Number (int) import Text.ParserCombinators.Parsec.Perm ((<|?>), (<$?>), permute) import Control.Monad.IO.Class (liftIO) -import System.Posix.Files -import System.FilePath (()) import System.Environment -import System.Directory (getHomeDirectory) - -import Xmobar.Config +import System.Posix.Files (fileExist) import qualified Xmobar.Config as C @@ -177,7 +173,7 @@ commandsErr = "commands: this usually means that a command could not" ++ "\nwhich follows the offending one." -- | Reads the configuration files or quits with an error -readConfig :: FilePath -> String -> IO (Config,[String]) +readConfig :: FilePath -> String -> IO (C.Config,[String]) readConfig f usage = do file <- liftIO $ fileExist f s <- liftIO $ if file then readFileSafe f else error $ @@ -186,22 +182,10 @@ readConfig f usage = do ": configuration file contains errors at:\n" ++ show err) return $ parseConfig s -xdgConfigDir :: IO String -xdgConfigDir = do env <- getEnvironment - case lookup "XDG_CONFIG_HOME" env of - Just val -> return val - Nothing -> fmap ( ".config") getHomeDirectory - -xmobarConfigDir :: IO FilePath -xmobarConfigDir = fmap ( "xmobar") xdgConfigDir - -getXdgConfigFile :: IO FilePath -getXdgConfigFile = fmap ( "xmobarrc") xmobarConfigDir - -- | Read default configuration file or load the default config -readDefaultConfig :: String -> IO (Config,[String]) +readDefaultConfig :: String -> IO (C.Config,[String]) readDefaultConfig usage = do - xdgConfigFile <- getXdgConfigFile + xdgConfigFile <- C.getXdgConfigFile xdgConfigFileExists <- liftIO $ fileExist xdgConfigFile home <- liftIO $ getEnv "HOME" let defaultConfigFile = home ++ "/.xmobarrc" @@ -210,4 +194,4 @@ readDefaultConfig usage = do then readConfig xdgConfigFile usage else if defaultConfigFileExists then readConfig defaultConfigFile usage - else return (defaultConfig,[]) + else return (C.defaultConfig,[]) diff --git a/src/lib/Xmobar.hs b/src/lib/Xmobar.hs index a90ec3e..547c549 100644 --- a/src/lib/Xmobar.hs +++ b/src/lib/Xmobar.hs @@ -15,7 +15,7 @@ -- ----------------------------------------------------------------------------- -module Xmobar (xmobar, Runnable (..)) where +module Xmobar (xmobar, Runnable (..), module Xmobar.Config) where import Xmobar.EventLoop (startLoop, startCommand) import Xmobar.Config @@ -30,7 +30,6 @@ import Control.Exception (bracket) import Xmobar.Parsers import Xmobar.XUtil -import Xmobar.Config() import Xmobar.Signal (setupSignalHandler, withDeferSignals) import Xmobar.Window import Xmobar.Types diff --git a/src/lib/Xmobar/Commands.hs b/src/lib/Xmobar/Commands.hs index ececdd9..9c92de0 100644 --- a/src/lib/Xmobar/Commands.hs +++ b/src/lib/Xmobar/Commands.hs @@ -32,7 +32,7 @@ import System.Exit import System.IO (hClose) import Xmobar.Signal -import Xmobar.XUtil +import Xmobar.Utils (hGetLineSafe) class Show e => Exec e where alias :: e -> String diff --git a/src/lib/Xmobar/Config.hs b/src/lib/Xmobar/Config.hs index 21b29fa..364ce17 100644 --- a/src/lib/Xmobar/Config.hs +++ b/src/lib/Xmobar/Config.hs @@ -21,8 +21,12 @@ module Xmobar.Config , XPosition (..), Align (..), Border(..) , defaultConfig , runnableTypes + , getXdgConfigFile ) where +import System.Environment +import System.Directory (getHomeDirectory) +import System.FilePath (()) import Xmobar.Commands import {-# SOURCE #-} Xmobar.Runnable @@ -168,3 +172,15 @@ runnableTypes :: Command :*: Monitors :*: Date :*: PipeReader :*: #endif MarqueePipeReader :*: () runnableTypes = undefined + +xdgConfigDir :: IO String +xdgConfigDir = do env <- getEnvironment + case lookup "XDG_CONFIG_HOME" env of + Just val -> return val + Nothing -> fmap ( ".config") getHomeDirectory + +xmobarConfigDir :: IO FilePath +xmobarConfigDir = fmap ( "xmobar") xdgConfigDir + +getXdgConfigFile :: IO FilePath +getXdgConfigFile = fmap ( "xmobarrc") xmobarConfigDir diff --git a/src/lib/Xmobar/Plugins.hs b/src/lib/Xmobar/Plugins.hs index e8c6403..284a9f5 100644 --- a/src/lib/Xmobar/Plugins.hs +++ b/src/lib/Xmobar/Plugins.hs @@ -17,8 +17,6 @@ module Xmobar.Plugins ( Exec (..) , tenthSeconds - , hGetLineSafe ) where import Xmobar.Commands -import Xmobar.XUtil diff --git a/src/lib/Xmobar/Plugins/BufferedPipeReader.hs b/src/lib/Xmobar/Plugins/BufferedPipeReader.hs index d4d30a1..4bb80db 100644 --- a/src/lib/Xmobar/Plugins/BufferedPipeReader.hs +++ b/src/lib/Xmobar/Plugins/BufferedPipeReader.hs @@ -23,6 +23,7 @@ import System.IO.Unsafe(unsafePerformIO) import Xmobar.Environment import Xmobar.Plugins import Xmobar.Signal +import Xmobar.Utils(hGetLineSafe) data BufferedPipeReader = BufferedPipeReader String [(Int, Bool, String)] deriving (Read, Show) diff --git a/src/lib/Xmobar/Plugins/CommandReader.hs b/src/lib/Xmobar/Plugins/CommandReader.hs index 80b6299..e90fa62 100644 --- a/src/lib/Xmobar/Plugins/CommandReader.hs +++ b/src/lib/Xmobar/Plugins/CommandReader.hs @@ -17,6 +17,7 @@ module Xmobar.Plugins.CommandReader where import System.IO import Xmobar.Plugins +import Xmobar.Utils (hGetLineSafe) import System.Process(runInteractiveCommand, getProcessExitCode) data CommandReader = CommandReader String String diff --git a/src/lib/Xmobar/Plugins/MBox.hs b/src/lib/Xmobar/Plugins/MBox.hs index 2281629..20e63cf 100644 --- a/src/lib/Xmobar/Plugins/MBox.hs +++ b/src/lib/Xmobar/Plugins/MBox.hs @@ -18,7 +18,7 @@ module Xmobar.Plugins.MBox (MBox(..)) where import Prelude import Xmobar.Plugins #ifdef INOTIFY -import Xmobar.Plugins.Utils (changeLoop, expandHome) +import Xmobar.Utils (changeLoop, expandHome) import Control.Monad (when) import Control.Concurrent.STM diff --git a/src/lib/Xmobar/Plugins/Mail.hs b/src/lib/Xmobar/Plugins/Mail.hs index c41b5b3..d8946bf 100644 --- a/src/lib/Xmobar/Plugins/Mail.hs +++ b/src/lib/Xmobar/Plugins/Mail.hs @@ -17,7 +17,7 @@ module Xmobar.Plugins.Mail where import Xmobar.Plugins #ifdef INOTIFY -import Xmobar.Plugins.Utils (expandHome, changeLoop) +import Xmobar.Utils (expandHome, changeLoop) import Control.Monad import Control.Concurrent.STM diff --git a/src/lib/Xmobar/Plugins/MarqueePipeReader.hs b/src/lib/Xmobar/Plugins/MarqueePipeReader.hs index ad6f27f..ad1ae40 100644 --- a/src/lib/Xmobar/Plugins/MarqueePipeReader.hs +++ b/src/lib/Xmobar/Plugins/MarqueePipeReader.hs @@ -16,7 +16,8 @@ module Xmobar.Plugins.MarqueePipeReader where import System.IO (openFile, IOMode(ReadWriteMode), Handle) import Xmobar.Environment -import Xmobar.Plugins (tenthSeconds, Exec(alias, start), hGetLineSafe) +import Xmobar.Plugins (tenthSeconds, Exec(alias, start)) +import Xmobar.Utils(hGetLineSafe) import System.Posix.Files (getFileStatus, isNamedPipe) import Control.Concurrent(forkIO, threadDelay) import Control.Concurrent.STM (TChan, atomically, writeTChan, tryReadTChan, newTChan) diff --git a/src/lib/Xmobar/Plugins/PipeReader.hs b/src/lib/Xmobar/Plugins/PipeReader.hs index 7166163..593a4a7 100644 --- a/src/lib/Xmobar/Plugins/PipeReader.hs +++ b/src/lib/Xmobar/Plugins/PipeReader.hs @@ -15,8 +15,9 @@ module Xmobar.Plugins.PipeReader where import System.IO -import Xmobar.Plugins -import Xmobar.Environment +import Xmobar.Plugins(Exec(..)) +import Xmobar.Environment(expandEnv) +import Xmobar.Utils(hGetLineSafe) import System.Posix.Files import Control.Concurrent(threadDelay) import Control.Exception diff --git a/src/lib/Xmobar/Plugins/StdinReader.hs b/src/lib/Xmobar/Plugins/StdinReader.hs index 372e4f9..ad8e60c 100644 --- a/src/lib/Xmobar/Plugins/StdinReader.hs +++ b/src/lib/Xmobar/Plugins/StdinReader.hs @@ -25,6 +25,7 @@ import System.IO import Control.Exception (SomeException(..), handle) import Xmobar.Plugins import Xmobar.Actions (stripActions) +import Xmobar.Utils (hGetLineSafe) data StdinReader = StdinReader | UnsafeStdinReader deriving (Read, Show) diff --git a/src/lib/Xmobar/Plugins/Utils.hs b/src/lib/Xmobar/Plugins/Utils.hs deleted file mode 100644 index 6546c15..0000000 --- a/src/lib/Xmobar/Plugins/Utils.hs +++ /dev/null @@ -1,43 +0,0 @@ ------------------------------------------------------------------------------- --- | --- Module: Plugins.Utils --- Copyright: (c) 2010 Jose Antonio Ortega Ruiz --- License: BSD3-style (see LICENSE) --- --- Maintainer: Jose A Ortega Ruiz --- Stability: unstable --- Portability: unportable --- Created: Sat Dec 11, 2010 20:55 --- --- --- Miscellaneous utility functions --- ------------------------------------------------------------------------------- - - -module Xmobar.Plugins.Utils (expandHome, changeLoop, safeHead) where - -import Control.Monad -import Control.Concurrent.STM - -import System.Environment -import System.FilePath - - -expandHome :: FilePath -> IO FilePath -expandHome ('~':'/':path) = fmap ( path) (getEnv "HOME") -expandHome p = return p - -changeLoop :: Eq a => STM a -> (a -> IO ()) -> IO () -changeLoop s f = atomically s >>= go - where - go old = do - f old - go =<< atomically (do - new <- s - guard (new /= old) - return new) - -safeHead :: [a] -> Maybe a -safeHead [] = Nothing -safeHead (x:_) = Just x diff --git a/src/lib/Xmobar/Signal.hs b/src/lib/Xmobar/Signal.hs index bdc4be1..fd68e80 100644 --- a/src/lib/Xmobar/Signal.hs +++ b/src/lib/Xmobar/Signal.hs @@ -33,7 +33,7 @@ import DBus (IsVariant(..)) import Control.Monad ((>=>)) #endif -import Xmobar.Plugins.Utils (safeHead) +import Xmobar.Utils (safeHead) data WakeUp = WakeUp deriving (Show,Typeable) instance Exception WakeUp diff --git a/src/lib/Xmobar/Utils.hs b/src/lib/Xmobar/Utils.hs new file mode 100644 index 0000000..ce8c4ad --- /dev/null +++ b/src/lib/Xmobar/Utils.hs @@ -0,0 +1,57 @@ +{-# LANGUAGE CPP #-} + +------------------------------------------------------------------------------ +-- | +-- Module: Utils +-- Copyright: (c) 2010, 2018 Jose Antonio Ortega Ruiz +-- License: BSD3-style (see LICENSE) +-- +-- Maintainer: Jose A Ortega Ruiz +-- Stability: unstable +-- Portability: unportable +-- Created: Sat Dec 11, 2010 20:55 +-- +-- +-- Miscellaneous utility functions +-- +------------------------------------------------------------------------------ + + +module Xmobar.Utils (expandHome, changeLoop, safeHead, hGetLineSafe) where + +import Control.Monad +import Control.Concurrent.STM + +import System.Environment +import System.FilePath +import System.IO + +#if defined XFT || defined UTF8 +import qualified System.IO as S (hGetLine) +#endif + +hGetLineSafe :: Handle -> IO String +#if defined XFT || defined UTF8 +hGetLineSafe = S.hGetLine +#else +hGetLineSafe = hGetLine +#endif + + +expandHome :: FilePath -> IO FilePath +expandHome ('~':'/':path) = fmap ( path) (getEnv "HOME") +expandHome p = return p + +changeLoop :: Eq a => STM a -> (a -> IO ()) -> IO () +changeLoop s f = atomically s >>= go + where + go old = do + f old + go =<< atomically (do + new <- s + guard (new /= old) + return new) + +safeHead :: [a] -> Maybe a +safeHead [] = Nothing +safeHead (x:_) = Just x diff --git a/src/lib/Xmobar/XUtil.hs b/src/lib/Xmobar/XUtil.hs index 536a2fb..c4b4531 100644 --- a/src/lib/Xmobar/XUtil.hs +++ b/src/lib/Xmobar/XUtil.hs @@ -22,7 +22,6 @@ module Xmobar.XUtil , textWidth , printString , nextEvent' - , hGetLineSafe ) where import Control.Concurrent @@ -36,26 +35,16 @@ import qualified Graphics.X11.Xlib as Xlib (textExtents, textWidth) import Graphics.X11.Xlib.Extras import System.Mem.Weak ( addFinalizer ) import System.Posix.Types (Fd(..)) -import System.IO - -#if defined XFT || defined UTF8 -import qualified System.IO as S (hGetLine) -#endif #if defined XFT import Xmobar.MinXft import Graphics.X11.Xrender +#else +import System.IO(hPutStrLn, stderr) #endif import Xmobar.ColorCache -hGetLineSafe :: Handle -> IO String -#if defined XFT || defined UTF8 -hGetLineSafe = S.hGetLine -#else -hGetLineSafe = hGetLine -#endif - -- Hide the Core Font/Xft switching here data XFont = Core FontStruct | Utf8 FontSet diff --git a/xmobar.cabal b/xmobar.cabal index 330704a..efe5a5d 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -133,6 +133,7 @@ library other-modules: Xmobar.Localize, Xmobar.Parsers, + Xmobar.Utils, Xmobar.XUtil, Xmobar.StatFS, Xmobar.ColorCache, @@ -140,8 +141,7 @@ library Xmobar.Draw, Xmobar.Signal, Xmobar.Environment, - Xmobar.EventLoop, - Xmobar.Plugins.Utils + Xmobar.EventLoop extra-libraries: Xrandr Xrender @@ -302,9 +302,9 @@ test-suite XmobarTest Xmobar.Commands, Xmobar.Signal, Xmobar.XUtil, + Xmobar.Utils, Xmobar.ColorCache, Xmobar.Plugins, - Xmobar.Plugins.Utils, Xmobar.Plugins.Monitors.Common if flag(with_alsa) || flag(all_extensions) -- cgit v1.2.3