2015-11-04 20:19:00 +00:00
|
|
|
{- git-annex output messages, including concurrent output to display regions
|
2015-04-03 20:48:30 +00:00
|
|
|
-
|
2018-02-19 19:39:52 +00:00
|
|
|
- Copyright 2010-2018 Joey Hess <id@joeyh.name>
|
2015-04-03 20:48:30 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-04-03 20:48:30 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Messages.Internal where
|
|
|
|
|
|
|
|
import Common
|
2015-11-04 18:52:07 +00:00
|
|
|
import Annex
|
2015-04-03 20:48:30 +00:00
|
|
|
import Types.Messages
|
2015-11-05 21:22:45 +00:00
|
|
|
import Messages.Concurrent
|
2018-02-19 18:59:30 +00:00
|
|
|
import qualified Messages.JSON as JSON
|
|
|
|
import Messages.JSON (JSONBuilder)
|
2016-09-09 19:49:44 +00:00
|
|
|
|
2016-09-09 16:57:42 +00:00
|
|
|
withMessageState :: (MessageState -> Annex a) -> Annex a
|
|
|
|
withMessageState a = Annex.getState Annex.output >>= a
|
2015-11-04 18:52:07 +00:00
|
|
|
|
2016-09-09 22:13:55 +00:00
|
|
|
outputMessage :: JSONBuilder -> String -> Annex ()
|
2018-02-06 17:03:55 +00:00
|
|
|
outputMessage = outputMessage' bufferJSON
|
|
|
|
|
|
|
|
outputMessage' :: (JSONBuilder -> MessageState -> Annex Bool) -> JSONBuilder -> String -> Annex ()
|
|
|
|
outputMessage' jsonoutputter jsonbuilder msg = withMessageState $ \s -> case outputType s of
|
2016-09-09 16:57:42 +00:00
|
|
|
NormalOutput
|
|
|
|
| concurrentOutputEnabled s -> concurrentMessage s False msg q
|
|
|
|
| otherwise -> liftIO $ flushed $ putStr msg
|
2018-02-06 17:03:55 +00:00
|
|
|
JSONOutput _ -> void $ jsonoutputter jsonbuilder s
|
2016-09-09 18:21:06 +00:00
|
|
|
QuietOutput -> q
|
|
|
|
|
2016-09-09 22:13:55 +00:00
|
|
|
-- Buffer changes to JSON until end is reached and then emit it.
|
2018-02-06 17:03:55 +00:00
|
|
|
bufferJSON :: JSONBuilder -> MessageState -> Annex Bool
|
|
|
|
bufferJSON jsonbuilder s = case outputType s of
|
2018-02-19 18:59:30 +00:00
|
|
|
JSONOutput jsonoptions
|
2016-09-09 22:13:55 +00:00
|
|
|
| endjson -> do
|
|
|
|
Annex.changeState $ \st ->
|
|
|
|
st { Annex.output = s { jsonBuffer = Nothing } }
|
2018-02-19 18:59:30 +00:00
|
|
|
maybe noop (liftIO . flushed . JSON.emit . JSON.finalize jsonoptions) json
|
2016-09-09 18:21:06 +00:00
|
|
|
return True
|
|
|
|
| otherwise -> do
|
2016-09-09 22:13:55 +00:00
|
|
|
Annex.changeState $ \st ->
|
|
|
|
st { Annex.output = s { jsonBuffer = json } }
|
2016-09-09 18:21:06 +00:00
|
|
|
return True
|
|
|
|
_ -> return False
|
2016-09-09 19:49:44 +00:00
|
|
|
where
|
2016-09-09 22:13:55 +00:00
|
|
|
(json, endjson) = case jsonbuilder i of
|
|
|
|
Nothing -> (jsonBuffer s, False)
|
|
|
|
(Just (j, e)) -> (Just j, e)
|
|
|
|
i = case jsonBuffer s of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just b -> Just (b, False)
|
2016-09-09 19:06:54 +00:00
|
|
|
|
2018-02-06 17:03:55 +00:00
|
|
|
-- Immediately output JSON.
|
|
|
|
outputJSON :: JSONBuilder -> MessageState -> Annex Bool
|
|
|
|
outputJSON jsonbuilder s = case outputType s of
|
|
|
|
JSONOutput _ -> do
|
2018-02-19 18:59:30 +00:00
|
|
|
maybe noop (liftIO . flushed . JSON.emit)
|
2018-02-06 17:03:55 +00:00
|
|
|
(fst <$> jsonbuilder Nothing)
|
|
|
|
return True
|
|
|
|
_ -> return False
|
|
|
|
|
2015-11-04 17:45:34 +00:00
|
|
|
outputError :: String -> Annex ()
|
2018-02-19 19:28:38 +00:00
|
|
|
outputError msg = withMessageState $ \s -> case (outputType s, jsonBuffer s) of
|
|
|
|
(JSONOutput jsonoptions, Just jb) | jsonErrorMessages jsonoptions ->
|
2018-02-19 19:55:00 +00:00
|
|
|
let jb' = Just (JSON.addErrorMessage (lines msg) jb)
|
2018-02-19 19:28:38 +00:00
|
|
|
in Annex.changeState $ \st ->
|
2018-02-19 19:39:52 +00:00
|
|
|
st { Annex.output = s { jsonBuffer = jb' } }
|
2018-02-19 19:28:38 +00:00
|
|
|
_
|
|
|
|
| concurrentOutputEnabled s -> concurrentMessage s True msg go
|
|
|
|
| otherwise -> go
|
2015-11-04 17:45:34 +00:00
|
|
|
where
|
2016-09-09 16:57:42 +00:00
|
|
|
go = liftIO $ do
|
2015-11-04 17:45:34 +00:00
|
|
|
hFlush stdout
|
2016-09-09 16:57:42 +00:00
|
|
|
hPutStr stderr msg
|
2015-11-04 17:45:34 +00:00
|
|
|
hFlush stderr
|
|
|
|
|
2015-04-03 20:48:30 +00:00
|
|
|
q :: Monad m => m ()
|
|
|
|
q = noop
|
|
|
|
|
|
|
|
flushed :: IO () -> IO ()
|
|
|
|
flushed a = a >> hFlush stdout
|