summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Run/Commands.hs
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2018-11-25 15:10:29 +0000
committerjao <jao@gnu.org>2018-11-25 15:10:29 +0000
commit77df1ac30fa7af5948f7ff64f5fee9aed64552b3 (patch)
tree647a4eb67ff1c293a5c530538ee88fc0093b577a /src/Xmobar/Run/Commands.hs
parente0d6da82de8d0d1cef98896164c6016b84e47068 (diff)
downloadxmobar-77df1ac30fa7af5948f7ff64f5fee9aed64552b3.tar.gz
xmobar-77df1ac30fa7af5948f7ff64f5fee9aed64552b3.tar.bz2
Back to app/src, since it seems they're the default convention for stack
Diffstat (limited to 'src/Xmobar/Run/Commands.hs')
-rw-r--r--src/Xmobar/Run/Commands.hs72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Xmobar/Run/Commands.hs b/src/Xmobar/Run/Commands.hs
new file mode 100644
index 0000000..198edee
--- /dev/null
+++ b/src/Xmobar/Run/Commands.hs
@@ -0,0 +1,72 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Xmobar.Commands
+-- Copyright : (c) Andrea Rossato
+-- License : BSD-style (see LICENSE)
+--
+-- Maintainer : Jose A. Ortega Ruiz <jao@gnu.org>
+-- Stability : unstable
+-- Portability : unportable
+--
+-- The 'Exec' class and the 'Command' data type.
+--
+-- The 'Exec' class rappresents the executable types, whose constructors may
+-- appear in the 'Config.commands' field of the 'Config.Config' data type.
+--
+-- The 'Command' data type is for OS commands to be run by xmobar
+--
+-----------------------------------------------------------------------------
+
+module Xmobar.Run.Commands (Command (..), Exec (..)) where
+
+import Prelude
+import Control.Exception (handle, SomeException(..))
+import Data.Char
+import System.Process
+import System.Exit
+import System.IO (hClose)
+
+import Xmobar.System.Signal
+import Xmobar.Utils (hGetLineSafe, tenthSeconds)
+
+class Show e => Exec e where
+ alias :: e -> String
+ alias e = takeWhile (not . isSpace) $ show e
+ rate :: e -> Int
+ rate _ = 10
+ run :: e -> IO String
+ run _ = return ""
+ start :: e -> (String -> IO ()) -> IO ()
+ start e cb = go
+ 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