configurationxmonadxmobar

How to change xmobar configuration on the fly


I want to switch between xmobar configurations on the fly by using key combinations. I have naively tagged the following onto my other key mods:

, ((controlMask, xK_l), xmproc <- spawnPipe "/usr/bin/xmobar /home/tony/.xmobarLrc")
, ((controlMask, xK_w), xmproc <- spawnPipe "/usr/bin/xmobar /home/tony/.xmobarWrc")

and the compiler barfs at <-. You can probably read my intention in the code. I am no Haskell expert and I'm slowly building up the environment I want by using a lego approach, but that has failed me here.

Where am I going wrong?

TIA


Solution

  • Well, it's quite complicated to do what you want. You can use the extensible state module from the xmonad-contrib library.

    For that, you have to add a LANGUAGE pragma at the top of your xmonad configuration file:

    {-# LANGUAGE DeriveDataTypeable #-}

    You need it to derive the Typeable instance for the data type that stores the xmobar handle.

    newtype XMobarHandle = XMobarHandle { xmhandle :: Maybe Handle } deriving Typeable
    
    instance ExtensionClass XMobarHandle where
            initialValue = XMobarHandle Nothing
    

    Now you can define the key-binding, which retrieves the current xmobar handle from the extensible state, closes it if it is not Nothing, spawns a new one and puts it into the state.

    ((controlMask, xK_l), do
        mh <- xmhandle `fmap` XS.get
        maybe (return ()) (io . hClose) mh
        xmproc <- spawnPipe "/usr/bin/xmobar /home/tony/.xmobarLrc"
        XS.put $ XMobarHandle (Just xmproc)
    )
    

    You can create a function for the do block in the binding if you like. The binding for the other key is left as an exercise!

    To make it compile, you still need the import statements for the modules used in this code. (I may have forgotten one, though!)

    import           XMonad.Util.Run
    import           System.IO
    import qualified XMonad.Util.ExtensibleState         as XS
    

    You have to edit your logHook as well. There you have to extract the handle from extensible state just like in the keybind and give it as a parameter to the function xmobarlog.