nix-config/modules/home-manager/desktop-xmonad/xmonad/lib/Polybar.hs

61 lines
1.9 KiB
Haskell
Raw Normal View History

2022-01-18 09:32:55 +01:00
module Polybar (polybarLogHook, mkDbusClient) where
import XMonad.Hooks.DynamicLog
import Text.Printf (printf)
import XMonad.Layout.IndependentScreens (marshallPP)
import qualified DBus as D
import qualified DBus.Client as D
import qualified Codec.Binary.UTF8.String as UTF8
mkDbusClient :: IO D.Client
mkDbusClient = do
dbus <- D.connectSession
D.requestName dbus (D.busName_ "org.xmonad.log") opts
return dbus
where
opts = [D.nameAllowReplacement, D.nameReplaceExisting, D.nameDoNotQueue]
monitorMsg :: Int -> String -> String
monitorMsg = printf "{\"%d\": \"%s\"}"
-- Emit a DBus signal on log updates
dbusOutput :: D.Client -> String -> IO ()
dbusOutput dbus str =
let opath = D.objectPath_ "/org/xmonad/Log"
iname = D.interfaceName_ "org.xmonad.Log"
mname = D.memberName_ "Update"
signal = D.signal opath iname mname
body = [D.toVariant $ UTF8.decodeString $ monitorMsg 0 str]
in D.emit dbus $ signal { D.signalBody = body }
polybarFmt :: String -> String -> String -> String
polybarFmt var color elem = "%{" ++ var ++ color ++ "}" ++ elem ++ "%{" ++ var ++ "-}"
polybarFg :: String -> String -> String
polybarFg = polybarFmt "F"
polybarBg :: String -> String -> String
polybarBg = polybarFmt "B"
polybarBgFg :: String -> String -> String -> String
polybarBgFg bg fg = polybarBg bg . polybarFg fg
polybarHook :: D.Client -> PP
polybarHook dbus = def
{ ppOutput = dbusOutput dbus
, ppCurrent = polybarBgFg "#458588" "#fbf1c7"
, ppVisible = polybarFg "#ebdbb2"
, ppUrgent = polybarBgFg "#cc241d" "#fbf1c7"
, ppHidden = polybarFg "#ebdbb2"
, ppLayout = const ""
, ppHiddenNoWindows = const ""
, ppTitle = shorten 100 . polybarFg "#fbf1c7"
, ppSep = polybarFg "#a89974" " | "
}
polybarLogHook dbus = dynamicLogWithPP $ (marshallPP 0 . polybarHook) dbus