summaryrefslogblamecommitdiffstats
path: root/lib/HistoryGrid.hs
blob: 2c53fe6ae77774b51b38dfe081bbf957e30a7fc0 (plain) (tree)









































                                                                                       
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