1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
6 , P.align, P.cat, P.empty, P.fill, P.fillSep, P.hang
7 , P.hcat, P.hsep, P.indent, P.sep, P.vcat
8 , P.red, P.cyan, P.magenta, P.green, P.blue, P.black, P.yellow
9 , P.dullred, P.dullcyan, P.dullyellow
47import Prelude hiding (cycle)
48import qualified Data.List as List
49import Data.Monoid ((<>))
50import qualified Data.Name as Name
51import qualified System.Console.ANSI.Types as Ansi
52import qualified System.Info as Info
53import System.IO (Handle)
54import qualified Text.PrettyPrint.ANSI.Leijen as P
56import qualified Data.Index as Index
57import qualified Elm.Package as Pkg
58import qualified Elm.Version as V
59import qualified Json.Encode as E
60import qualified Json.String as Json
67fromChars :: String -> P.Doc
72fromName :: Name.Name -> P.Doc
74 P.text (Name.toChars name)
77fromVersion :: V.Version -> P.Doc
79 P.text (V.toChars vsn)
82fromPackage :: Pkg.Name -> P.Doc
84 P.text (Pkg.toChars pkg)
87fromInt :: Int -> P.Doc
96toAnsi :: Handle -> P.Doc -> IO ()
98 P.displayIO handle (P.renderPretty 1 80 doc)
101toString :: P.Doc -> String
103 P.displayS (P.renderPretty 1 80 (P.plain doc)) ""
106toLine :: P.Doc -> String
108 P.displayS (P.renderPretty 1 (div maxBound 2) (P.plain doc)) ""
115stack :: [P.Doc] -> P.Doc
117 P.vcat (List.intersperse "" docs)
120reflow :: String -> P.Doc
122 P.fillSep (map P.text (words paragraph))
125commaSep :: P.Doc -> (P.Doc -> P.Doc) -> [P.Doc] -> [P.Doc]
126commaSep conjunction addStyle names =
132 [ addStyle name1, conjunction, addStyle name2 ]
135 map (\name -> addStyle name <> ",") (init names)
138 , addStyle (last names)
146toSimpleNote :: String -> P.Doc
147toSimpleNote message =
148 toFancyNote (map P.text (words message))
151toFancyNote :: [P.Doc] -> P.Doc
153 P.fillSep (P.underline "Note" <> ":" : chunks)
160toSimpleHint :: String -> P.Doc
161toSimpleHint message =
162 toFancyHint (map P.text (words message))
165toFancyHint :: [P.Doc] -> P.Doc
167 P.fillSep (P.underline "Hint" <> ":" : chunks)
174link :: String -> String -> String -> String -> P.Doc
175link word before fileName after =
177 (P.underline (P.text word) <> ":")
178 : map P.text (words before)
179 ++ P.text (makeLink fileName)
180 : map P.text (words after)
183fancyLink :: String -> [P.Doc] -> String -> [P.Doc] -> P.Doc
184fancyLink word before fileName after =
186 (P.underline (P.text word) <> ":") : before ++ P.text (makeLink fileName) : after
189makeLink :: [Char] -> [Char]
191 "<https://elm-lang.org/" <> V.toChars V.compiler <> "/" <> fileName <> ">"
194makeNakedLink :: [Char] -> [Char]
195makeNakedLink fileName =
196 "https://elm-lang.org/" <> V.toChars V.compiler <> "/" <> fileName
199reflowLink :: [Char] -> [Char] -> [Char] -> P.Doc
200reflowLink before fileName after =
202 map P.text (words before)
203 ++ P.text (makeLink fileName)
204 : map P.text (words after)
213 show n <> if n == 1 then " argument" else " arguments"
216moreArgs :: Int -> String
218 show n <> " more" <> if n == 1 then " argument" else " arguments"
221ordinal :: Index.ZeroBased -> String
223 intToOrdinal (Index.toHuman index)
226intToOrdinal :: Int -> String
236 | remainder100 `elem` [11..13] = "th"
237 | remainder10 == 1 = "st"
238 | remainder10 == 2 = "nd"
239 | remainder10 == 3 = "rd"
242 show number <> ending
246cycle :: Int -> Name.Name -> [Name.Name] -> P.Doc
247cycle indent name names =
249 toLn n = cycleLn <> P.dullyellow (fromName n)
251 P.indent indent $ P.vcat $
252 cycleTop : List.intersperse cycleMid (toLn name : map toLn names) ++ [ cycleEnd ]
255cycleTop, cycleLn, cycleMid, cycleEnd :: P.Doc
256cycleTop = if isWindows then "+-----+" else "┌─────┐"
257cycleLn = if isWindows then "| " else "│ "
258cycleMid = if isWindows then "| |" else "│ ↓"
259cycleEnd = if isWindows then "+-<---+" else "└─────┘"
271encode :: P.Doc -> E.Value
273 E.array (toJsonHelp noStyle [] (P.renderPretty 1 80 doc))
280 , _color :: Maybe Color
286 Style False False Nothing
308toJsonHelp :: Style -> [a] -> P.SimpleDoc -> [E.Value]
309toJsonHelp style revChunks simpleDoc =
310 error "TODO generate json on errors" style revChunks simpleDoc sgrToStyle encodeColor
315 "according to the main implementation, @SFail@ can not\
316 \ appear uncaught in a rendered @SimpleDoc@"
319 [ encodeChunks style revChunks ]
322 toJsonHelp style (TB.singleton char : revChunks) rest
324 P.SText _ string rest ->
325 toJsonHelp style (TB.fromString string : revChunks) rest
327 P.SLine indent rest ->
328 toJsonHelp style (spaces indent : "\n" : revChunks) rest
331 encodeChunks style revChunks : toJsonHelp (sgrToStyle sgrs style) [] rest
334spaces :: Int -> TB.Builder
336 TB.fromText (Text.replicate n " ")
339sgrToStyle :: [Ansi.SGR] -> Style -> Style
340sgrToStyle sgrs style@(Style bold underline color) =
348 Ansi.Reset -> noStyle
349 Ansi.SetConsoleIntensity i -> Style (isBold i) underline color
350 Ansi.SetItalicized _ -> style
351 Ansi.SetUnderlining u -> Style bold (isUnderline u) color
352 Ansi.SetBlinkSpeed _ -> style
353 Ansi.SetVisible _ -> style
354 Ansi.SetSwapForegroundBackground _ -> style
355 Ansi.SetColor l i c -> Style bold underline (toColor l i c)
356 Ansi.SetRGBColor _ _ -> style
359isBold :: Ansi.ConsoleIntensity -> Bool
362 Ansi.BoldIntensity -> True
363 Ansi.FaintIntensity -> False
364 Ansi.NormalIntensity -> False
367isUnderline :: Ansi.Underlining -> Bool
368isUnderline underlining =
370 Ansi.SingleUnderline -> True
371 Ansi.DoubleUnderline -> False
372 Ansi.NoUnderline -> False
375toColor :: Ansi.ConsoleLayer -> Ansi.ColorIntensity -> Ansi.Color -> Maybe Color
376toColor layer intensity color =
390 Ansi.Red -> pick Red RED
391 Ansi.Magenta -> pick Magenta MAGENTA
392 Ansi.Yellow -> pick Yellow YELLOW
393 Ansi.Green -> pick Green GREEN
394 Ansi.Cyan -> pick Cyan CYAN
395 Ansi.Blue -> pick Blue BLUE
396 Ansi.White -> pick White WHITE
397 Ansi.Black -> pick Black BLACK
400encodeChunks :: Style -> [TB.Builder] -> E.Value
401encodeChunks (Style bold underline color) revChunks =
403 !str = Utf8.concat (reverse revChunks)
406 Nothing | not bold && not underline ->
411 [ ("bold", E.bool bold)
412 , ("underline", E.bool underline)
413 , ("color", maybe E.null encodeColor color)
414 , ("string", E.string str)
418encodeColor :: Color -> E.Value
420 E.string $ Json.fromChars $
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list