aboutsummaryrefslogtreecommitdiff
path: root/Messages/JSON.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-09-09 15:06:54 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-09-09 15:06:54 -0400
commit2408f5c6084aa04a09b36edcd264ce6bc7177c93 (patch)
treec745dce52ab6510948402eb1585b13718b39da3e /Messages/JSON.hs
parent8f8a17cf1855d809bf3655a3e4e9351b3850d3f2 (diff)
addurl, get: Added --json-progress option, which adds progress objects to the json output.
This doesn't work right when used with -J yet, and there is some really ugly hand-crafting of part of the json output.
Diffstat (limited to 'Messages/JSON.hs')
-rw-r--r--Messages/JSON.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/Messages/JSON.hs b/Messages/JSON.hs
index 0cceda3f3..7b94aa220 100644
--- a/Messages/JSON.hs
+++ b/Messages/JSON.hs
@@ -13,6 +13,7 @@ module Messages.JSON (
note,
add,
complete,
+ progress,
DualDisp(..),
ObjectMap(..),
JSONActionItem(..),
@@ -30,6 +31,8 @@ import Prelude
import qualified Utility.JSONStream as Stream
import Types.Key
+import Utility.Metered
+import Utility.Percentage
start :: String -> Maybe FilePath -> Maybe Key -> IO ()
start command file key = B.hPut stdout $ Stream.start $ Stream.AesonObject o
@@ -53,6 +56,20 @@ add = B.hPut stdout . Stream.add
complete :: Stream.JSONChunk a -> IO ()
complete v = B.hPut stdout $ Stream.start v `B.append` Stream.end
+progress :: IO () -> Integer -> BytesProcessed -> IO ()
+progress jsonbuffer size bytesprocessed = do
+ B.hPut stdout $ Stream.start $ Stream.AesonObject o
+ putStr ",\"action\":"
+ jsonbuffer
+ B.hPut stdout $ Stream.end
+ B.hPut stdout $ Stream.end
+ where
+ n = fromBytesProcessed bytesprocessed :: Integer
+ Object o = object
+ [ "byte-progress" .= n
+ , "percent-progress" .= showPercentage 2 (percentage size n)
+ ]
+
-- A value that can be displayed either normally, or as JSON.
data DualDisp = DualDisp
{ dispNormal :: String