{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
module Shellmet
( ($|)
, ($^)
, ($?)
, isSuccess
) where
import Control.Exception (catch)
import Data.String (IsString (..))
import Data.Text (Text)
import System.Process (callCommand, readProcess, showCommandForUser)
import qualified Data.Text as T
instance (a ~ [Text], b ~ IO ()) => IsString (a -> b) where
fromString :: String -> [Text] -> IO ()
fromString :: String -> [Text] -> IO ()
fromString String
cmd [Text]
args = do
let cmdStr :: String
cmdStr = String -> [String] -> String
showCommandForUser String
cmd ((Text -> String) -> [Text] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Text -> String
T.unpack [Text]
args)
String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"⚙ " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
cmdStr
String -> IO ()
callCommand String
cmdStr
{-# INLINE fromString #-}
infix 5 $|
($|) :: FilePath -> [Text] -> IO Text
String
cmd $| :: String -> [Text] -> IO Text
$| [Text]
args = Text -> Text
T.strip (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> IO String -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [String] -> String -> IO String
readProcess String
cmd ((Text -> String) -> [Text] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Text -> String
T.unpack [Text]
args) String
""
{-# INLINE ($|) #-}
infix 5 $^
($^) :: FilePath -> [Text] -> IO ()
String
cmd $^ :: String -> [Text] -> IO ()
$^ [Text]
args = String -> IO ()
callCommand (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [String] -> String
showCommandForUser String
cmd ((Text -> String) -> [Text] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Text -> String
T.unpack [Text]
args)
{-# INLINE ($^) #-}
infixl 4 $?
($?) :: IO a -> IO a -> IO a
IO a
action $? :: forall a. IO a -> IO a -> IO a
$? IO a
handler = IO a
action IO a -> (IOError -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(IOError
_ :: IOError) -> IO a
handler
{-# INLINE ($?) #-}
isSuccess :: IO a -> IO Bool
isSuccess :: forall a. IO a -> IO Bool
isSuccess IO a
action = (Bool
True Bool -> IO a -> IO Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ IO a
action) IO Bool -> IO Bool -> IO Bool
forall a. IO a -> IO a -> IO a
$? Bool -> IO Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False