{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables, RankNTypes, DeriveDataTypeable #-}

-- | Run external pagers (@$PAGER@, @less@, @more@).
module System.Process.Pager
  ( pageWriter
  , pageText
  , PagerException (..)
  ) where

import Stack.Prelude
import System.Directory (findExecutable)
import System.Environment (lookupEnv)
import System.Process ( createProcess, cmdspec, shell, proc, waitForProcess
                      , CmdSpec (ShellCommand, RawCommand)
                      , StdStream (CreatePipe)
                      , CreateProcess (std_in, close_fds, delegate_ctlc)
                      )
import Control.Monad.Trans.Maybe (MaybeT (runMaybeT, MaybeT))
import qualified Data.Text.IO as T

-- | Run pager, providing a function that writes to the pager's input.
pageWriter :: (Handle -> IO ()) -> IO ()
pageWriter :: (Handle -> IO ()) -> IO ()
pageWriter Handle -> IO ()
writer =
  do Maybe CreateProcess
mpager <- MaybeT IO CreateProcess -> IO (Maybe CreateProcess)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO CreateProcess -> IO (Maybe CreateProcess))
-> MaybeT IO CreateProcess -> IO (Maybe CreateProcess)
forall a b. (a -> b) -> a -> b
$ MaybeT IO CreateProcess
cmdspecFromEnvVar
                       MaybeT IO CreateProcess
-> MaybeT IO CreateProcess -> MaybeT IO CreateProcess
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> MaybeT IO CreateProcess
cmdspecFromExeName String
"less"
                       MaybeT IO CreateProcess
-> MaybeT IO CreateProcess -> MaybeT IO CreateProcess
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> MaybeT IO CreateProcess
cmdspecFromExeName String
"more"
     case Maybe CreateProcess
mpager of
       Just CreateProcess
pager ->
         do (Just Handle
h,Maybe Handle
_,Maybe Handle
_,ProcessHandle
procHandle) <- CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess CreateProcess
pager
                                         { std_in :: StdStream
std_in = StdStream
CreatePipe
                                         , close_fds :: Bool
close_fds = Bool
True
                                         , delegate_ctlc :: Bool
delegate_ctlc = Bool
True
                                         }
            (Either IOException ()
_ :: Either IOException ()) <- IO () -> IO (Either IOException ())
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try (do Handle -> IO ()
writer Handle
h
                                                    Handle -> IO ()
forall (m :: * -> *). MonadIO m => Handle -> m ()
hClose Handle
h)
            ExitCode
exit <- ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
procHandle
            case ExitCode
exit of
              ExitCode
ExitSuccess -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
              ExitFailure Int
n -> PagerException -> IO ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (CmdSpec -> Int -> PagerException
PagerExitFailure (CreateProcess -> CmdSpec
cmdspec CreateProcess
pager) Int
n)
            () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
       Maybe CreateProcess
Nothing -> Handle -> IO ()
writer Handle
stdout
  where
    cmdspecFromEnvVar :: MaybeT IO CreateProcess
cmdspecFromEnvVar = String -> CreateProcess
shell (String -> CreateProcess)
-> MaybeT IO String -> MaybeT IO CreateProcess
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe String) -> MaybeT IO String
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (String -> IO (Maybe String)
lookupEnv String
"PAGER")
    cmdspecFromExeName :: String -> MaybeT IO CreateProcess
cmdspecFromExeName =
      (String -> CreateProcess)
-> MaybeT IO String -> MaybeT IO CreateProcess
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\String
path -> String -> [String] -> CreateProcess
proc String
path []) (MaybeT IO String -> MaybeT IO CreateProcess)
-> (String -> MaybeT IO String)
-> String
-> MaybeT IO CreateProcess
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Maybe String) -> MaybeT IO String
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IO (Maybe String) -> MaybeT IO String)
-> (String -> IO (Maybe String)) -> String -> MaybeT IO String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO (Maybe String)
findExecutable

-- | Run pager to display a 'Text'
pageText :: Text -> IO ()
pageText :: Text -> IO ()
pageText = (Handle -> IO ()) -> IO ()
pageWriter ((Handle -> IO ()) -> IO ())
-> (Text -> Handle -> IO ()) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Handle -> Text -> IO ()) -> Text -> Handle -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Handle -> Text -> IO ()
T.hPutStr

-- | Exception running pager.
data PagerException = PagerExitFailure CmdSpec Int
  deriving Typeable
instance Show PagerException where
  show :: PagerException -> String
show (PagerExitFailure CmdSpec
cmd Int
n) =
    let
      getStr :: CmdSpec -> String
getStr (ShellCommand String
c) = String
c
      getStr (RawCommand String
exePath [String]
_) = String
exePath
    in
      String
"Pager (`" String -> ShowS
forall a. [a] -> [a] -> [a]
++ CmdSpec -> String
getStr CmdSpec
cmd String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"') exited with non-zero status: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n

instance Exception PagerException