From 98acc016a67690b73c721dbbe43c2616e56cd918 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Tue, 6 Jan 2026 22:52:43 +0000 Subject: [PATCH] Justify test time to right of terminal --- haskell/Main.hs | 5 ++++- haskell/Pre.hs | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/haskell/Main.hs b/haskell/Main.hs index 6ef10c7..bd3021d 100644 --- a/haskell/Main.hs +++ b/haskell/Main.hs @@ -16,11 +16,14 @@ import Puzzles.Day6 qualified as Day6 import Puzzles.Day7 qualified as Day7 import Puzzles.Day8 qualified as Day8 import Puzzles.Day9 qualified as Day9 +import System.Console.ANSI (getTerminalSize) main :: IO () main = do + -- terminalWidth <- fmap snd <$> getTerminalSize -- TODO this doesn't work in GHCID or GHCIWatch... + terminalWidth <- pure $ Just 60 putStrLn $ drawTree $ show <$> getTestTree tests - TL.putStrLn . displayTestResultsConsole =<< runTests () tests + TL.putStrLn . displayTestResultsConsole terminalWidth =<< runTests () tests tests :: TestTree IO () tests = diff --git a/haskell/Pre.hs b/haskell/Pre.hs index 64fe062..230aaeb 100644 --- a/haskell/Pre.hs +++ b/haskell/Pre.hs @@ -279,23 +279,23 @@ instance Show SomeExceptionLegalShow where getTestTree :: TestTree m r -> Tree TestName getTestTree (TestTree name _ ts) = Node name $ map getTestTree ts -displayTestResultsConsole :: TestResult -> TL.Text -displayTestResultsConsole testResult = +displayTestResultsConsole :: Maybe Int -> TestResult -> TL.Text +displayTestResultsConsole terminalWidth testResult = displayResult 0 testResult <> TL.pack (setSGRCode [Reset]) where displayResult indent = - (TL.replicate indent " " <>) . \case + (TL.replicate (fromIntegral indent) " " <>) . \case Pass (TestName name) dt children -> - TL.fromStrict (header Green '✓' name (Just dt)) + TL.fromStrict (header Green '✓' name indent (Just dt)) <> TL.concat (map (displayResult (indent + 1)) children) Fail (TestName name) (SomeExceptionLegalShow e) -> TL.fromStrict - ( header Red '✗' name Nothing + ( header Red '✗' name indent Nothing <> setColour Vivid Red ) <> TL.show e <> "\n" - header colour icon name time = + header colour icon name indent time = setColour Vivid colour <> T.singleton icon <> " " @@ -304,7 +304,14 @@ displayTestResultsConsole testResult = <> maybe mempty ( \_t@(showTime -> tt) -> - " " + T.replicate + ( fromIntegral $ + maybe + 3 + (\n -> n - (2 * indent + length name + T.length tt + 2)) + terminalWidth + ) + " " <> setColour Dull Black <> tt )