xmonad-config/lib/XMonad/Internal/Shell.hs

107 lines
2.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
2020-04-01 22:06:00 -04:00
--------------------------------------------------------------------------------
-- | Functions for formatting and spawning shell commands
2020-04-01 20:17:47 -04:00
module XMonad.Internal.Shell
( fmtCmd
2020-04-01 20:17:47 -04:00
, spawnCmd
2022-12-29 00:06:55 -05:00
, spawn
, spawnAt
, spawnStdin
2021-06-17 01:17:59 -04:00
, doubleQuote
, singleQuote
2021-06-19 00:17:47 -04:00
, skip
, runProcess
, proc
, shell
2020-04-01 20:17:47 -04:00
, (#!&&)
, (#!||)
, (#!|)
2020-04-01 20:17:47 -04:00
, (#!>>)
) where
2020-03-18 12:17:39 -04:00
import Control.Monad.IO.Class
2022-12-29 00:06:55 -05:00
import RIO
import qualified RIO.ByteString.Lazy as B
2022-12-29 00:06:55 -05:00
import qualified RIO.Text as T
import qualified System.Process.Typed as P
2022-12-29 00:06:55 -05:00
import qualified XMonad.Core as X
2020-03-28 18:38:38 -04:00
2020-04-01 22:06:00 -04:00
--------------------------------------------------------------------------------
-- | Opening subshell
-- https://github.com/xmonad/xmonad/issues/113
2020-03-18 12:17:39 -04:00
2022-12-29 00:06:55 -05:00
withDefaultSignalHandlers :: IO a -> IO a
withDefaultSignalHandlers =
bracket_ X.uninstallSignalHandlers X.installSignalHandlers
addGroupSession :: P.ProcessConfig x y z -> P.ProcessConfig x y z
addGroupSession = P.setCreateGroup True . P.setNewSession True
2022-12-29 00:06:55 -05:00
-- readProcess :: P.ProcessConfig a b c -> IO (ExitCode, B.ByteString, B.ByteString)
-- readProcess = withDefaultSignalHandlers . P.readProcess
2022-12-29 00:06:55 -05:00
runProcess :: P.ProcessConfig a b c -> IO ExitCode
runProcess = withDefaultSignalHandlers . P.runProcess
2022-12-29 00:06:55 -05:00
shell :: T.Text -> P.ProcessConfig () () ()
shell = addGroupSession . P.shell . T.unpack
2022-12-29 00:06:55 -05:00
proc :: FilePath -> [T.Text] -> P.ProcessConfig () () ()
proc cmd args = addGroupSession $ P.proc cmd (T.unpack <$> args)
2022-12-29 00:06:55 -05:00
spawn :: MonadIO m => T.Text -> m ()
spawn = liftIO . void . P.startProcess . shell
2022-12-29 00:06:55 -05:00
spawnAt :: MonadIO m => FilePath -> T.Text -> m ()
spawnAt fp = liftIO . void . P.startProcess . P.setWorkingDir fp . shell
2022-12-29 00:06:55 -05:00
spawnStdin :: MonadIO m => B.ByteString -> T.Text -> m ()
spawnStdin i =
liftIO . void . P.startProcess . P.setStdin (P.byteStringInput i) . shell
2022-12-29 00:06:55 -05:00
spawnCmd :: MonadIO m => FilePath -> [T.Text] -> m ()
spawnCmd cmd = spawn . fmtCmd cmd
2020-03-18 12:17:39 -04:00
2020-04-01 22:06:00 -04:00
--------------------------------------------------------------------------------
-- | Formatting commands
fmtCmd :: FilePath -> [T.Text] -> T.Text
fmtCmd cmd args = T.unwords $ T.pack cmd : args
op :: T.Text -> T.Text -> T.Text -> T.Text
op a x b = T.unwords [a, x, b]
2020-04-01 22:06:00 -04:00
(#!&&) :: T.Text -> T.Text -> T.Text
cmdA #!&& cmdB = op cmdA "&&" cmdB
2020-03-18 12:17:39 -04:00
infixr 0 #!&&
(#!|) :: T.Text -> T.Text -> T.Text
cmdA #!| cmdB = op cmdA "|" cmdB
infixr 0 #!|
(#!||) :: T.Text -> T.Text -> T.Text
cmdA #!|| cmdB = op cmdA "||" cmdB
2020-03-18 12:17:39 -04:00
infixr 0 #!||
(#!>>) :: T.Text -> T.Text -> T.Text
cmdA #!>> cmdB = op cmdA ";" cmdB
2020-03-18 12:17:39 -04:00
infixr 0 #!>>
2021-06-17 01:17:59 -04:00
doubleQuote :: T.Text -> T.Text
doubleQuote s = T.concat ["\"", s, "\""]
2021-06-17 01:17:59 -04:00
singleQuote :: T.Text -> T.Text
singleQuote s = T.concat ["'", s, "'"]
skip :: Monad m => m ()
skip = return ()