1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
18import Control.Arrow ((***))
19import qualified Data.ByteString.Char8 as BSC
20import qualified Data.ByteString.Builder as B
21import qualified Data.Map as Map
22import Data.Monoid ((<>))
23import qualified Data.Text as Text
24import qualified Numeric
25import System.IO (IOMode(WriteMode), withBinaryFile)
34 | Object [(String, Value)]
39array :: [Value] -> Value
44object :: [(String,Value)] -> Value
49text :: Text.Text -> Value
51 String (encodeString (Text.unpack txt))
54string :: String -> Value
56 String (encodeString str)
64dict :: (k -> String) -> (v -> Value) -> Map.Map k v -> Value
65dict encodeKey encodeValue pairs =
66 Object $ map (encodeKey *** encodeValue) (Map.toList pairs)
69list :: (a -> Value) -> [a] -> Value
70list encodeEntry entries =
71 Array $ map encodeEntry entries
78write :: FilePath -> Value -> IO ()
80 withBinaryFile path WriteMode $ \handle ->
81 B.hPutBuilder handle (encode value)
88encode :: Value -> B.Builder
93encodeHelp :: BSC.ByteString -> Value -> B.Builder
94encodeHelp indent value =
99 Array (first : rest) ->
100 encodeArray indent first rest
105 Object (first : rest) ->
106 encodeObject indent first rest
112 B.string7 (if boolean then "true" else "false")
119encodeArray :: BSC.ByteString -> Value -> [Value] -> B.Builder
121 encodeSequence arrayOpen arrayClose encodeHelp
124arrayOpen :: B.Builder
129arrayClose :: B.Builder
138encodeObject :: BSC.ByteString -> (String, Value) -> [(String, Value)] -> B.Builder
140 encodeSequence objectOpen objectClose encodeField
143objectOpen :: B.Builder
148objectClose :: B.Builder
153encodeField :: BSC.ByteString -> (String, Value) -> B.Builder
154encodeField indent (key, value) =
155 encodeString key <> B.string7 ": " <> encodeHelp indent value
162encodeSequence :: B.Builder -> B.Builder -> (BSC.ByteString -> a -> B.Builder) -> BSC.ByteString -> a -> [a] -> B.Builder
163encodeSequence open close encodeEntry indent first rest =
169 B.byteString newIndent
172 newline <> B.byteString indent <> close
174 addValue field builder =
178 <> encodeEntry newIndent field
182 <> encodeEntry newIndent first
183 <> foldr addValue closer rest
186commaNewline :: B.Builder
200encodeString :: String -> B.Builder
202 B.char7 '"' <> escape str <> B.char7 '"'
205escape :: String -> B.Builder
216 B.stringUtf8 front <> escapeChar char <> escape rest
219isEscape :: Char -> Bool
221 c == '\"' || c == '\\' || c < '\x20'
224escapeChar :: Char -> B.Builder
227 '\"' -> B.string7 "\\\""
228 '\\' -> B.string7 "\\\\"
229 '\n' -> B.string7 "\\n"
230 '\r' -> B.string7 "\\r"
231 '\t' -> B.string7 "\\t"
232 _ -> if char < '\x20' then toEscapeCode char else B.charUtf8 char
235toEscapeCode :: Char -> B.Builder
239 Numeric.showHex (fromEnum char) ""
242 "\\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