on 1925e31 help sign in
elk/src/Reporting/Report.hs
111 lines 2.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Report
4 ( Report
5 , report
6 , toString
7 , toJson
8 , toHandle
9 )
10 where
12import Data.Aeson ((.=))
13import qualified Data.Aeson.Types as Json
14import qualified Data.Text as Text
15import Data.Text (Text)
16import System.IO (Handle)
17import Text.PrettyPrint.ANSI.Leijen
18 ( Doc, (<>), displayS, displayIO, dullcyan
19 , hardline, plain, renderPretty, text
20 )
22import qualified Reporting.Helpers as Help
23import qualified Reporting.Region as R
24import qualified Reporting.Render.Code as Code
28-- BUILD REPORTS
31data Report = Report
32 { _title :: Text
33 , _highlight :: Maybe R.Region
34 , _preHint :: Doc
35 , _postHint :: Doc
36 }
39report :: Text -> Maybe R.Region -> Text -> Doc -> Report
40report title highlight pre post =
41 Report title highlight (Help.reflowParagraph pre) post
45-- REPORT TO JSON
48toJson :: [Json.Pair] -> Report -> (Maybe R.Region, [Json.Pair])
49toJson extraFields (Report title subregion pre post) =
50 let
51 fields =
52 [ "tag" .= title
53 , "overview" .= nonAnsiRender pre
54 , "details" .= nonAnsiRender post
55 ]
56 in
57 (subregion, fields ++ extraFields)
61-- REPORT TO DOC
64toDoc :: String -> R.Region -> Report -> Text -> Doc
65toDoc location region (Report title highlight preHint postHint) source =
66 messageBar title location
67 <> hardline <> hardline <>
68 preHint
69 <> hardline <> hardline <>
70 Code.render highlight region source
71 <> hardline <>
72 postHint
73 <> hardline <> hardline
76messageBar :: Text -> String -> Doc
77messageBar tag location =
78 let
79 usedSpace =
80 4 + Text.length tag + 1 + length location
81 in
82 dullcyan $ text $
83 "-- " ++ Text.unpack tag
84 ++ " " ++ replicate (max 1 (80 - usedSpace)) '-'
85 ++ " " ++ location
89-- RENDER DOCS
92toHandle :: Handle -> String -> R.Region -> Report -> Text -> IO ()
93toHandle handle location region rprt source =
94 displayIO
95 handle
96 (renderPretty 1 80 (toDoc location region rprt source))
99toString :: String -> R.Region -> Report -> Text -> String
100toString location region rprt source =
101 nonAnsiRender (toDoc location region rprt source)
105-- DOC TO STRING WITH NO ANSI CODES
108nonAnsiRender :: Doc -> String
109nonAnsiRender doc =
110 displayS (renderPretty 1 80 (plain doc)) ""