From 1afb84c95d0f8d395117d160476b77a22b660d93 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 17 Apr 2021 14:51:24 -1000 Subject: [PATCH] lib: outputFileFromOpts now returns a maybe; cleanups --- hledger/Hledger/Cli/CliOptions.hs | 12 +++++++----- hledger/Hledger/Cli/Utils.hs | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index e4dabca84..b482c24e9 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -555,17 +555,19 @@ expandPathPreservingPrefix d prefixedf = do Just p -> p ++ ":" ++ f' Nothing -> f' --- | Get the expanded, absolute output file path from options, --- or the default (-, meaning stdout). -outputFileFromOpts :: CliOpts -> IO FilePath +-- | Get the expanded, absolute output file path specified by an +-- -o/--output-file options, or nothing, meaning stdout. +outputFileFromOpts :: CliOpts -> IO (Maybe FilePath) outputFileFromOpts opts = do d <- getCurrentDirectory case output_file_ opts of - Just p -> expandPath d p - Nothing -> return "-" + Nothing -> return Nothing + Just f -> Just <$> expandPath d f +defaultOutputFormat :: String defaultOutputFormat = "txt" +outputFormats :: [String] outputFormats = [defaultOutputFormat] ++ ["csv" diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index 4bb092ce5..404898a9e 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -161,7 +161,7 @@ journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j = writeOutput :: CliOpts -> String -> IO () writeOutput opts s = do f <- outputFileFromOpts opts - (if f == "-" then putStr else writeFile f) s + (maybe putStr writeFile f) s -- | Write some output to stdout or to a file selected by --output-file. -- If the file exists it will be overwritten. This function operates on Lazy @@ -169,7 +169,7 @@ writeOutput opts s = do writeOutputLazyText :: CliOpts -> TL.Text -> IO () writeOutputLazyText opts s = do f <- outputFileFromOpts opts - (if f == "-" then TL.putStr else TL.writeFile f) s + (maybe TL.putStr TL.writeFile f) s -- -- | Get a journal from the given string and options, or throw an error. -- readJournal :: CliOpts -> String -> IO Journal