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
|
|
|
-
|
2016-09-09 17:21:38 +00:00
|
|
|
- Copyright 2010-2016 Joey Hess <id@joeyh.name>
|
2015-04-03 20:48:30 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
2016-09-09 19:49:44 +00:00
|
|
|
import Messages.JSON
|
|
|
|
|
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 ()
|
|
|
|
outputMessage 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
|
2016-09-09 22:13:55 +00:00
|
|
|
JSONOutput _ -> void $ outputJSON 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.
|
|
|
|
outputJSON :: JSONBuilder -> MessageState -> Annex Bool
|
|
|
|
outputJSON jsonbuilder s = case outputType s of
|
|
|
|
JSONOutput _
|
|
|
|
| endjson -> do
|
|
|
|
Annex.changeState $ \st ->
|
|
|
|
st { Annex.output = s { jsonBuffer = Nothing } }
|
|
|
|
maybe noop (liftIO . flushed . emit) 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
|
|
|
|
2015-11-04 17:45:34 +00:00
|
|
|
outputError :: String -> Annex ()
|
2016-09-09 16:57:42 +00:00
|
|
|
outputError msg = withMessageState $ \s ->
|
|
|
|
if concurrentOutputEnabled s
|
|
|
|
then concurrentMessage s True msg go
|
|
|
|
else 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
|