aboutsummaryrefslogtreecommitdiff
path: root/src/Internal/XMobarLog.hs
blob: c0aa2a74fd1194b47ad62f8079c7076dcf3d3bf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module Internal.XMobarLog ( XMobarLog, spawnXMobar, xMobarLogHook ) where

import Control.Arrow (second)
import Control.Monad (forM_)
import Control.Monad.Writer (tell, execWriter)
import Data.List (sortBy)
import Data.Maybe (mapMaybe)
import Data.Ord (comparing)
import Internal.LayoutDraw (drawLayout)
import System.IO (Handle, hSetEncoding, hPutStrLn, utf8)
import XMonad.Util.NamedWindows (getName)
import XMonad.Util.Run (spawnPipe)
import XMonad (X)
import Internal.Lib (getPopulatedWorkspaces, WorkspaceState(..))

import qualified XMonad as X
import qualified XMonad.StackSet as S

data XMobarLog = XMobarLog Handle

-- The log hook for XMobar. This is a custom log hook that does not use any
-- of the Xmonad dynamic log libraries.
--
-- This is because the given dynamic log libraries don't handle unicode properly
-- and this has been causing issues. It is also more flexible and frankly easier
-- to just DIY.

spawnXMobar :: IO XMobarLog
spawnXMobar = do
  pipe <- spawnPipe "xmobar"
  hSetEncoding pipe utf8
  return (XMobarLog pipe)


-- XMonad Log Hook meant to be used with the XMonad config logHook.
xMobarLogHook :: XMobarLog -> X ()
xMobarLogHook (XMobarLog xmproc) = do
  (_, _, layoutXpm) <- drawLayout

  winset <- X.gets X.windowset
  title <- maybe (pure "") (fmap show . getName) . S.peek $ winset
  let wss = getPopulatedWorkspaces winset

  X.liftIO $ do
    hPutStrLn xmproc $ trunc 80 $ execWriter $ do
      tell layoutXpm
      tell $ "<fc=#404040> │ </fc>"

      forM_ wss $ \(t, ws) -> do
        case t of
          Current -> tell "<fn=1><fc=#ff8888>"
          Visible -> tell "<fn=6><fc=#8888ff>"
          Hidden -> tell "<fn=2><fc=#888888>"
        tell (S.tag ws)
        tell " </fc></fn>"

      tell $ "<fc=#404040>│ </fc><fc=#a0a0a0><fn=3>"
      tell $ title
      tell $ "</fn></fc>"

-- Truncate an XMobar string to the provided number of _visible_ characters.
-- This is to keep long window titles from overrunning the whole bar.
trunc :: Int -> String -> String
trunc amt str = reverse $ trunc' False amt str []
  where
    trunc' _ _ [] acc = acc
    trunc' ignore amt (a:as) acc =
      case a of
        '<' -> trunc' True amt as (a : acc)
        '>' -> trunc' False amt as (a : acc)
        _ ->
          if ignore
            then trunc' True amt as (a : acc)
            else
              case amt of
                0 -> trunc' False 0 as acc
                3 -> trunc' False 0 as ("..." ++ acc)
                _ -> trunc' False (amt - 1) as (a : acc)