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 <- IO (Either ClipboardException ())
-> MH (Either ClipboardException ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO (Either ClipboardException ())
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 _ ->
              Text
"Matterhorn does not support yanking on this operating system."
            ClipboardException
NoTextualData ->
              Text
"Textual data is required to set the clipboard."
            MissingCommands cmds ->
              Text
"Could not set clipboard due to missing one of the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
              Text
"required program(s): " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall a. Show a => a -> String
show [String]
cmds)
      MHError -> MH ()
mhError (MHError -> MH ()) -> MHError -> MH ()
forall a b. (a -> b) -> a -> b
$ Text -> MHError
ClipboardError Text
errMsg
    Right () ->
      () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()