blob: e4402fcf560ef7b8bdf81bd395b520dfac27cd1a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
-----------------------------------------------------------------------------
-- |
-- 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 Commands
( Command (..)
, Exec (..)
, tenthSeconds
) where
import Prelude
import Control.Concurrent
import Control.Exception (handle, SomeException(..))
import Data.Char
import System.Process
import System.Exit
import System.IO (hClose)
import Signal
import XUtil
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
deriving (Show,Read,Eq)
type Args = [String]
type Program = String
type Alias = String
type Rate = Int
instance Exec Command where
alias (Com p _ a _)
| p /= "" = if a == "" then p else a
| otherwise = ""
start (Com prog args _ 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
_ -> do closeHandles
cb $ "Could not execute command " ++ prog
-- | 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.
-- With a little recursion we solve the problem.
tenthSeconds :: Int -> IO ()
tenthSeconds s | s >= x = do threadDelay (x * 100000)
tenthSeconds (s - x)
| otherwise = threadDelay (s * 100000)
where x = (maxBound :: Int) `div` 100000
|