From fc93a4c644f4358ad7de6a685653451280ba0f3a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 1 Jul 2025 16:40:35 -0700 Subject: [PATCH] fix:add: show green prompts properly instead of ANSI codes [#2410] --- hledger-lib/Hledger/Utils/IO.hs | 2 +- hledger/Hledger/Cli/Commands/Add.hs | 15 ++++++++++----- hledger/hledger.m4.md | 7 ++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index 6c21684bb..0ad91e577 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -763,7 +763,7 @@ useColorOnStderrUnsafe = unsafePerformIO useColorOnStderr -- | Detect whether ANSI should be used on stdout using useColorOnStdoutUnsafe, -- and if so prepend and append the given SGR codes to a string. --- Currently used in a few places (the commands list, the demo command, the recentassertions error message); +-- Currently used in a few places (the commands list, the recentassertions error message, add, demo); -- see useColorOnStdoutUnsafe's limitations. ansiWrapUnsafe :: SGRString -> SGRString -> String -> String ansiWrapUnsafe pre post s = if useColorOnStdoutUnsafe then pre<>s<>post else s diff --git a/hledger/Hledger/Cli/Commands/Add.hs b/hledger/Hledger/Cli/Commands/Add.hs index 449b5e354..3c53051c6 100644 --- a/hledger/Hledger/Cli/Commands/Add.hs +++ b/hledger/Hledger/Cli/Commands/Add.hs @@ -264,7 +264,7 @@ confirmedTransactionWizard prevInput es@EntryState{..} stack@(currentStage : _) retryMsg "Please enter y or n." $ parser ((fmap (\c -> if c == '<' then Nothing else Just c)) . headMay . map toLower . strip) $ defaultTo' def $ nonEmpty $ - line $ green' $ printf "Save this transaction to the journal ?%s: " (showDefault def) + line' $ green' $ printf "Save this transaction to the journal ?%s: " (showDefault def) case y of Just 'y' -> return t Just _ -> throw RestartTransactionException @@ -272,6 +272,11 @@ confirmedTransactionWizard prevInput es@EntryState{..} stack@(currentStage : _) where replaceNthOrAppend n newElem xs = take n xs ++ [newElem] ++ drop (n + 1) xs +-- | A workaround we seem to need for #2410 right now: wizards' input-reading functions disrupt ANSI codes +-- somehow, so these variants first print the ANSI coded prompt as ordinary output, then do the input with no prompt. +line' prompt = output prompt >> line "" +linePrewritten' prompt beforetxt aftertxt = output prompt >> linePrewritten "" beforetxt aftertxt + dateAndCodeWizard PrevInput{..} EntryState{..} = do let def = headDef (T.unpack $ showDate esDefDate) esArgs retryMsg "A valid hledger smart date is required. Eg: 2022-08-30, 8/30, 30, yesterday." $ @@ -280,7 +285,7 @@ dateAndCodeWizard PrevInput{..} EntryState{..} = do defaultTo' def $ nonEmpty $ maybeExit $ -- maybeShowHelp $ - linePrewritten (green' $ printf "Date%s: " (showDefault def)) (fromMaybe "" prevDateAndCode) "" + linePrewritten' (green' $ printf "Date%s: " (showDefault def)) (fromMaybe "" prevDateAndCode) "" where parseSmartDateAndCode refdate s = if s == "<" then return Nothing else either (const Nothing) (\(d,c) -> return $ Just (fixSmartDate refdate d, c)) edc where @@ -299,7 +304,7 @@ descriptionAndCommentWizard PrevInput{..} EntryState{..} = do let def = headDef "" esArgs s <- withCompletion (descriptionCompleter esJournal def) $ defaultTo' def $ nonEmpty $ - linePrewritten (green' $ printf "Description%s: " (showDefault def)) (fromMaybe "" prevDescAndCmnt) "" + linePrewritten' (green' $ printf "Description%s: " (showDefault def)) (fromMaybe "" prevDescAndCmnt) "" if s == "<" then return Nothing else do @@ -322,7 +327,7 @@ accountWizard PrevInput{..} EntryState{..} = do parser (parseAccountOrDotOrNull def canfinish) $ withCompletion (accountCompleter esJournal def) $ defaultTo' def $ -- nonEmpty $ - linePrewritten (green' $ printf "Account %d%s%s: " pnum (endmsg::String) (showDefault def)) (fromMaybe "" $ prevAccount `atMay` length esPostings) "" + linePrewritten' (green' $ printf "Account %d%s%s: " pnum (endmsg::String) (showDefault def)) (fromMaybe "" $ prevAccount `atMay` length esPostings) "" where canfinish = not (null esPostings) && postingsBalanced esPostings parseAccountOrDotOrNull :: String -> Bool -> String -> Maybe (Maybe String) @@ -362,7 +367,7 @@ amountAndCommentWizard previnput@PrevInput{..} entrystate@EntryState{..} = do withCompletion (amountCompleter def) $ defaultTo' def $ nonEmpty $ - linePrewritten (green' $ printf "Amount %d%s: " pnum (showDefault def)) (fromMaybe "" $ prevAmountAndCmnt `atMay` length esPostings) "" + linePrewritten' (green' $ printf "Amount %d%s: " pnum (showDefault def)) (fromMaybe "" $ prevAmountAndCmnt `atMay` length esPostings) "" where -- Custom parser that combines with Wizard to use IO via outputLn parser' f a = a >>= \input -> diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 3cbf9ba04..f6b88ab85 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -7026,11 +7026,8 @@ Some known issues and limitations: A system locale with a suitable text encoding must be configured to work with non-ascii data. (See Text encoding, Troubleshooting.) -On Microsoft Windows, depending whether you are running in a CMD window or a Cygwin/MSYS/Mintty window -and how you installed hledger, -non-ascii characters and colours may not be supported, -and the tab key may not be supported by `hledger add`. -(Running in a WSL window should resolve these.) +On Microsoft Windows, depending what kind of terminal window you use, +non-ascii characters, ANSI text formatting, and/or the add command's TAB key for completion, may not be supported. When processing large data files, hledger uses more memory than Ledger.