Colortheme switching with pywall for xmonad and xmobar
Tags: xmonad, pywall, wm, xmobar
Published: 2020-12-04 18:55:03

---
title: Colortheme switching with pywall for xmonad and xmobar
published: true
series: xmonad
tags: xmonad, pywall, wm, xmobar
---

I switched recently from I3wm to xmonad. On my I3, all the colors of rofi, bars, terminal, background and browser are matching. I archived this with [pywal](https://github.com/dylanaraps/pywal)

Pywal is a tool that generates a color palette from the dominant colors in an image. It then applies the colors system-wide and on-the-fly in all of your favourite programs.

I want to have some similar in xmonad and xmobar. To avoid cross sym-linking and get all my config files on one place (my dotfile dir), I had to be a little bit creative. 

I have created a xmonad theme switcher script, which changed the colors in the xmonad and xmobar configuration.

After setting up my base configuration for both, I created template files. This file will be processed with and bash script:

```bash

#!/bin/bash

input="/home/maren/.cache/wal/colors.yml"
# yq is a command line yaml parser which allows querring yaml with a selector
yq r ~/.cache/wal/colors.yml special.background
yq r ~/.cache/wal/colors.yml special.foreground

background=$(yq r ~/.cache/wal/colors.yml special.background )
foreground=$(yq r ~/.cache/wal/colors.yml special.foreground )

color0=$(yq r ~/.cache/wal/colors.yml colors.color0 )
color1=$(yq r ~/.cache/wal/colors.yml colors.color1 )
color2=$(yq r ~/.cache/wal/colors.yml colors.color2 )
color3=$(yq r ~/.cache/wal/colors.yml colors.color3 )
color4=$(yq r ~/.cache/wal/colors.yml colors.color4 )
color5=$(yq r ~/.cache/wal/colors.yml colors.color5 )
color6=$(yq r ~/.cache/wal/colors.yml colors.color6 )
color7=$(yq r ~/.cache/wal/colors.yml colors.color7 )
color8=$(yq r ~/.cache/wal/colors.yml colors.color8 )
color9=$(yq r ~/.cache/wal/colors.yml colors.color9 )
color10=$(yq r ~/.cache/wal/colors.yml colors.color10 )
color11=$(yq r ~/.cache/wal/colors.yml colors.color11 )
color12=$(yq r ~/.cache/wal/colors.yml colors.color12 )
color13=$(yq r ~/.cache/wal/colors.yml colors.color13 )
color14=$(yq r ~/.cache/wal/colors.yml colors.color14 )
color15=$(yq r ~/.cache/wal/colors.yml colors.color15 )

sed -e "s/BGCOLOR/$background/g" \
    -e "s/FGCOLOR/$foreground/g" \
    -e "s/COLOR1/$color1/g" \
    -e "s/COLOR2/$color2/g" \
    -e "s/COLOR3/$color3/g" \
    -e "s/COLOR4/$color4/g" \
    -e "s/COLOR5/$color5/g" \
    -e "s/COLOR6/$color6/g" \
    -e "s/COLOR7/$color7/g" \
    -e "s/COLOR8/$color8/g" \
    -e "s/COLOR9/$color9/g" \
    -e "s/COLOR10/$color10/g" \
    -e "s/COLOR11/$color11/g" \
    -e "s/COLOR12/$color12/g" \
    -e "s/COLOR13/$color13/g" \
    -e "s/COLOR14/$color14/g" \
    -e "s/COLOR15/$color15/g" \
    /home/maren/dotfiles/xmobar/xmobarrc1-template > /home/maren/dotfiles/xmobar/xmobarrc1


sed -e "s/BGCOLOR/$background/g" \
    -e "s/FGCOLOR/$foreground/g" \
    -e "s/COLOR1/$color1/g" \
    -e "s/COLOR2/$color2/g" \
    -e "s/COLOR3/$color3/g" \
    -e "s/COLOR4/$color4/g" \
    -e "s/COLOR5/$color5/g" \
    -e "s/COLOR6/$color6/g" \
    -e "s/COLOR7/$color7/g" \
    -e "s/COLOR8/$color8/g" \
    -e "s/COLOR9/$color9/g" \
    -e "s/COLOR10/$color10/g" \
    -e "s/COLOR11/$color11/g" \
    -e "s/COLOR12/$color12/g" \
    -e "s/COLOR13/$color13/g" \
    -e "s/COLOR14/$color14/g" \
    -e "s/COLOR15/$color15/g" \
    /home/maren/dotfiles/xmobar/xmobarrc0-template > /home/maren/dotfiles/xmobar/xmobarrc0

sed -e "s/BGCOLOR/$background/g" \
    -e "s/FGCOLOR/$foreground/g" \
    -e "s/COLOR1/$color1/g" \
    -e "s/COLOR2/$color2/g" \
    -e "s/COLOR3/$color3/g" \
    -e "s/COLOR4/$color4/g" \
    -e "s/COLOR5/$color5/g" \
    -e "s/COLOR6/$color6/g" \
    -e "s/COLOR7/$color7/g" \
    -e "s/COLOR8/$color8/g" \
    -e "s/COLOR9/$color9/g" \
    -e "s/COLOR10/$color10/g" \
    -e "s/COLOR11/$color11/g" \
    -e "s/COLOR12/$color12/g" \
    -e "s/COLOR13/$color13/g" \
    -e "s/COLOR14/$color14/g" \
    -e "s/COLOR15/$color15/g" \
    /home/maren/dotfiles/xmonad/xmonad-template.hs > /home/maren/dotfiles/xmonad/xmonad.hs

killall xmobar
xmonad --recompile; xmonad --restart

```

And here is my xmobar config:

```haskell


Config { font    = "xft:Ubuntu:weight=bold:pixelsize=14:antialias=true:hinting=true"
       , additionalFonts = [ "xft:Mononoki Nerd Font:pixelsize=14:antialias=true:hinting=true"
                           , "xft:Mononoki Nerd Font:pixelsize=16:antialias=true:hinting=true"
                           , "xft:FontAwesome:pixelsize=13"
                           ]
       , bgColor = "BGCOLOR"
       , fgColor = "FGCOLOR"
       , position = Static { xpos = 0 , ypos = 0, width = 1920, height = 24 }
       , lowerOnStart = True
       , hideOnStart = False
       , allDesktops = True
       , persistent = True
       , commands = [
                    -- Time and date
                      Run Date "<fn=1>\xf133 </fn>  %b %d %Y - %H:%M " "date" 50
                      -- Network up and down

                    , Run Wireless "wlp3s0" [ "-t", "<essid>" ] 10
                    , Run Network "enp0s31f6" ["-t", "<fn=1>\xf0aa </fn>  <rx>kb  <fn=1>\xf0ab </fn>  <tx>kb"] 20
                      -- Cpu usage in percent
                    , Run Cpu ["-t", "<fn=1>\xf108 </fn>  cpu: (<total>%)","-H","50","--high","red"] 20
                      -- Ram used number and percent
                    , Run Memory ["-t", "<fn=1>\xf233 </fn>  mem: <used>M (<usedratio>%)"] 20
                      -- Disk space free
                    , Run DiskU [("/", "<fn=1>\xf0c7 </fn>  hdd: <free> free")] [] 60
                      -- Runs custom script to check for pacman updates.
                      -- This script is in my dotfiles repo in .local/bin.
                    -- watson is a time tracker tool, i wraped some calls
                    -- this shows eg. , " {taskname} started 2 hours ago / Friday 27 November 2020 (4h 40m 00s)" 
                    , Run Com "watson.sh" ["-l"] "watson" 60
                    , Run Com "watson.sh" ["-s"] "watson2" 60
                    , Run UnsafeStdinReader
                    ]
       , sepChar = "%"
       , alignSep = "}{"
       , template = " <action=`xdotool key control+alt+g`><icon=haskell_20.xpm/> </action><fc=FGCOLOR>  |</fc> %UnsafeStdinReader% }{ <fc=FGCOLOR><fn=2>|</fn> </fc><fc=COLOR3> %cpu% </fc><fc=FGCOLOR> <fn=2>|</fn></fc> <fc=COLOR3> %memory% </fc><fc=FGCOLOR> <fn=2>|</fn></fc> <fc=COLOR3> %disku% </fc><fc=FGCOLOR> <fn=2>|</fn></fc>  <fc=COLOR4> %enp0s31f6% </fc><fc=FGCOLOR> <fn=2>|</fn></fc> <fc=FGCOLOR> <fn=2>|</fn></fc> <fc=COLOR4> %wlp3s0wi% </fc><fc=FGCOLOR> <fn=2>|</fn></fc> <fc=COLOR1> %date% </fc><fc=FGCOLOR><fn=2>|</fn></fc> <fc=COLOR1>%watson2% / %watson%</fc> "
       }

```

My templates for xmonad are [here](https://github.com/mafflerbach/dotfiles/blob/master/xmonad/xmonad-template.hs)

It is very simple and is maybe the fastest approach.

Used tool:
* [yq](https://mikefarah.gitbook.io/yq/)