Created
August 2, 2015 22:25
-
-
Save berdario/c3ddcfbe1a9252c11052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
import System.Environment (getArgs, getEnv) | |
import Control.Exception (catch, SomeException) | |
import Data.Map.Strict (fromList, lookup, findWithDefault, insert, assocs) | |
import Prelude hiding (lookup) | |
partitionPairs' Nothing ys (x:xs) = partitionPairs' (Just x) ys xs | |
partitionPairs' Nothing ys [] = ys | |
partitionPairs' (Just x) ys (y:xs) = partitionPairs' Nothing ((x,y):ys) xs | |
partitionPairs' (Just _) ys [] = error "corrupt ~/.jumphs" | |
partitionPairs = partitionPairs' Nothing [] | |
updateBookmarks bookmarks _ [] = bookmarks | |
updateBookmarks bookmarks bookmark [dir] = insert bookmark dir bookmarks | |
main = do | |
args@(bookmark:xs) <- getArgs | |
jumpdb <- fmap (++ "/.jumphs") $ getEnv "HOME" | |
bookmarks <- readFile jumpdb `catch` (\(_ :: SomeException) -> return "") | |
bookmarks <- return $ fromList $ partitionPairs $ lines bookmarks | |
bookmarks <- return $ updateBookmarks bookmarks bookmark xs | |
bookmarks `seq` writeFile jumpdb $ unlines $ Prelude.map (\(a,b) -> a++"\n"++b) $ assocs bookmarks | |
putStrLn $ findWithDefault "" bookmark bookmarks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment