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/lib/Xmobar/Utils.hs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/lib/Xmobar/Utils.hs (limited to 'src/lib/Xmobar/Utils.hs') 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 -- cgit v1.2.3