diff options
Diffstat (limited to 'src/Xmobar/Run')
-rw-r--r-- | src/Xmobar/Run/Command.hs | 55 | ||||
-rw-r--r-- | src/Xmobar/Run/Exec.hs (renamed from src/Xmobar/Run/Commands.hs) | 38 | ||||
-rw-r--r-- | src/Xmobar/Run/Runnable.hs | 2 | ||||
-rw-r--r-- | src/Xmobar/Run/Runnable.hs-boot | 2 | ||||
-rw-r--r-- | src/Xmobar/Run/Template.hs | 3 | ||||
-rw-r--r-- | src/Xmobar/Run/Types.hs | 4 |
6 files changed, 63 insertions, 41 deletions
diff --git a/src/Xmobar/Run/Command.hs b/src/Xmobar/Run/Command.hs new file mode 100644 index 0000000..0953132 --- /dev/null +++ b/src/Xmobar/Run/Command.hs @@ -0,0 +1,55 @@ +------------------------------------------------------------------------------ +-- | +-- Module: Xmobar.Plugins.Command +-- Copyright: (c) 2018 Jose Antonio Ortega Ruiz +-- License: BSD3-style (see LICENSE) +-- +-- Maintainer: jao@gnu.org +-- Stability: unstable +-- Portability: portable +-- Created: Sun Dec 02, 2018 05:29 +-- +-- +-- The basic Command plugin +-- +------------------------------------------------------------------------------ + + +module Xmobar.Run.Command where + +import Control.Exception (handle, SomeException(..)) +import System.Process +import System.Exit +import System.IO (hClose) +import Xmobar.System.Utils (hGetLineSafe) + +import Xmobar.Run.Exec + +data Command = Com Program Args Alias Rate + | ComX Program Args String Alias Rate + deriving (Show,Read,Eq) + +type Args = [String] +type Program = String +type Alias = String +type Rate = Int + +instance Exec Command where + alias (ComX p _ _ a _) = + if p /= "" then (if a == "" then p else a) else "" + alias (Com p a al r) = alias (ComX p a "" al r) + start (Com p as al r) cb = + start (ComX p as ("Could not execute command " ++ p) al r) cb + start (ComX prog args msg _ r) cb = if r > 0 then go else exec + where go = exec >> tenthSeconds r >> go + exec = do + (i,o,e,p) <- runInteractiveProcess prog args Nothing Nothing + exit <- waitForProcess p + let closeHandles = hClose o >> hClose i >> hClose e + getL = handle (\(SomeException _) -> return "") + (hGetLineSafe o) + case exit of + ExitSuccess -> do str <- getL + closeHandles + cb str + _ -> closeHandles >> cb msg diff --git a/src/Xmobar/Run/Commands.hs b/src/Xmobar/Run/Exec.hs index 2aac344..c8fcb9e 100644 --- a/src/Xmobar/Run/Commands.hs +++ b/src/Xmobar/Run/Exec.hs @@ -1,6 +1,6 @@ ----------------------------------------------------------------------------- -- | --- Module : Xmobar.Commands +-- Module : Xmobar.Exec -- Copyright : (c) Andrea Rossato -- License : BSD-style (see LICENSE) -- @@ -17,18 +17,13 @@ -- ----------------------------------------------------------------------------- -module Xmobar.Run.Commands (Command (..), Exec (..), tenthSeconds) where +module Xmobar.Run.Exec (Exec (..), tenthSeconds) where import Prelude -import Control.Exception (handle, SomeException(..)) import Data.Char -import System.Process -import System.Exit -import System.IO (hClose) import Control.Concurrent import Xmobar.System.Signal -import Xmobar.System.Utils (hGetLineSafe) -- | Work around to the Int max bound: since threadDelay takes an Int, it -- is not possible to set a thread delay grater than about 45 minutes. @@ -51,32 +46,3 @@ class Show e => Exec e where where go = run e >>= cb >> tenthSeconds (rate e) >> go trigger :: e -> (Maybe SignalType -> IO ()) -> IO () trigger _ sh = sh Nothing - -data Command = Com Program Args Alias Rate - | ComX Program Args String Alias Rate - deriving (Show,Read,Eq) - -type Args = [String] -type Program = String -type Alias = String -type Rate = Int - -instance Exec Command where - alias (ComX p _ _ a _) = - if p /= "" then (if a == "" then p else a) else "" - alias (Com p a al r) = alias (ComX p a "" al r) - start (Com p as al r) cb = - start (ComX p as ("Could not execute command " ++ p) al r) cb - start (ComX prog args msg _ r) cb = if r > 0 then go else exec - where go = exec >> tenthSeconds r >> go - exec = do - (i,o,e,p) <- runInteractiveProcess prog args Nothing Nothing - exit <- waitForProcess p - let closeHandles = hClose o >> hClose i >> hClose e - getL = handle (\(SomeException _) -> return "") - (hGetLineSafe o) - case exit of - ExitSuccess -> do str <- getL - closeHandles - cb str - _ -> closeHandles >> cb msg diff --git a/src/Xmobar/Run/Runnable.hs b/src/Xmobar/Run/Runnable.hs index 962166e..068a05b 100644 --- a/src/Xmobar/Run/Runnable.hs +++ b/src/Xmobar/Run/Runnable.hs @@ -24,7 +24,7 @@ module Xmobar.Run.Runnable where import Control.Monad import Text.Read import Xmobar.Run.Types (runnableTypes) -import Xmobar.Run.Commands +import Xmobar.Run.Exec data Runnable = forall r . (Exec r, Read r, Show r) => Run r diff --git a/src/Xmobar/Run/Runnable.hs-boot b/src/Xmobar/Run/Runnable.hs-boot index f272d81..1cd1688 100644 --- a/src/Xmobar/Run/Runnable.hs-boot +++ b/src/Xmobar/Run/Runnable.hs-boot @@ -1,6 +1,6 @@ {-# LANGUAGE ExistentialQuantification #-} module Xmobar.Run.Runnable where -import Xmobar.Run.Commands +import Xmobar.Run.Exec data Runnable = forall r . (Exec r,Read r,Show r) => Run r diff --git a/src/Xmobar/Run/Template.hs b/src/Xmobar/Run/Template.hs index 749edcd..50c3a08 100644 --- a/src/Xmobar/Run/Template.hs +++ b/src/Xmobar/Run/Template.hs @@ -20,7 +20,8 @@ module Xmobar.Run.Template(parseTemplate, splitTemplate) where import qualified Data.Map as Map import Text.ParserCombinators.Parsec -import Xmobar.Run.Commands +import Xmobar.Run.Exec +import Xmobar.Run.Command import Xmobar.Run.Runnable defaultAlign :: String diff --git a/src/Xmobar/Run/Types.hs b/src/Xmobar/Run/Types.hs index 4fb526a..f4a7252 100644 --- a/src/Xmobar/Run/Types.hs +++ b/src/Xmobar/Run/Types.hs @@ -18,8 +18,6 @@ module Xmobar.Run.Types(runnableTypes) where -import Xmobar.Run.Commands - import {-# SOURCE #-} Xmobar.Run.Runnable() import Xmobar.Plugins.Monitors import Xmobar.Plugins.Date @@ -42,6 +40,8 @@ import Xmobar.Plugins.MBox import Xmobar.Plugins.DateZone #endif +import Xmobar.Run.Command + -- | An alias for tuple types that is more convenient for long lists. type a :*: b = (a, b) infixr :*: |