XMobar clickable workspaces

XMobar clickable workspaces

After years of using xfce for daily work I’ve headed back to tiling territory.

Stumbled on a git bug report discussing clickable workspaces with xmobar, which lead me to the xmobar plugin: UnsafeStdinReader that would enable escape sequences in workspace to be accepted by xmobar.

Below is a working example:

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.Run(spawnPipe)
import XMonad.Util.EZConfig(additionalKeys)
import System.IO
 
myManageHook = composeAll
    [ className =? "Gimp"      --> doFloat
    , className =? "rdesktop" --> doFloat
    ]

xmobarEscape = concatMap doubleLts
  where doubleLts '<' = "<<"
        doubleLts x   = [x]

myWorkspaces :: [String]        
myWorkspaces = clickable . (map xmobarEscape) $ ["1","2","3","4","5"]
                                                                              
  where                                                                       
         clickable l = [ "<action=xdotool key alt+" ++ show (n) ++ ">" ++ ws ++ "</action>" |
                             (i,ws) <- zip [1..5] l,                                        
                            let n = i ]
main = do
    xmproc <- spawnPipe "xmobar /home/regj/.xmobarrc"
    xmonad $ defaultConfig
        { manageHook = manageDocks <+> myManageHook -- make sure to include myManageHook definition from above
                        <+> manageHook defaultConfig
        , layoutHook = avoidStruts  $  layoutHook defaultConfig
        , workspaces = myWorkspaces
        , logHook = dynamicLogWithPP xmobarPP
                        { ppOutput = hPutStrLn xmproc
                        , ppCurrent = xmobarColor "yellow" "" . wrap "[" "]"
                        , ppHiddenNoWindows = xmobarColor "grey" ""
                        , ppTitle   = xmobarColor "green"  "" . shorten 40
                        , ppVisible = wrap "(" ")"
                        , ppUrgent  = xmobarColor "red" "yellow"
                        }
        , modMask = mod1Mask     -- Rebind Mod to the Windows key
        } `additionalKeys`
        [ ((mod1Mask, xK_f), spawn "firefox")
        , ((mod1Mask, xK_Print), spawn "/home/regj/bin/scrotter")
        , ((mod1Mask, xK_Return), spawn "urxvt")
        ]

And the .xmobarrc:

Config { font = "xft:Dina:pixelsize=13"
       , bgColor = "black"
       , fgColor = "grey"
       , position = TopW L 100
       , lowerOnStart = True
       , commands = [ Run Cpu ["-L","3","-H","50","--normal","green","--high","red"] 10
                    , Run Memory ["-t","Mem: %"] 10
                    , Run Swap [] 10
                    , Run Date "%a %b %_d %H:%M" "date" 10
                    , Run UnsafeStdinReader
                    ]
       , sepChar = "%"
       , alignSep = "}{"
       , template = "%UnsafeStdinReader% }{ %cpu% | %memory% * %swap%    %date%" 
       }