module Matterhorn.Clipboard
  ( copyToClipboard
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Control.Exception ( try )
import qualified Data.Text as T
import           System.Hclip ( setClipboard, ClipboardException(..) )

import           Matterhorn.Types


copyToClipboard :: Text -> MH ()
copyToClipboard :: Text -> MH ()
copyToClipboard Text
txt = do
  Either ClipboardException ()
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => IO a -> IO (Either e a)
try (String -> IO ()
setClipboard (Text -> String
T.unpack Text
txt)))
  case Either ClipboardException ()
result of
    Left ClipboardException
e -> do
      let errMsg :: Text
errMsg = case ClipboardException
e of
            UnsupportedOS String
_ ->
              Text
"Matterhorn does not support yanking on this operating system."
            ClipboardException
NoTextualData ->
              Text
"Textual data is required to set the clipboard."
            MissingCommands [String]
cmds ->
              Text
"Could not set clipboard due to missing one of the " forall a. Semigroup a => a -> a -> a
<>
              Text
"required program(s): " forall a. Semigroup a => a -> a -> a
<> (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show [String]
cmds)
      MHError -> MH ()
mhError forall a b. (a -> b) -> a -> b
$ Text -> MHError
ClipboardError Text
errMsg
    Right () ->
      forall (m :: * -> *) a. Monad m => a -> m a
return ()