aboutsummaryrefslogtreecommitdiff
path: root/Messages/Internal.hs
blob: 21d11d8113a5964a7cd1df25d6a7fad54da785e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{- git-annex output messages, including concurrent output to display regions
 -
 - Copyright 2010-2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Messages.Internal where

import Common
import Annex
import Types.Messages
import Messages.Concurrent

withMessageState :: (MessageState -> Annex a) -> Annex a
withMessageState a = Annex.getState Annex.output >>= a

outputMessage :: IO () -> String -> Annex ()
outputMessage = outputMessage' False

outputMessageFinal :: IO () -> String -> Annex ()
outputMessageFinal = outputMessage' True

outputMessage' :: Bool -> IO () -> String -> Annex ()
outputMessage' endmessage json msg = withMessageState $ \s -> case outputType s of
	NormalOutput
		| concurrentOutputEnabled s -> concurrentMessage s False msg q
		| otherwise -> liftIO $ flushed $ putStr msg
	JSONOutput -> void $ outputJSON json endmessage s
	QuietOutput -> q

outputJSON :: IO () -> Bool -> MessageState -> Annex Bool
outputJSON json endmessage s = case outputType s of
	JSONOutput
		| concurrentOutputEnabled s -> do
			-- Buffer json fragments until end is reached.
			if endmessage
				then do
					Annex.changeState $ \st -> 
						st { Annex.output = s { jsonBuffer = [] } }
					liftIO $ flushed $ do
						sequence_ $ reverse $ jsonBuffer s
						json
				else Annex.changeState $ \st ->
				        st { Annex.output = s { jsonBuffer = json : jsonBuffer s } }
			return True
		| otherwise -> do
			liftIO $ flushed json
			return True
	_ -> return False

outputError :: String -> Annex ()
outputError msg = withMessageState $ \s ->
	if concurrentOutputEnabled s
		then concurrentMessage s True msg go
		else go
  where
	go = liftIO $ do
		hFlush stdout
		hPutStr stderr msg
		hFlush stderr

q :: Monad m => m ()
q = noop

flushed :: IO () -> IO ()
flushed a = a >> hFlush stdout