{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module XMonad.Layout.OnHost (
OnHost
,onHost
,onHosts
,modHost
,modHosts
) where
import XMonad
import qualified XMonad.StackSet as W
import XMonad.Layout.LayoutModifier
import Data.Maybe (fromMaybe)
import System.Posix.Env (getEnv)
onHost :: (LayoutClass l1 a, LayoutClass l2 a)
=> String
-> l1 a
-> l2 a
-> OnHost l1 l2 a
onHost :: String -> l1 a -> l2 a -> OnHost l1 l2 a
onHost String
host = [String] -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) a (l2 :: * -> *).
(LayoutClass l1 a, LayoutClass l2 a) =>
[String] -> l1 a -> l2 a -> OnHost l1 l2 a
onHosts [String
host]
onHosts :: (LayoutClass l1 a, LayoutClass l2 a)
=> [String]
-> l1 a
-> l2 a
-> OnHost l1 l2 a
onHosts :: [String] -> l1 a -> l2 a -> OnHost l1 l2 a
onHosts [String]
hosts = [String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
False
modHost :: (LayoutClass l a)
=> String
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
modHost :: String
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
modHost String
host = [String]
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
forall (l :: * -> *) a (lm :: * -> *).
LayoutClass l a =>
[String]
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
modHosts [String
host]
modHosts :: (LayoutClass l a)
=> [String]
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
modHosts :: [String]
-> (l a -> ModifiedLayout lm l a)
-> l a
-> OnHost (ModifiedLayout lm l) l a
modHosts [String]
hosts l a -> ModifiedLayout lm l a
f l a
l = [String]
-> Bool
-> ModifiedLayout lm l a
-> l a
-> OnHost (ModifiedLayout lm l) l a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
False (l a -> ModifiedLayout lm l a
f l a
l) l a
l
data OnHost l1 l2 a = OnHost [String]
Bool
(l1 a)
(l2 a)
deriving (ReadPrec [OnHost l1 l2 a]
ReadPrec (OnHost l1 l2 a)
Int -> ReadS (OnHost l1 l2 a)
ReadS [OnHost l1 l2 a]
(Int -> ReadS (OnHost l1 l2 a))
-> ReadS [OnHost l1 l2 a]
-> ReadPrec (OnHost l1 l2 a)
-> ReadPrec [OnHost l1 l2 a]
-> Read (OnHost l1 l2 a)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadPrec [OnHost l1 l2 a]
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadPrec (OnHost l1 l2 a)
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
Int -> ReadS (OnHost l1 l2 a)
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadS [OnHost l1 l2 a]
readListPrec :: ReadPrec [OnHost l1 l2 a]
$creadListPrec :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadPrec [OnHost l1 l2 a]
readPrec :: ReadPrec (OnHost l1 l2 a)
$creadPrec :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadPrec (OnHost l1 l2 a)
readList :: ReadS [OnHost l1 l2 a]
$creadList :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
ReadS [OnHost l1 l2 a]
readsPrec :: Int -> ReadS (OnHost l1 l2 a)
$creadsPrec :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Read (l1 a), Read (l2 a)) =>
Int -> ReadS (OnHost l1 l2 a)
Read, Int -> OnHost l1 l2 a -> ShowS
[OnHost l1 l2 a] -> ShowS
OnHost l1 l2 a -> String
(Int -> OnHost l1 l2 a -> ShowS)
-> (OnHost l1 l2 a -> String)
-> ([OnHost l1 l2 a] -> ShowS)
-> Show (OnHost l1 l2 a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
Int -> OnHost l1 l2 a -> ShowS
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
[OnHost l1 l2 a] -> ShowS
forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
OnHost l1 l2 a -> String
showList :: [OnHost l1 l2 a] -> ShowS
$cshowList :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
[OnHost l1 l2 a] -> ShowS
show :: OnHost l1 l2 a -> String
$cshow :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
OnHost l1 l2 a -> String
showsPrec :: Int -> OnHost l1 l2 a -> ShowS
$cshowsPrec :: forall (l1 :: * -> *) (l2 :: * -> *) a.
(Show (l1 a), Show (l2 a)) =>
Int -> OnHost l1 l2 a -> ShowS
Show)
instance (LayoutClass l1 a, LayoutClass l2 a, Show a) => LayoutClass (OnHost l1 l2) a where
runLayout :: Workspace String (OnHost l1 l2 a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (OnHost l1 l2 a))
runLayout (W.Workspace String
i p :: OnHost l1 l2 a
p@(OnHost [String]
hosts Bool
_ l1 a
lt l2 a
lf) Maybe (Stack a)
ms) Rectangle
r = do
Maybe String
h <- IO (Maybe String) -> X (Maybe String)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO (Maybe String) -> X (Maybe String))
-> IO (Maybe String) -> X (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> IO (Maybe String)
getEnv String
"HOST"
if Bool -> (String -> Bool) -> Maybe String -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (String -> [String] -> Bool
`elemFQDN` [String]
hosts) Maybe String
h
then do ([(a, Rectangle)]
wrs, Maybe (l1 a)
mlt') <- Workspace String (l1 a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (l1 a))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout (String -> l1 a -> Maybe (Stack a) -> Workspace String (l1 a) a
forall i l a. i -> l -> Maybe (Stack a) -> Workspace i l a
W.Workspace String
i l1 a
lt Maybe (Stack a)
ms) Rectangle
r
([(a, Rectangle)], Maybe (OnHost l1 l2 a))
-> X ([(a, Rectangle)], Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return ([(a, Rectangle)]
wrs, OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a. a -> Maybe a
Just (OnHost l1 l2 a -> Maybe (OnHost l1 l2 a))
-> OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a b. (a -> b) -> a -> b
$ OnHost l1 l2 a -> Maybe (l1 a) -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
OnHost l1 l2 a -> Maybe (l1 a) -> OnHost l1 l2 a
mkNewOnHostT OnHost l1 l2 a
p Maybe (l1 a)
mlt')
else do ([(a, Rectangle)]
wrs, Maybe (l2 a)
mlt') <- Workspace String (l2 a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (l2 a))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace String (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout (String -> l2 a -> Maybe (Stack a) -> Workspace String (l2 a) a
forall i l a. i -> l -> Maybe (Stack a) -> Workspace i l a
W.Workspace String
i l2 a
lf Maybe (Stack a)
ms) Rectangle
r
([(a, Rectangle)], Maybe (OnHost l1 l2 a))
-> X ([(a, Rectangle)], Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return ([(a, Rectangle)]
wrs, OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a. a -> Maybe a
Just (OnHost l1 l2 a -> Maybe (OnHost l1 l2 a))
-> OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a b. (a -> b) -> a -> b
$ OnHost l1 l2 a -> Maybe (l2 a) -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
OnHost l1 l2 a -> Maybe (l2 a) -> OnHost l1 l2 a
mkNewOnHostF OnHost l1 l2 a
p Maybe (l2 a)
mlt')
handleMessage :: OnHost l1 l2 a -> SomeMessage -> X (Maybe (OnHost l1 l2 a))
handleMessage (OnHost [String]
hosts Bool
bool l1 a
lt l2 a
lf) SomeMessage
m
| Bool
bool = l1 a -> SomeMessage -> X (Maybe (l1 a))
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l1 a
lt SomeMessage
m X (Maybe (l1 a))
-> (Maybe (l1 a) -> X (Maybe (OnHost l1 l2 a)))
-> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= X (Maybe (OnHost l1 l2 a))
-> (l1 a -> X (Maybe (OnHost l1 l2 a)))
-> Maybe (l1 a)
-> X (Maybe (OnHost l1 l2 a))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (OnHost l1 l2 a)
forall a. Maybe a
Nothing) (\l1 a
nt -> Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a)))
-> (OnHost l1 l2 a -> Maybe (OnHost l1 l2 a))
-> OnHost l1 l2 a
-> X (Maybe (OnHost l1 l2 a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a. a -> Maybe a
Just (OnHost l1 l2 a -> X (Maybe (OnHost l1 l2 a)))
-> OnHost l1 l2 a -> X (Maybe (OnHost l1 l2 a))
forall a b. (a -> b) -> a -> b
$ [String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
bool l1 a
nt l2 a
lf)
| Bool
otherwise = l2 a -> SomeMessage -> X (Maybe (l2 a))
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage l2 a
lf SomeMessage
m X (Maybe (l2 a))
-> (Maybe (l2 a) -> X (Maybe (OnHost l1 l2 a)))
-> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= X (Maybe (OnHost l1 l2 a))
-> (l2 a -> X (Maybe (OnHost l1 l2 a)))
-> Maybe (l2 a)
-> X (Maybe (OnHost l1 l2 a))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (OnHost l1 l2 a)
forall a. Maybe a
Nothing) (Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (OnHost l1 l2 a) -> X (Maybe (OnHost l1 l2 a)))
-> (l2 a -> Maybe (OnHost l1 l2 a))
-> l2 a
-> X (Maybe (OnHost l1 l2 a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnHost l1 l2 a -> Maybe (OnHost l1 l2 a)
forall a. a -> Maybe a
Just (OnHost l1 l2 a -> Maybe (OnHost l1 l2 a))
-> (l2 a -> OnHost l1 l2 a) -> l2 a -> Maybe (OnHost l1 l2 a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
bool l1 a
lt)
description :: OnHost l1 l2 a -> String
description (OnHost [String]
_ Bool
True l1 a
l1 l2 a
_) = l1 a -> String
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description l1 a
l1
description (OnHost [String]
_ Bool
_ l1 a
_ l2 a
l2) = l2 a -> String
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description l2 a
l2
mkNewOnHostT :: OnHost l1 l2 a -> Maybe (l1 a) -> OnHost l1 l2 a
mkNewOnHostT :: OnHost l1 l2 a -> Maybe (l1 a) -> OnHost l1 l2 a
mkNewOnHostT (OnHost [String]
hosts Bool
_ l1 a
lt l2 a
lf) Maybe (l1 a)
mlt' =
(\l1 a
lt' -> [String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
True l1 a
lt' l2 a
lf) (l1 a -> OnHost l1 l2 a) -> l1 a -> OnHost l1 l2 a
forall a b. (a -> b) -> a -> b
$ l1 a -> Maybe (l1 a) -> l1 a
forall a. a -> Maybe a -> a
fromMaybe l1 a
lt Maybe (l1 a)
mlt'
mkNewOnHostF :: OnHost l1 l2 a -> Maybe (l2 a) -> OnHost l1 l2 a
mkNewOnHostF :: OnHost l1 l2 a -> Maybe (l2 a) -> OnHost l1 l2 a
mkNewOnHostF (OnHost [String]
hosts Bool
_ l1 a
lt l2 a
lf) Maybe (l2 a)
mlf' =
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
forall (l1 :: * -> *) (l2 :: * -> *) a.
[String] -> Bool -> l1 a -> l2 a -> OnHost l1 l2 a
OnHost [String]
hosts Bool
False l1 a
lt (l2 a -> OnHost l1 l2 a) -> l2 a -> OnHost l1 l2 a
forall a b. (a -> b) -> a -> b
$ l2 a -> Maybe (l2 a) -> l2 a
forall a. a -> Maybe a -> a
fromMaybe l2 a
lf Maybe (l2 a)
mlf'
elemFQDN :: String -> [String] -> Bool
elemFQDN :: String -> [String] -> Bool
elemFQDN String
_ [] = Bool
False
elemFQDN String
h0 (String
h:[String]
hs)
| String
h0 String -> String -> Bool
`eqFQDN` String
h = Bool
True
| Bool
otherwise = String -> [String] -> Bool
elemFQDN String
h0 [String]
hs
eqFQDN :: String -> String -> Bool
eqFQDN :: String -> String -> Bool
eqFQDN String
a String
b
| Char
'.' Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
a Bool -> Bool -> Bool
&& Char
'.' Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
b = String
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
b
| Char
'.' Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
a = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') String
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
b
| Char
'.' Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
b = String
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') String
b
| Bool
otherwise = String
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
b