1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
21import Prelude hiding (null)
22import Control.Arrow ((***))
23import qualified Data.ByteString.Char8 as BSC
24import qualified Data.ByteString.Builder as B
25import qualified Data.Map as Map
26import Data.Monoid ((<>))
27import qualified Data.Text as Text
28import qualified Numeric
29import System.IO (IOMode(WriteMode), withBinaryFile)
38 | Object [(String, Value)]
45array :: [Value] -> Value
50object :: [(String,Value)] -> Value
55text :: Text.Text -> Value
57 String (encodeString (Text.unpack txt))
60string :: String -> Value
62 String (encodeString str)
80dict :: (k -> String) -> (v -> Value) -> Map.Map k v -> Value
81dict encodeKey encodeValue pairs =
82 Object $ map (encodeKey *** encodeValue) (Map.toList pairs)
85list :: (a -> Value) -> [a] -> Value
86list encodeEntry entries =
87 Array $ map encodeEntry entries
94(==>) :: a -> b -> (a, b)
103write :: FilePath -> Value -> IO ()
105 withBinaryFile path WriteMode $ \handle ->
106 B.hPutBuilder handle (encode value)
113encode :: Value -> B.Builder
118encodeHelp :: BSC.ByteString -> Value -> B.Builder
119encodeHelp indent value =
124 Array (first : rest) ->
125 encodeArray indent first rest
130 Object (first : rest) ->
131 encodeObject indent first rest
137 B.string7 (if boolean then "true" else "false")
150encodeArray :: BSC.ByteString -> Value -> [Value] -> B.Builder
152 encodeSequence arrayOpen arrayClose encodeHelp
155arrayOpen :: B.Builder
160arrayClose :: B.Builder
169encodeObject :: BSC.ByteString -> (String, Value) -> [(String, Value)] -> B.Builder
171 encodeSequence objectOpen objectClose encodeField
174objectOpen :: B.Builder
179objectClose :: B.Builder
184encodeField :: BSC.ByteString -> (String, Value) -> B.Builder
185encodeField indent (key, value) =
186 encodeString key <> B.string7 ": " <> encodeHelp indent value
193encodeSequence :: B.Builder -> B.Builder -> (BSC.ByteString -> a -> B.Builder) -> BSC.ByteString -> a -> [a] -> B.Builder
194encodeSequence open close encodeEntry indent first rest =
200 B.byteString newIndent
203 newline <> B.byteString indent <> close
205 addValue field builder =
208 <> encodeEntry newIndent field
213 <> encodeEntry newIndent first
214 <> foldr addValue closer rest
217commaNewline :: B.Builder
231encodeString :: String -> B.Builder
233 B.char7 '"' <> escape str <> B.char7 '"'
236escape :: String -> B.Builder
247 B.stringUtf8 front <> escapeChar char <> escape rest
250isEscape :: Char -> Bool
252 c == '\"' || c == '\\' || c < '\x20'
255escapeChar :: Char -> B.Builder
258 '\"' -> B.string7 "\\\""
259 '\\' -> B.string7 "\\\\"
260 '\n' -> B.string7 "\\n"
261 '\r' -> B.string7 "\\r"
262 '\t' -> B.string7 "\\t"
263 _ -> if char < '\x20' then toEscapeCode char else B.charUtf8 char
266toEscapeCode :: Char -> B.Builder
270 Numeric.showHex (fromEnum char) ""
273 "\\u" ++ replicate (4 - length hex) '0' ++ hex
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