--------------------------------------------------------------------------------

-- For operating systems that use an application on the PATH to open the URL.

--------------------------------------------------------------------------------


module Utils
  ( openBrowserWith
  ) where

import System.Exit ( ExitCode (..) )
import System.Process ( proc, waitForProcess, withCreateProcess )

openBrowserWith ::
     String
     -- ^ Name of relevant executable on the PATH.

  -> [String]
     -- ^ Arguments for executable.

  -> IO Bool
openBrowserWith :: String -> [String] -> IO Bool
openBrowserWith String
cmd [String]
args =
  CreateProcess
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO Bool)
-> IO Bool
forall a.
CreateProcess
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
withCreateProcess (String -> [String] -> CreateProcess
proc String
cmd [String]
args) ((Maybe Handle
  -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO Bool)
 -> IO Bool)
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO Bool)
-> IO Bool
forall a b. (a -> b) -> a -> b
$ \Maybe Handle
_ Maybe Handle
_ Maybe Handle
_ ProcessHandle
p ->
    (ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
== ExitCode
ExitSuccess) (ExitCode -> Bool) -> IO ExitCode -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
p