summaryrefslogtreecommitdiffstats
path: root/lib/HistoryGrid.hs
blob: 2c53fe6ae77774b51b38dfe081bbf957e30a7fc0 (plain) (blame)
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
module HistoryGrid (openLastHistoryGrid) where

import Prelude hiding (catch)

import Control.Exception.Extensible hiding (handle)
import Control.Monad.IO.Class
import Data.List
import qualified Data.Map as M
import Data.Maybe
import System.Directory
import System.IO

import XMonad.Core
import XMonad.Actions.GridSelect


type History = M.Map String [String]

emptyHistory :: History
emptyHistory = M.empty

getHistoryFile :: IO FilePath
getHistoryFile = fmap (++ "/history") $ getAppUserDataDirectory "xmonad"

readHistory :: IO History
readHistory = readHist `catch` \(SomeException _) -> return emptyHistory
    where
        readHist = do
            path <- getHistoryFile
            xs <- bracket (openFile path ReadMode) hClose hGetLine
            readIO xs

getLastHistoryItems :: History -> Int -> [String]
getLastHistoryItems hist i = take i $ nub $ fromMaybe [] $ M.lookup "Run: " hist

getLastHistory :: Int -> IO [String]
getLastHistory count = readHistory >>= \hist -> return $ getLastHistoryItems hist count

openLastHistoryGrid :: GSConfig String -> Int -> X()
openLastHistoryGrid config count = do
    hist <- liftIO $ getLastHistory count
    spawnSelected config hist