summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/System
diff options
context:
space:
mode:
authorSibi Prabakaran <sibi@psibi.in>2020-05-10 19:43:56 +0530
committerSibi Prabakaran <sibi@psibi.in>2020-05-10 19:43:56 +0530
commit3e9e1cb9d300e13109206496d825351c4f41cc1c (patch)
treed40f6c9f185ea4b01b6652e4178a5f28e450c8fa /src/Xmobar/System
parent6f3b730415147041ff34558fe7292a76dc87b1f0 (diff)
downloadxmobar-3e9e1cb9d300e13109206496d825351c4f41cc1c.tar.gz
xmobar-3e9e1cb9d300e13109206496d825351c4f41cc1c.tar.bz2
Fix crashes/busy looping happening via index
Right now, with the `StdinReader` plugin enabled - you can crash/cause busy looping of xmobar if the following html file is opened: ``` <html> <head> <title>hello <fn=1>string</fn> </title> </head> </html> ``` More details about this bug is here: https://github.com/jaor/xmobar/issues/442#issuecomment-625706001 This MR also fixes another bug which produces a crash in xmobar if you pass non integer items between fn: <fn=crash>
Diffstat (limited to 'src/Xmobar/System')
-rw-r--r--src/Xmobar/System/Utils.hs24
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