From b33957ed5644042da0270c9732942414373b3d57 Mon Sep 17 00:00:00 2001 From: Matthew Pickering Date: Fri, 14 Mar 2025 16:58:43 +0000 Subject: [PATCH] Print 10 violations in normal mode Using --verbose on a large project prints out too much output (see https://github.com/haskell/cabal/issues/10744) Printing out all the violations if there are a lot of them is quite slow, printing out a lot to stdout is not that fast. Therefore we reach a compromise where we print up to 10 reasons for failure and omit the rest. If you want all the failures you can use --verbose. Fixes #48 --- FixWhitespace.hs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/FixWhitespace.hs b/FixWhitespace.hs index eebf449..6a284a3 100644 --- a/FixWhitespace.hs +++ b/FixWhitespace.hs @@ -204,7 +204,7 @@ main = do fix :: Mode -> Verbose -> TabSize -> FilePath -> IO Bool fix mode verbose tabSize f = - checkFile tabSize verbose f >>= \case + checkFile tabSize True f >>= \case CheckOK -> do when verbose $ @@ -212,7 +212,7 @@ fix mode verbose tabSize f = return False CheckViolation s vs -> do - hPutStrLn stderr (msg vs) + Text.hPutStrLn stderr (msg vs) when (mode == Fix) $ withFile f WriteMode $ \h -> do hSetEncoding h utf8 @@ -227,9 +227,20 @@ fix mode verbose tabSize f = where msg vs | mode == Fix = - "[ Violation fixed ] " ++ f + "[ Violation fixed ] " <> Text.pack f | otherwise = - "[ Violation detected ] " ++ f ++ - (if not verbose then "" else - ":\n" ++ unlines (map (Text.unpack . displayLineError f) vs)) + "[ Violation detected ]:\n" <> Text.pack f <> + (if not verbose then (displayViolations (Just 10) vs) + else (displayViolations Nothing vs)) + + + displayViolations mlimit violations = + let (display_violations, more_violations) = + case mlimit of + Just limit -> splitAt limit violations + Nothing -> (violations, []) + in Text.unlines (map (displayLineError f) display_violations) + <> case more_violations of + [] -> mempty + (_:_) -> "\n... and " <> Text.pack (show (length more_violations)) <> " more violations."