diff options
Diffstat (limited to 'src/Xmobar/System/Utils.hs')
-rw-r--r-- | src/Xmobar/System/Utils.hs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/Xmobar/System/Utils.hs b/src/Xmobar/System/Utils.hs index 59c485c..33f221c 100644 --- a/src/Xmobar/System/Utils.hs +++ b/src/Xmobar/System/Utils.hs @@ -17,11 +17,16 @@ ------------------------------------------------------------------------------ -module Xmobar.System.Utils (expandHome, changeLoop, onSomeException) -where +module Xmobar.System.Utils + ( expandHome + , changeLoop + , onSomeException + , safeIndex + ) where import Control.Monad import Control.Concurrent.STM +import qualified Data.List.NonEmpty as NE import System.Environment import System.FilePath @@ -50,3 +55,18 @@ onSomeException :: IO a -> (SomeException -> IO b) -> IO a onSomeException io what = io `catch` \e -> do _ <- what e throwIO (e :: SomeException) +(!!?) :: [a] -> Int -> Maybe a +(!!?) xs i + | i < 0 = Nothing + | otherwise = go i xs + where + go :: Int -> [a] -> Maybe a + go 0 (x:_) = Just x + go j (_:ys) = go (j - 1) ys + go _ [] = Nothing +{-# INLINE (!!?) #-} + +safeIndex :: NE.NonEmpty a -> Int -> a +safeIndex xs index = case (NE.toList xs) !!? index of + Nothing -> NE.head xs + Just value -> value |