2012-04-27 17:23:52 +00:00
|
|
|
{- git-annex Messages data types
|
|
|
|
-
|
2020-12-03 17:01:28 +00:00
|
|
|
- Copyright 2012-2020 Joey Hess <id@joeyh.name>
|
2012-04-27 17:23:52 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-04-27 17:23:52 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Types.Messages where
|
|
|
|
|
Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale
As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.
It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.
This commit was supported by the NSF-funded DataLad project.
2018-04-16 19:42:45 +00:00
|
|
|
import qualified Utility.Aeson as Aeson
|
2020-12-03 17:01:28 +00:00
|
|
|
import Utility.Metered
|
2015-04-03 23:56:56 +00:00
|
|
|
|
2017-05-11 21:33:18 +00:00
|
|
|
import Control.Concurrent
|
2015-11-04 20:19:00 +00:00
|
|
|
import System.Console.Regions (ConsoleRegion)
|
2020-12-03 18:47:04 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2015-11-04 20:19:00 +00:00
|
|
|
|
2020-12-03 17:01:28 +00:00
|
|
|
data OutputType
|
|
|
|
= NormalOutput
|
|
|
|
| QuietOutput
|
|
|
|
| JSONOutput JSONOptions
|
2020-12-04 18:54:09 +00:00
|
|
|
| SerializedOutput
|
|
|
|
(SerializedOutput -> IO ())
|
|
|
|
(IO (Maybe SerializedOutputResponse))
|
2012-04-27 17:23:52 +00:00
|
|
|
|
2018-02-19 18:03:23 +00:00
|
|
|
data JSONOptions = JSONOptions
|
|
|
|
{ jsonProgress :: Bool
|
2018-02-19 18:28:17 +00:00
|
|
|
, jsonErrorMessages :: Bool
|
2018-02-19 18:03:23 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
adjustOutputType :: OutputType -> OutputType -> OutputType
|
|
|
|
adjustOutputType (JSONOutput old) (JSONOutput new) = JSONOutput $ JSONOptions
|
|
|
|
{ jsonProgress = jsonProgress old || jsonProgress new
|
2018-02-19 18:28:17 +00:00
|
|
|
, jsonErrorMessages = jsonErrorMessages old || jsonErrorMessages new
|
2018-02-19 18:03:23 +00:00
|
|
|
}
|
|
|
|
adjustOutputType _old new = new
|
|
|
|
|
2012-04-27 17:23:52 +00:00
|
|
|
data SideActionBlock = NoBlock | StartBlock | InBlock
|
2012-11-25 21:54:08 +00:00
|
|
|
deriving (Eq)
|
2012-04-27 17:23:52 +00:00
|
|
|
|
|
|
|
data MessageState = MessageState
|
|
|
|
{ outputType :: OutputType
|
2016-09-09 16:57:42 +00:00
|
|
|
, concurrentOutputEnabled :: Bool
|
2012-04-27 17:23:52 +00:00
|
|
|
, sideActionBlock :: SideActionBlock
|
2015-11-04 20:19:00 +00:00
|
|
|
, consoleRegion :: Maybe ConsoleRegion
|
|
|
|
, consoleRegionErrFlag :: Bool
|
2016-09-09 22:13:55 +00:00
|
|
|
, jsonBuffer :: Maybe Aeson.Object
|
2017-05-11 21:33:18 +00:00
|
|
|
, promptLock :: MVar () -- left full when not prompting
|
2021-06-08 16:48:30 +00:00
|
|
|
, clearProgressMeter :: IO ()
|
2012-04-27 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:33:18 +00:00
|
|
|
newMessageState :: IO MessageState
|
|
|
|
newMessageState = do
|
|
|
|
promptlock <- newMVar ()
|
|
|
|
return $ MessageState
|
2015-11-06 16:51:25 +00:00
|
|
|
{ outputType = NormalOutput
|
2016-09-09 16:57:42 +00:00
|
|
|
, concurrentOutputEnabled = False
|
2015-11-06 16:51:25 +00:00
|
|
|
, sideActionBlock = NoBlock
|
|
|
|
, consoleRegion = Nothing
|
|
|
|
, consoleRegionErrFlag = False
|
2016-09-09 22:13:55 +00:00
|
|
|
, jsonBuffer = Nothing
|
2017-05-11 21:33:18 +00:00
|
|
|
, promptLock = promptlock
|
2021-06-08 16:48:30 +00:00
|
|
|
, clearProgressMeter = return ()
|
2015-11-06 16:51:25 +00:00
|
|
|
}
|
2020-12-03 17:01:28 +00:00
|
|
|
|
2020-12-03 18:57:22 +00:00
|
|
|
-- | When communicating with a child process over a pipe while it is
|
|
|
|
-- performing some action, this is used to pass back output that the child
|
|
|
|
-- would normally display to the console.
|
2020-12-03 17:01:28 +00:00
|
|
|
data SerializedOutput
|
2020-12-03 18:47:04 +00:00
|
|
|
= OutputMessage S.ByteString
|
2020-12-03 17:01:28 +00:00
|
|
|
| OutputError String
|
2020-12-11 16:52:22 +00:00
|
|
|
| BeginProgressMeter
|
2020-12-11 16:39:00 +00:00
|
|
|
| UpdateProgressMeterTotalSize TotalSize
|
2020-12-11 16:52:22 +00:00
|
|
|
| UpdateProgressMeter BytesProcessed
|
2020-12-04 17:50:03 +00:00
|
|
|
| EndProgressMeter
|
2020-12-11 16:39:00 +00:00
|
|
|
| BeginPrompt
|
2020-12-04 18:54:09 +00:00
|
|
|
| EndPrompt
|
2020-12-03 18:47:04 +00:00
|
|
|
| JSONObject L.ByteString
|
2020-12-03 18:57:22 +00:00
|
|
|
-- ^ This is always sent, it's up to the consumer to decide if it
|
|
|
|
-- wants to display JSON, or human-readable messages.
|
2020-12-09 19:44:00 +00:00
|
|
|
deriving (Show)
|
2020-12-04 18:54:09 +00:00
|
|
|
|
|
|
|
data SerializedOutputResponse
|
|
|
|
= ReadyPrompt
|
2020-12-09 19:44:00 +00:00
|
|
|
deriving (Eq, Show)
|