[Xmonad] New state logging framework

Donald Bruce Stewart dons at cse.unsw.edu.au
Sun Jun 10 02:38:57 EDT 2007


Ok. I'm done with the new logging framework.
If you live in darcs, you'll see Config.hs has a new field:

    --
    -- Perform an arbitrary action on each state change.
    -- Examples include:
    --      * do nothing
    --      * log the state to stdout
    --
    logHook :: X ()
    logHook = return ()

This logHook code will be run on every change to the internal state, and
it can do just about anything (!).

One possible use is to print the internal workspace state to stdout, and
to then pipe that into dzen , to get workspace info in a status bar.
Here's the code I use for my own logHook:

    logHook = withWindowSet $ io . putStrLn . ppr
      where
        ppr s =  concatMap fmt $ sortBy tags
                    (map S.workspace (S.current s : S.visible s) ++ S.hidden s)

           where tags a b = S.tag a `compare` S.tag b
                 this     = S.tag (S.workspace (S.current s))
                 pprTag   = show . (+(1::Int)) . fromIntegral . S.tag
                 fmt w | S.tag w == this      = "[" ++ pprTag w ++ "]"
                       | S.stack w /= S.Empty = " " ++ pprTag w ++ " "
                       | otherwise            = ""

Which takes the workspace description, and prints it as a string in the form:

    1 [2] 3  5

Showing the non-empty workspaces, and the current workspace. 
I then run xmonad as follows, piping this stuff into dzen:

    # create a pipe for xmonad to talk to
    PIPE=$HOME/.xmonad-status
    rm -f $PIPE
    /sbin/mkfifo -m 600 $PIPE
    [ -p $PIPE ] || exit

    # launch the external 60 second clock, and launch the workspace status bar
    # xmonad-clock.c is a little program in XMonadContrib/scripts
    xmonad-clock | dzen2 -e '' -x 300 -w 768 -ta r -fg $FG -bg $BG -fn $FONT &

    # and a workspace status bar, reading's xmonad's stdout
    dzen2 -e '' -w 300 -ta l -fg $FG -bg $BG -fn $FONT < $PIPE &

    xmonad > $PIPE & 
    wait $!

I imagine people can do all sorts of wacky things with this logHook.
My example logHook is also in XMonadContrib/DynamicLog.hs.

-- Don


More information about the Xmonad mailing list