{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
module Shellmet
( ($|)
, ($^)
, ($?)
) 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 cmd :: String
cmd args :: [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
forall a. [a] -> [a] -> [a]
++ String
cmdStr
String -> IO ()
callCommand String
cmdStr
{-# INLINE fromString #-}
infix 5 $|
($|) :: FilePath -> [Text] -> IO Text
cmd :: String
cmd $| :: String -> [Text] -> IO Text
$| args :: [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) ""
{-# INLINE ($|) #-}
infix 5 $^
($^) :: FilePath -> [Text] -> IO ()
cmd :: String
cmd $^ :: String -> [Text] -> IO ()
$^ args :: [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
action :: IO a
action $? :: IO a -> IO a -> IO a
$? handler :: 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 ($?) #-}