hledger
Safe HaskellNone
LanguageGHC2021

Hledger.Cli.Utils

Description

Utilities for top-level modules and ghci. See also Hledger.Read and Hledger.Utils.

Synopsis

Documentation

unsupportedOutputFormatError :: String -> String Source #

Standard error message for a bad output format specified with -O/-o. If it looks like a file name with a known format's extension, it suggests -o.

withJournal :: CliOpts -> (Journal -> IO a) -> IO a Source #

Parse the user's specified journal file(s) as a Journal, maybe apply some transformations according to options, and run a hledger command with it. Or, throw an error.

Sets _journaldir on the iopts used for this read, so CSV sourcearchive rules find the journal's @data directory. If the callback subsequently performs another readJournalFile / readJournalFiles call (eg in the import command), it should apply inputOptsSetJournalDir to those iopts using the loaded journal — the local opts.inputopts_ does not yet carry _journaldir@ at the call site here.

withJournalDo :: CliOpts -> (Journal -> IO a) -> IO a Source #

Deprecated: renamed, please use withJournal instead

withPossibleJournal :: CliOpts -> (Journal -> IO a) -> IO a Source #

Like withJournal, but if the first journal file does not exist, provides an empty journal with that file path set. This is useful for commands like add and import that need to work with a potentially non-existent first journal file, while still reading all specified files (for completions, etc).

See withJournal for the note about inputOptsSetJournalDir and secondary reads.

writeOutput :: CliOpts -> String -> IO () Source #

Write some output to stdout or to a file selected by --output-file. If the file exists it will be overwritten.

writeOutputLazyText :: CliOpts -> Text -> IO () Source #

Write some output, to a file specified by --output-file if any, otherwise to stdout. If writing to a file and the file exists, it will be overwritten. If writing to stdout, a pager is used when appropriate and possible.

warnIfLargeMultiPeriodReport :: ReportSpec -> Journal -> IO () Source #

If a multi-period report has an implausible number of periods (more than 10,000; in practice this means a mistyped date in the journal, making the report span thousands of years), print a warning on stderr before starting it, so the user can cancel (#1683). Such a report can need a lot of memory (a balance report needs a column per period). The periods are counted without building the report, in constant memory.

This is called explicitly by each command that runs such a report (balance, the compound balance commands, stats, roi, activity), rather than living in the report code: the report functions are pure, so warning from them would need unsafePerformIO or trace, with no guarantee the message appears before the work starts. Doing it in the command's IO, before the report is forced, keeps the ordering reliable. New multi-period report commands should call this too.

withTitle :: ReportOpts -> Text -> Text Source #

Prepend the effective report heading (followed by a blank line) to a report's lazy text output, if non-empty. Used by reports whose default heading is empty; pass the explicit --title value or "" via effectiveTitle.

writeOutputWithTitle :: CliOpts -> Text -> IO () Source #

Write some text output with writeOutput, after the effective report heading if any (see withTitle). For commands producing simple text, like lists of names.

journalTransform :: CliOpts -> Journal -> Journal Source #

Apply some journal transformations, if enabled by options, that should happen late. These happen after parsing, finalising the journal, strict checks, and .latest filtering/updating, but before report calculation. They are, in processing order: --pivot, --anonymise error message, --obfuscate, and preparing lot detail (converting to cost basis for -B or --gain, then, when --lots is off, collapsing it).

Why not do these in journalFinalise ? That step is supposed to check the data's intrinsic correctness, regardless of view options; whereas here we assume correctness and are just transforming the view (based only on InputOpts). XXX But it's easy to forget to call this. Current callers include withJournal, journalReload, uiReload, withJournalCached.

journalPrepareLotDetail :: ReportOpts -> Bool -> Journal -> Journal Source #

Prepare a journal's lot detail for reports with these options: convert lot postings to their cost basis where -B or --gain needs it, then, unless keeplots is true, collapse lot detail (journalCollapseLotDetail). hledger, hledger-ui, and hledger-web all display lots this way, so that they report the same amounts.

journalReload :: CliOpts -> ExceptT String IO Journal Source #

Re-read the journal file(s) specified by options, applying any transformations specified by options. Or return an error string. Reads the full journal, without filtering.

Sets _journaldir on the iopts used for this read. Callers that subsequently do another readJournalFile / readJournalFiles call should apply inputOptsSetJournalDir to those iopts (see withJournal).

journalCheckChange :: CliOpts -> FilePath -> Text -> ExceptT String IO Journal Source #

Check a change to one of the journal's files before writing it: re-read the journal as journalReload would after this text was written to this file (which need not exist yet), without writing anything. Return the journal that read gives, or its error message. So the change passes exactly the checks the next read will make, with these options (balancing, balance assertions, lots, strict checks, etc), in the context of all the journal's files. The text should have n line endings.

journalReloadIfChanged :: CliOpts -> Day -> Journal -> ExceptT String IO (Journal, Bool) Source #

Re-read the option-specified journal file(s), but only if any of them has changed since last read. (If the file is standard input, this will either do nothing or give an error, not tested yet). Returns a journal or error message, and a flag indicating whether it was re-read or not. Like withJournal and journalReload, reads the full journal, without filtering.

journalFileIsNewer :: Journal -> FilePath -> IO Bool Source #

Has the specified file changed since the journal was last read ? Typically this is one of the journal's journalFilePaths. These are not always real files, so the file's existence is tested first; for non-files the answer is always no.

maybeFileModificationTime :: FilePath -> IO (Maybe POSIXTime) Source #

Get the last modified time of the specified file, if it exists.

openBrowserOn :: String -> IO ExitCode Source #

Attempt to open a web browser on the given url, all platforms. On Windows this goes through the open-browser package, which asks the Win32 API to open the url. Elsewhere it runs the platform's launchers in turn until one exits successfully (one that is not installed counts as failing): open on mac; `xdg-open` and then some older launchers that may be installed on Linux. If nothing starts, print the url instead.

writeFileWithBackup :: FilePath -> String -> IO () Source #

Back up this file with a (incrementing) numbered suffix, then overwrite it with this new text, or give an error.

writeFileWithBackupIfChanged :: FilePath -> Text -> IO Bool Source #

Back up this file with a (incrementing) numbered suffix then overwrite it with this new text, or give an error, but only if the text is different from the current file contents, and return a flag indicating whether we did anything.

The given text should have unix line endings (n); the existing file content will be normalised to unix line endings before comparing the two. If the file is overwritten, the new file will have the current system's native line endings (n on unix, rn on windows). This could be different from the file's previous line endings, if working with a DOS file on unix or vice-versa.

postingsOrTransactionsReportAsText :: Bool -> CliOpts -> (Int -> Int -> (a, [WideBuilder], [WideBuilder]) -> Builder) -> (a -> MixedAmount) -> (a -> MixedAmount) -> [a] -> Builder Source #

Render a PostingsReport or AccountTransactionsReport as Text, determining the appropriate starting widths and increasing as necessary.