From 6a6dc927416c5c488aa73d3ee13f6640ed3b617e Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 3 Jul 2014 00:43:19 +0200 Subject: initial commit --- lib/HistoryGrid.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/HistoryGrid.hs (limited to 'lib/HistoryGrid.hs') diff --git a/lib/HistoryGrid.hs b/lib/HistoryGrid.hs new file mode 100644 index 0000000..2c53fe6 --- /dev/null +++ b/lib/HistoryGrid.hs @@ -0,0 +1,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 -- cgit v1.2.3