1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
24import Prelude hiding (null)
25import Control.Arrow ((***))
26import qualified Data.ByteString.Char8 as BSC
27import qualified Data.ByteString.Builder as B
28import qualified Data.ByteString.Builder.Prim as BP
29import Data.ByteString.Builder.Prim ((>*<))
30import qualified Data.Map as Map
31import qualified Data.Scientific as Sci
32import Data.Monoid ((<>))
33import qualified Data.Text as Text
34import qualified Data.Text.Encoding as Text
35import qualified Data.Word as W
36import System.IO (IOMode(WriteMode), withBinaryFile)
38import qualified Elm.Name as N
47 | Object [(Text.Text, Value)]
51 | Number Sci.Scientific
55array :: [Value] -> Value
60object :: [(Text.Text,Value)] -> Value
65text :: Text.Text -> Value
67 String (encodeText txt)
70name :: N.Name -> Value
72 String ("\"" <> N.toBuilder nm <> "\"")
85number :: Sci.Scientific -> Value
95dict :: (k -> Text.Text) -> (v -> Value) -> Map.Map k v -> Value
96dict encodeKey encodeValue pairs =
97 Object $ map (encodeKey *** encodeValue) (Map.toList pairs)
100list :: (a -> Value) -> [a] -> Value
101list encodeEntry entries =
102 Array $ map encodeEntry entries
109(==>) :: a -> b -> (a, b)
118write :: FilePath -> Value -> IO ()
120 withBinaryFile path WriteMode $ \handle ->
121 B.hPutBuilder handle (encode value)
124writeUgly :: FilePath -> Value -> IO ()
125writeUgly path value =
126 withBinaryFile path WriteMode $ \handle ->
127 B.hPutBuilder handle (encodeUgly value)
134encodeUgly :: Value -> B.Builder
140 Array (first : rest) ->
143 B.char7 ',' <> encodeUgly entry
145 B.char7 '[' <> encodeUgly first <> mconcat (map encodeEntry rest) <> B.char7 ']'
150 Object (first : rest) ->
152 encodeEntry char (key, entry) =
153 B.char7 char <> encodeText key <> B.char7 ':' <> encodeUgly entry
155 encodeEntry '{' first <> mconcat (map (encodeEntry ',') rest) <> B.char7 '}'
161 B.string7 (if boolean then "true" else "false")
167 B.string7 (Sci.formatScientific Sci.Generic Nothing scientific)
177encode :: Value -> B.Builder
182encodeHelp :: BSC.ByteString -> Value -> B.Builder
183encodeHelp indent value =
188 Array (first : rest) ->
189 encodeArray indent first rest
194 Object (first : rest) ->
195 encodeObject indent first rest
201 B.string7 (if boolean then "true" else "false")
207 B.string7 (Sci.formatScientific Sci.Generic Nothing scientific)
217encodeArray :: BSC.ByteString -> Value -> [Value] -> B.Builder
219 encodeSequence arrayOpen arrayClose encodeHelp
222arrayOpen :: B.Builder
227arrayClose :: B.Builder
236encodeObject :: BSC.ByteString -> (Text.Text, Value) -> [(Text.Text, Value)] -> B.Builder
238 encodeSequence objectOpen objectClose encodeField
241objectOpen :: B.Builder
246objectClose :: B.Builder
251encodeField :: BSC.ByteString -> (Text.Text, Value) -> B.Builder
252encodeField indent (key, value) =
253 encodeText key <> B.string7 ": " <> encodeHelp indent value
260encodeSequence :: B.Builder -> B.Builder -> (BSC.ByteString -> a -> B.Builder) -> BSC.ByteString -> a -> [a] -> B.Builder
261encodeSequence open close encodeEntry indent first rest =
267 B.byteString newIndent
270 newline <> B.byteString indent <> close
272 addValue field builder =
275 <> encodeEntry newIndent field
280 <> encodeEntry newIndent first
281 <> foldr addValue closer rest
284commaNewline :: B.Builder
298encodeText :: Text.Text -> B.Builder
300 B.char7 '"' <> Text.encodeUtf8BuilderEscaped escapeAscii txt <> B.char7 '"'
303{-# INLINE escapeAscii #-}
304escapeAscii :: BP.BoundedPrim W.Word8
306 BP.condB (== 0x5C {- \\ -}) (ascii2 ('\\','\\')) $
307 BP.condB (== 0x22 {- \" -}) (ascii2 ('\\','"' )) $
308 BP.condB (>= 0x20 ) (BP.liftFixedToBounded BP.word8) $
309 BP.condB (== 0x0A {- \n -}) (ascii2 ('\\','n' )) $
310 BP.condB (== 0x0D {- \r -}) (ascii2 ('\\','r' )) $
311 BP.condB (== 0x09 {- \t -}) (ascii2 ('\\','t' )) $
312 BP.liftFixedToBounded hexEscape -- fallback for chars < 0x20
314 hexEscape :: BP.FixedPrim W.Word8
315 hexEscape = (\c -> ('\\', ('u', fromIntegral c))) BP.>$<
316 BP.char8 >*< BP.char8 >*< BP.word16HexFixed
320ascii2 :: (Char, Char) -> BP.BoundedPrim a
322 BP.liftFixedToBounded $ const cs BP.>$< BP.char7 >*< BP.char7
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