add: avoid excess whitespace between transactions (#46)

This commit is contained in:
Simon Michael 2011-09-25 17:47:55 +00:00
parent 7e753e13b5
commit fffb15c1ae

View File

@ -197,26 +197,24 @@ askFor prompt def validator = do
journalAddTransaction :: Journal -> CliOpts -> Transaction -> IO Journal journalAddTransaction :: Journal -> CliOpts -> Transaction -> IO Journal
journalAddTransaction j@Journal{jtxns=ts} opts t = do journalAddTransaction j@Journal{jtxns=ts} opts t = do
let f = journalFilePath j let f = journalFilePath j
appendToJournalFile f $ showTransaction t appendToJournalFileOrStdout f $ showTransaction t
when (debug_ opts) $ do when (debug_ opts) $ do
putStrLn $ printf "\nAdded transaction to %s:" f putStrLn $ printf "\nAdded transaction to %s:" f
putStrLn =<< registerFromString (show t) putStrLn =<< registerFromString (show t)
return j{jtxns=ts++[t]} return j{jtxns=ts++[t]}
-- | Append data to a journal file; or if the file is "-", dump it to stdout. -- | Append a string, typically one or more transactions, to a journal
appendToJournalFile :: FilePath -> String -> IO () -- file, or if the file is "-", dump it to stdout. Tries to avoid
appendToJournalFile f s = -- excess whitespace.
if f == "-" appendToJournalFileOrStdout :: FilePath -> String -> IO ()
then putStr $ sep ++ s appendToJournalFileOrStdout f s
else appendFile f $ sep++s | f == "-" = putStr s'
where | otherwise = appendFile f s'
-- appendFile means we don't need file locking to be where s' = "\n" ++ ensureOneNewlineTerminated s
-- multi-user-safe, but also that we can't figure out the minimal
-- number of newlines needed as separator -- | Replace a string's 0 or more terminating newlines with exactly one.
sep = "\n\n" ensureOneNewlineTerminated :: String -> String
-- sep | null $ strip t = "" ensureOneNewlineTerminated = (++"\n") . reverse . dropWhile (=='\n') . reverse
-- | otherwise = replicate (2 - min 2 (length lastnls)) '\n'
-- where lastnls = takeWhile (=='\n') $ reverse t
-- | Convert a string of journal data into a register report. -- | Convert a string of journal data into a register report.
registerFromString :: String -> IO String registerFromString :: String -> IO String