diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 477b84ca7..6a68560ac 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -87,7 +87,6 @@ This is used by the import command. module Hledger.Read ( -- * Journal files - PrefixedFilePath, defaultJournal, defaultJournalWith, defaultJournalSafely, @@ -227,10 +226,6 @@ defaultJournalPath = do home <- fromMaybe "" <$> getHomeSafe return $ home journalDefaultFilename --- | A file path optionally prefixed by a reader name and colon --- (journal:, csv:, timedot:, etc.). -type PrefixedFilePath = FilePath - -- | @readJournal iopts mfile txt@ -- -- Read a Journal from some handle, with strict checks if enabled, diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 7b7efbfe0..7bddc2ad8 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -30,6 +30,8 @@ Some of these might belong in Hledger.Read.JournalReader or Hledger.Read. --- ** exports module Hledger.Read.Common ( Reader (..), + PrefixedFilePath, + isStdin, InputOpts(..), HasInputOpts(..), definputopts, @@ -193,6 +195,17 @@ data Reader m = Reader { instance Show (Reader m) where show r = show (rFormat r) ++ " reader" +-- | A file path optionally prefixed by a reader name and colon (journal:, csv:, timedot:, etc.). +-- The file path part can also be - meaning standard input. +type PrefixedFilePath = FilePath + +-- | Is this the special file path meaning standard input ? (-, possibly prefixed) +isStdin :: PrefixedFilePath -> Bool +isStdin f = case splitAtElement ':' f of + [_,"-"] -> True + ["-"] -> True + _ -> False + -- | Parse an InputOpts from a RawOpts and a provided date. -- This will fail with a usage error if the forecast period expression cannot be parsed. rawOptsToInputOpts :: Day -> Bool -> Bool -> RawOpts -> InputOpts diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 8f2741ca8..deed48299 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -167,14 +167,6 @@ findReader Nothing (Just path) = (prefix,path') = splitReaderPrefix path ext = map toLower $ drop 1 $ takeExtension path' --- | A prefix used to specify a particular reader to be used for a file path, --- overriding the file extension. It is a valid reader name followed by a colon. --- Eg journal:, csv:, timeclock:, timedot:. --- type ReaderPrefix = String - --- | A file path with an optional reader prefix. -type PrefixedFilePath = FilePath - -- | Separate a file path and its reader prefix, if any. -- -- >>> splitReaderPrefix "csv:-" diff --git a/hledger/Hledger/Cli/Commands/Run.hs b/hledger/Hledger/Cli/Commands/Run.hs index 405957ed8..d55c68aa0 100644 --- a/hledger/Hledger/Cli/Commands/Run.hs +++ b/hledger/Hledger/Cli/Commands/Run.hs @@ -241,7 +241,7 @@ withJournalCached defaultJournalOverride cliopts cmd = do return (cache, journal) Nothing -> do dbg1IO ("readAndCacheJournalFile reading and caching "++fp) iopts - journal <- runExceptT $ if snd (splitReaderPrefix fp) == "-" then readStdin else readJournalFile iopts fp + journal <- runExceptT $ if isStdin fp then readStdin else readJournalFile iopts fp either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal where -- InputOptions contain reportspan_ that is used to calculate forecast period, @@ -257,7 +257,7 @@ withJournalCached defaultJournalOverride cliopts cmd = do -- forecast transactions are never generated before journal end -- unless specifically requested). Just forecastspan -> forecastspan `spanValidDefaultsFrom` reportspan_ iopts - -- Read stdin, or if we read it alread, use a cache + -- Read stdin, or if we read it already, use a cache -- readStdin :: InputOpts -> ExceptT String IO Journal readStdin = do stdinContent <- liftIO $ modifyMVar stdinCache $ \cache ->