Konrad Eisele
Konrad Eisele

Reputation: 3194

Haskell XMonad configuration using readProcess

I'm trying to configure XMonad without actually understand the Haskell syntax.

I run a gnome session with xmonad on top. I want to press mod-p to toggle the gnome-panel:

I thought that this would do it:

startgpanel :: X ()
startgpanel = do
  gp <- readProcess "pidof" ["gnome-panel"] ""
     if (length gp)
     then spawn "killall gnome-panel"
     else spawn "gnome-panel"

  ...
 ((myModMask, xK_g), startgpanel)
  ...

But I get an error:

xmonad.hs:169:12:
    Couldn't match expected type âX (t0 a0)â
                with actual type âIO Stringâ
    In a stmt of a 'do' block:
      gp <- readProcess "pidof" ["gnome-panel"] ""
    In the expression:
      do { gp <- readProcess "pidof" ["gnome-panel"] "";
           if (length gp) then
               spawn "killall gnome-panel"
           else
               spawn "gnome-panel" }

I dont really understand the Monad concept, I just want to do some IO, but it seems very complex...

Upvotes: 1

Views: 315

Answers (3)

Daniel Wagner
Daniel Wagner

Reputation: 152707

Because of all the nastiness xmonad does with processes, I would propose you move the whole darn bunch of logic into the shell; bind a key to this operation:

spawn "pidof gnome-panel && killall gnome-panel || gnome-panel"

Look ma, no do!

Upvotes: -1

ErikR
ErikR

Reputation: 52039

As others have mentioned, you need to use liftIO.

Also consult the discussion of this SO question:

How do I use the output of readProcess in an xmonad keybinding?

Instead of readProcess you might need to use readProcessWithInput.

Upvotes: 1

felix-eku
felix-eku

Reputation: 2213

You need to wrap your IO action in liftIO to run it in the X monad. Also you cannot just use the result of length as an Bool for the if statement, as it returns an Int:

startgpanel :: X ()
startgpanel = do
  gp <- liftIO $ readProcess "pidof" ["gnome-panel"] ""
  if (length gp > 0)
    then spawn "killall gnome-panel"
    else spawn "gnome-panel"

Upvotes: 1

Related Questions