2015-11-05 21:22:45 +00:00
|
|
|
{- git-annex output messages, including concurrent output to display regions
|
|
|
|
-
|
2017-05-16 19:28:06 +00:00
|
|
|
- Copyright 2010-2017 Joey Hess <id@joeyh.name>
|
2015-11-05 21:22:45 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-11-05 21:22:45 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
2016-02-14 19:02:42 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2015-11-05 21:22:45 +00:00
|
|
|
|
|
|
|
module Messages.Concurrent where
|
|
|
|
|
2017-05-16 19:28:06 +00:00
|
|
|
import Types
|
2016-02-15 19:27:58 +00:00
|
|
|
import Types.Messages
|
2017-05-16 19:28:06 +00:00
|
|
|
import qualified Annex
|
2015-11-05 21:22:45 +00:00
|
|
|
|
2015-11-10 17:42:39 +00:00
|
|
|
import Common
|
2015-11-05 21:22:45 +00:00
|
|
|
import qualified System.Console.Concurrent as Console
|
|
|
|
import qualified System.Console.Regions as Regions
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import qualified Data.Text as T
|
2016-02-15 19:06:54 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2016-02-14 19:02:42 +00:00
|
|
|
import GHC.IO.Encoding
|
2015-11-05 21:22:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{- Outputs a message in a concurrency safe way.
|
|
|
|
-
|
|
|
|
- The message may be an error message, in which case it goes to stderr.
|
|
|
|
-
|
|
|
|
- When built without concurrent-output support, the fallback action is run
|
|
|
|
- instead.
|
|
|
|
-}
|
2016-09-09 16:57:42 +00:00
|
|
|
concurrentMessage :: MessageState -> Bool -> String -> Annex () -> Annex ()
|
|
|
|
concurrentMessage s iserror msg fallback
|
|
|
|
| concurrentOutputEnabled s =
|
2016-02-14 19:02:42 +00:00
|
|
|
go =<< consoleRegion <$> Annex.getState Annex.output
|
|
|
|
| otherwise = fallback
|
2015-11-05 21:22:45 +00:00
|
|
|
where
|
|
|
|
go Nothing
|
|
|
|
| iserror = liftIO $ Console.errorConcurrent msg
|
|
|
|
| otherwise = liftIO $ Console.outputConcurrent msg
|
|
|
|
go (Just r) = do
|
|
|
|
-- Can't display the error to stdout while
|
|
|
|
-- console regions are in use, so set the errflag
|
|
|
|
-- to get it to display to stderr later.
|
|
|
|
when iserror $ do
|
2016-09-09 16:57:42 +00:00
|
|
|
Annex.changeState $ \st ->
|
|
|
|
st { Annex.output = (Annex.output st) { consoleRegionErrFlag = True } }
|
2015-11-06 00:18:27 +00:00
|
|
|
liftIO $ atomically $ do
|
|
|
|
Regions.appendConsoleRegion r msg
|
|
|
|
rl <- takeTMVar Regions.regionList
|
|
|
|
putTMVar Regions.regionList
|
|
|
|
(if r `elem` rl then rl else r:rl)
|
2015-11-05 21:22:45 +00:00
|
|
|
|
|
|
|
{- Runs an action in its own dedicated region of the console.
|
|
|
|
-
|
|
|
|
- The region is closed at the end or on exception, and at that point
|
|
|
|
- the value of the region is displayed in the scrolling area above
|
|
|
|
- any other active regions.
|
|
|
|
-
|
|
|
|
- When not at a console, a region is not displayed until the action is
|
|
|
|
- complete.
|
|
|
|
-}
|
2016-09-09 16:57:42 +00:00
|
|
|
inOwnConsoleRegion :: MessageState -> Annex a -> Annex a
|
|
|
|
inOwnConsoleRegion s a
|
|
|
|
| concurrentOutputEnabled s = do
|
2016-02-14 19:02:42 +00:00
|
|
|
r <- mkregion
|
|
|
|
setregion (Just r)
|
|
|
|
eret <- tryNonAsync a `onException` rmregion r
|
|
|
|
case eret of
|
|
|
|
Left e -> do
|
|
|
|
-- Add error message to region before it closes.
|
2016-09-09 16:57:42 +00:00
|
|
|
concurrentMessage s True (show e) noop
|
2016-02-14 19:02:42 +00:00
|
|
|
rmregion r
|
|
|
|
throwM e
|
|
|
|
Right ret -> do
|
|
|
|
rmregion r
|
|
|
|
return ret
|
|
|
|
| otherwise = a
|
2015-11-05 21:22:45 +00:00
|
|
|
where
|
2015-11-06 00:18:27 +00:00
|
|
|
-- The region is allocated here, but not displayed until
|
|
|
|
-- a message is added to it. This avoids unnecessary screen
|
|
|
|
-- updates when a region does not turn out to need to be used.
|
|
|
|
mkregion = Regions.newConsoleRegion Regions.Linear ""
|
2016-09-09 16:57:42 +00:00
|
|
|
setregion r = Annex.changeState $ \st -> st
|
|
|
|
{ Annex.output = (Annex.output st) { consoleRegion = r } }
|
2015-11-05 21:22:45 +00:00
|
|
|
rmregion r = do
|
|
|
|
errflag <- consoleRegionErrFlag <$> Annex.getState Annex.output
|
|
|
|
let h = if errflag then Console.StdErr else Console.StdOut
|
2016-09-09 16:57:42 +00:00
|
|
|
Annex.changeState $ \st -> st
|
|
|
|
{ Annex.output = (Annex.output st) { consoleRegionErrFlag = False } }
|
2015-11-05 21:22:45 +00:00
|
|
|
setregion Nothing
|
|
|
|
liftIO $ atomically $ do
|
|
|
|
t <- Regions.getConsoleRegion r
|
|
|
|
unless (T.null t) $
|
|
|
|
Console.bufferOutputSTM h t
|
|
|
|
Regions.closeConsoleRegion r
|
|
|
|
|
2015-11-06 17:44:57 +00:00
|
|
|
{- The progress region is displayed inline with the current console region. -}
|
|
|
|
withProgressRegion :: (Regions.ConsoleRegion -> Annex a) -> Annex a
|
|
|
|
withProgressRegion a = do
|
|
|
|
parent <- consoleRegion <$> Annex.getState Annex.output
|
|
|
|
Regions.withConsoleRegion (maybe Regions.Linear Regions.InLine parent) a
|
|
|
|
|
2015-11-05 21:22:45 +00:00
|
|
|
instance Regions.LiftRegion Annex where
|
|
|
|
liftRegion = liftIO . atomically
|
2016-02-14 19:02:42 +00:00
|
|
|
|
|
|
|
{- The concurrent-output library uses Text, which bypasses the normal use
|
|
|
|
- of the fileSystemEncoding to roundtrip invalid characters, when in a
|
|
|
|
- non-unicode locale. Work around that problem by avoiding using
|
|
|
|
- concurrent output when not in a unicode locale. -}
|
|
|
|
concurrentOutputSupported :: IO Bool
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
concurrentOutputSupported = do
|
|
|
|
enc <- getLocaleEncoding
|
|
|
|
return ("UTF" `isInfixOf` textEncodingName enc)
|
|
|
|
#else
|
|
|
|
concurrentOutputSupported = return True -- Windows is always unicode
|
|
|
|
#endif
|
2017-05-16 19:28:06 +00:00
|
|
|
|
|
|
|
{- Hide any currently displayed console regions while running the action,
|
2019-07-05 19:09:37 +00:00
|
|
|
- so that the action can use the console itself. -}
|
2018-11-15 18:26:40 +00:00
|
|
|
hideRegionsWhile :: MessageState -> Annex a -> Annex a
|
|
|
|
hideRegionsWhile s a
|
|
|
|
| concurrentOutputEnabled s = bracketIO setup cleanup go
|
|
|
|
| otherwise = a
|
2017-05-16 19:28:06 +00:00
|
|
|
where
|
|
|
|
setup = Regions.waitDisplayChange $ swapTMVar Regions.regionList []
|
|
|
|
cleanup = void . atomically . swapTMVar Regions.regionList
|
|
|
|
go _ = do
|
|
|
|
liftIO $ hFlush stdout
|
|
|
|
a
|