From aca843ed3ff90409d216d9da2a3be961533bc513 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 19 May 2017 16:07:30 -0400 Subject: [PATCH] Add some combinators for working with SGR codes. --- src/Data/Syntax/Assignment.hs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Data/Syntax/Assignment.hs b/src/Data/Syntax/Assignment.hs index af5a4fcfc..de43a7a82 100644 --- a/src/Data/Syntax/Assignment.hs +++ b/src/Data/Syntax/Assignment.hs @@ -167,7 +167,7 @@ data ErrorCause symbol -- | Pretty-print an Error with reference to the source where it occurred. showError :: Show symbol => Source.Source -> Error symbol -> ShowS showError source Error{..} - = showSourcePos errorPos . showString ": " . showString (setSGRCode [SetColor Foreground Vivid Red]) . showString "error" . showString (setSGRCode []) . showString ": " . showExpectation . showChar '\n' + = showSourcePos errorPos . showString ": " . withSGRCode [SetColor Foreground Vivid Red] "error" . showString ": " . showExpectation . showChar '\n' . showString context -- actualLines results include line endings, so no newline here . showString (replicate (succ (Info.column errorPos + lineNumberDigits)) ' ') . showChar '^' . showChar '\n' . showString (prettyCallStack callStack) . showChar '\n' @@ -179,6 +179,8 @@ showError source Error{..} context = maybe "\n" (toS . Source.sourceText . sconcat) (nonEmpty [ Source.Source (toS (showLineNumber i)) <> Source.Source ": " <> l | (i, l) <- zip [1..] (Source.actualLines source), inRange (Info.line errorPos - 2, Info.line errorPos) i ]) showLineNumber n = let s = show n in replicate (lineNumberDigits - length s) ' ' <> s lineNumberDigits = succ (floor (logBase 10 (fromIntegral (Info.line errorPos) :: Double))) + showSGRCode = showString . setSGRCode + withSGRCode code string = showSGRCode code . showString string . showSGRCode [] showSymbols :: Show symbol => [symbol] -> ShowS showSymbols [] = showString "end of input nodes"