This commit is contained in:
Julian Ospald 2016-04-26 12:23:47 +02:00
parent 8bcdb84efd
commit a452b44cfe
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 48 additions and 16 deletions

View File

@ -37,9 +37,11 @@ library
bytestring,
containers,
data-default,
errors,
filepath >= 1.3.0.0,
hinotify,
hpath,
monad-loops,
mtl >= 2.2,
old-locale >= 1,
posix-paths,

View File

@ -16,6 +16,7 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
--}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK ignore-exports #-}
module HSFM.Settings.Bookmarks where
@ -34,11 +35,11 @@ import Data.ByteString
import Data.Maybe
(
catMaybes
, fromJust
)
import Data.Word8
(
_lf
, _nul
_nul
)
import qualified HPath as P
import HPath
@ -50,6 +51,10 @@ import HPath
import HSFM.FileSystem.FileOperations
import HSFM.FileSystem.FileType
import Prelude hiding (readFile, writeFile)
import System.Posix.Env.ByteString
(
getEnv
)
@ -62,12 +67,12 @@ data Bookmark = MkBookmark {
-- |Parses bookmarks from a ByteString that has pairs of
-- name and path. Name and path are separated by null character
-- and the pairs istelf are separated by newline `_lf` from
-- name and path. Name and path are separated by one null character
-- and the pairs itself are separated by two null characters from
-- each other.
bkParser :: Parser [Bookmark]
bkParser =
fmap catMaybes $ many' (fmap toBm $ bookmark <* word8 _lf)
fmap catMaybes $ many' (fmap toBm $ bookmark <* word8 _nul <* word8 _nul)
where
toBm :: (ByteString, ByteString) -> Maybe Bookmark
toBm (name, path) = MkBookmark <$> P.parseFn name
@ -78,20 +83,24 @@ bkParser =
<$> many1' char
<* (word8 _nul)
<*> many1' char
char = satisfy (`notElem` [_nul, _lf])
char = satisfy (`notElem` [_nul])
-- |Writes bookmarks to a give file.
writeBookmarksToFile :: Path Abs -> [Bookmark] -> IO ()
writeBookmarksToFile _ [] = return ()
writeBookmarksToFile p bs = do
let str = foldr1 (\x y -> x `BS.append` BS.singleton _lf
-- |Writes bookmarks to a given file.
writeBookmarks :: [Bookmark] -> IO ()
writeBookmarks [] = return ()
writeBookmarks bs = do
bf <- bookmarksFile
bfd <- bookmarksDir
mkdirP bfd
readFile getFileInfo bfd >>= (\x -> createFile x bookmarksFileName)
let str = foldr1 (\x y -> x `BS.append` BS.pack [_nul, _nul]
`BS.append`
y `BS.append` BS.singleton _lf)
y `BS.append` BS.pack [_nul, _nul])
(fmap toByteString bs)
file <- readFile getFileInfo p
file <- readFile getFileInfo bf
void $ writeFile file str
where
where
toByteString :: Bookmark -> ByteString
toByteString b = (P.fromRel $ bkName b)
`BS.append` BS.singleton _nul
@ -99,11 +108,32 @@ writeBookmarksToFile p bs = do
-- |Reads bookmarks from a given file.
readBookmarksFromFile :: Path Abs -> IO [Bookmark]
readBookmarksFromFile p = do
readBookmarks :: IO [Bookmark]
readBookmarks = do
p <- bookmarksFile
file <- readFile getFileInfo p
c <- readFileContents file
case parseOnly bkParser c of
Left _ -> return []
Right x -> return x
bookmarksDir :: IO (Path Abs)
bookmarksDir = do
mhomedir <- getEnv "HOME"
case mhomedir of
Nothing -> ioError (userError "No valid homedir?!")
Just home -> do
phome <- P.parseAbs home
reldir <- P.parseRel ".config/hsfm"
return $ phome P.</> reldir
bookmarksFile :: IO (Path Abs)
bookmarksFile = do
path <- bookmarksDir
return $ path P.</> bookmarksFileName
bookmarksFileName :: Path Fn
bookmarksFileName = fromJust $ P.parseFn "bookmarks"