1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
20import Control.Arrow ((***))
21import qualified Data.ByteString.Char8 as BSC
22import qualified Data.ByteString.Builder as B
23import qualified Data.Map as Map
24import Data.Monoid ((<>))
25import qualified Data.Text as Text
26import qualified Numeric
27import System.IO (IOMode(WriteMode), withBinaryFile)
36 | Object [(String, Value)]
42array :: [Value] -> Value
47object :: [(String,Value)] -> Value
52text :: Text.Text -> Value
54 String (encodeString (Text.unpack txt))
57string :: String -> Value
59 String (encodeString str)
72dict :: (k -> String) -> (v -> Value) -> Map.Map k v -> Value
73dict encodeKey encodeValue pairs =
74 Object $ map (encodeKey *** encodeValue) (Map.toList pairs)
77list :: (a -> Value) -> [a] -> Value
78list encodeEntry entries =
79 Array $ map encodeEntry entries
86(==>) :: a -> b -> (a, b)
95write :: FilePath -> Value -> IO ()
97 withBinaryFile path WriteMode $ \handle ->
98 B.hPutBuilder handle (encode value)
105encode :: Value -> B.Builder
110encodeHelp :: BSC.ByteString -> Value -> B.Builder
111encodeHelp indent value =
116 Array (first : rest) ->
117 encodeArray indent first rest
122 Object (first : rest) ->
123 encodeObject indent first rest
129 B.string7 (if boolean then "true" else "false")
139encodeArray :: BSC.ByteString -> Value -> [Value] -> B.Builder
141 encodeSequence arrayOpen arrayClose encodeHelp
144arrayOpen :: B.Builder
149arrayClose :: B.Builder
158encodeObject :: BSC.ByteString -> (String, Value) -> [(String, Value)] -> B.Builder
160 encodeSequence objectOpen objectClose encodeField
163objectOpen :: B.Builder
168objectClose :: B.Builder
173encodeField :: BSC.ByteString -> (String, Value) -> B.Builder
174encodeField indent (key, value) =
175 encodeString key <> B.string7 ": " <> encodeHelp indent value
182encodeSequence :: B.Builder -> B.Builder -> (BSC.ByteString -> a -> B.Builder) -> BSC.ByteString -> a -> [a] -> B.Builder
183encodeSequence open close encodeEntry indent first rest =
189 B.byteString newIndent
192 newline <> B.byteString indent <> close
194 addValue field builder =
198 <> encodeEntry newIndent field
202 <> encodeEntry newIndent first
203 <> foldr addValue closer rest
206commaNewline :: B.Builder
220encodeString :: String -> B.Builder
222 B.char7 '"' <> escape str <> B.char7 '"'
225escape :: String -> B.Builder
236 B.stringUtf8 front <> escapeChar char <> escape rest
239isEscape :: Char -> Bool
241 c == '\"' || c == '\\' || c < '\x20'
244escapeChar :: Char -> B.Builder
247 '\"' -> B.string7 "\\\""
248 '\\' -> B.string7 "\\\\"
249 '\n' -> B.string7 "\\n"
250 '\r' -> B.string7 "\\r"
251 '\t' -> B.string7 "\\t"
252 _ -> if char < '\x20' then toEscapeCode char else B.charUtf8 char
255toEscapeCode :: Char -> B.Builder
259 Numeric.showHex (fromEnum char) ""
262 "\\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