-
Notifications
You must be signed in to change notification settings - Fork 2
/
Background.hs
47 lines (42 loc) · 1.43 KB
/
Background.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-- | Selects and displays a random desktop background
module Background
( randomBackground
, updateBackgrounds
) where
import Path (parseAbsDir, fileExtension, toFilePath)
import Path.IO (listDirRecur)
import qualified Data.Vector as V
import Imports
import Misc
randomBackground :: Xio ()
randomBackground = do
backgrounds <- ensureBackgrounds
result <- randomComponent backgrounds
spawn "feh" ["--bg-scale", result]
ensureBackgrounds :: Xio (V.Vector FilePath)
ensureBackgrounds = do
backgroundsVar <- view envBackgroundsVar
mbackgrounds <- readMVar backgroundsVar
case mbackgrounds of
Nothing -> updateBackgrounds
Just backgrounds -> return backgrounds
updateBackgrounds :: Xio (V.Vector FilePath)
updateBackgrounds = do
homeDir <- view envHomeDir
backgroundsVar <- view envBackgroundsVar
modifyMVar backgroundsVar $ \oldValue -> do
let backgroundsDir = homeDir </> "env/untracked/backgrounds"
exists <- doesDirectoryExist backgroundsDir
if exists
then do
(_, files) <- listDirRecur =<< parseAbsDir backgroundsDir
result <- V.fromList . map toFilePath <$>
filterM (\x -> (".jpg" ==) <$> fileExtension x) files
return (Just result, result)
else do
logError $ mconcat
[ "Not updating backgrounds list, because "
, fromString (show backgroundsDir)
, " does not exist."
]
return (oldValue, fromMaybe mempty oldValue)