Skip to content

Commit

Permalink
Retain ANSI "show cursor"-sequence when parsing summary
Browse files Browse the repository at this point in the history
This fixes a bug introduced in 559eaf6
  • Loading branch information
sol committed Oct 18, 2024
1 parent 3b6235d commit d6c821b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module Session (
, hasSpec
, hasHspecCommandSignature
, hspecCommand
, Extract(..)
, extractSummary
, ansiShowCursor
#endif
) where

Expand Down Expand Up @@ -123,16 +125,21 @@ isSuccess = not . isFailure
extractSummary :: Extract Summary
extractSummary = Extract {
isPartialMessage = partialMessageStartsWithOneOf [summaryPrefix, ansiShowCursor <> summaryPrefix]
, parseMessage = fmap (flip (,) "") . parseSummary
, parseMessage
} where
summaryPrefix :: ByteString
summaryPrefix = "Summary {"

parseMessage :: ByteString -> Maybe (Summary, ByteString)
parseMessage input = case ByteString.stripPrefix ansiShowCursor input of
Nothing -> flip (,) "" <$> parseSummary input
Just i -> flip (,) ansiShowCursor <$> parseSummary i

parseSummary :: ByteString -> Maybe Summary
parseSummary = readMaybe . decodeUtf8 . stripAnsiShowCursor

ansiShowCursor :: ByteString
ansiShowCursor = "\ESC[?25h"

stripAnsiShowCursor :: ByteString -> ByteString
stripAnsiShowCursor input = fromMaybe input $ ByteString.stripPrefix ansiShowCursor input

ansiShowCursor :: ByteString
ansiShowCursor = "\ESC[?25h"
15 changes: 15 additions & 0 deletions test/SessionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,18 @@ spec = do
withSession [name, "--no-color", "-m", "foo"] $ \session -> do
_ <- runSpec session >> runSpec session
hspecPreviousSummary session `shouldReturn` Just (Summary 1 0)

describe "extractSummary" $ do
let
summary :: Summary
summary = Summary 0 0

input :: ByteString
input = fromString $ show summary

it "extracts summary" $ do
extractSummary.parseMessage input `shouldBe` Just (summary, "")

context "when the input starts with the ANSI \"show cursor\"-sequence" $ do
it "extracts summary" $ do
extractSummary.parseMessage (ansiShowCursor <> input) `shouldBe` Just (summary, ansiShowCursor)

0 comments on commit d6c821b

Please sign in to comment.