module Hbro.History (
Entry(..),
log,
add,
parseEntry,
select
) where
import Hbro.Core
import Hbro.Types
import Hbro.Util
import Control.Exception
import Data.Functor
import Data.List
import Data.Time
import Network.URI
import Prelude hiding(log)
import System.IO
import System.Locale
data Entry = Entry {
mTime :: LocalTime,
mURI :: URI,
mTitle :: String
}
instance Show Entry where
show (Entry time uri title) = unwords [(formatTime defaultTimeLocale dateFormat time), show uri, title]
dateFormat :: String
dateFormat = "%F %T"
log :: PortableFilePath -> K ()
log file = withURI $ \uri -> withTitle $ \title -> io $ do
timeZone <- utcToLocalTime <$> getCurrentTimeZone
now <- timeZone <$> getCurrentTime
add file (Entry now uri title) >> return ()
add :: PortableFilePath
-> Entry
-> IO Bool
add file newEntry = do
file' <- resolve file
result <- try $ withFile file' AppendMode (`hPutStrLn` show newEntry)
either (\e -> errorHandler file' e >> return False) (const $ return True) result
parseEntry :: String -> Maybe Entry
parseEntry [] = Nothing
parseEntry line = (parseEntry' . words) line
parseEntry' :: [String] -> Maybe Entry
parseEntry' (d:t:u:t') = do
time <- parseTime defaultTimeLocale dateFormat (unwords [d, t])
uri <- parseURI u
return $ Entry time uri (unwords t')
parseEntry' _ = Nothing
select :: PortableFilePath
-> [String]
-> IO (Maybe Entry)
select file dmenuOptions = do
file' <- resolve file
result <- try $ readFile file'
either (\e -> errorHandler file' e >> return Nothing) (return . return) result
>>= (return . ((return . unlines . reverse . sort . nub . lines) =<<))
>>= (maybe (return Nothing) (dmenu dmenuOptions))
>>= (return . (parseEntry =<<))