summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/Swaybar.hs
blob: 69739edcf5192f25c2ddbc302497fd5b261707de (plain)
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
{-# LANGUAGE DeriveGeneric #-}

------------------------------------------------------------------------------
-- |
-- Module: Xmobar.Text.Swaybar
-- Copyright: (c) 2022 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: portable
-- Created: Fri Feb 4, 2022 03:58
--
--
-- Segment codification using swaybar-protocol JSON strings
--
------------------------------------------------------------------------------

module Xmobar.Text.Swaybar (prepare, formatSwaybar) where

import Data.Aeson

import Data.ByteString.Lazy.UTF8 (toString)

import GHC.Generics

import Xmobar.Config.Types (Config)

import Xmobar.Run.Parsers ( Segment
                          , Widget(..)
                          , tColorsString
                          , colorComponents)

import Xmobar.Text.SwaybarClicks (startHandler)

data Preamble =
  Preamble {version :: !Int, click_events :: Bool} deriving (Eq,Show,Generic)

asString :: ToJSON a => a -> String
asString = toString . encode

preamble :: String
preamble = (asString $ Preamble { version = 1, click_events = True }) ++ "\x0A["

data Block =
  Block { full_text :: !String
        , color :: !String
        , background :: !String
        , separator :: !Bool
        , separator_block_width :: !Int
        , name :: !String
        } deriving (Eq,Show,Generic)

defaultBlock :: Block
defaultBlock = Block { full_text = ""
                     , name = ""
                     , color = ""
                     , background = ""
                     , separator = False
                     , separator_block_width = 0}

instance ToJSON Preamble
instance ToJSON Block

formatSwaybar' :: Config -> Segment -> Block
formatSwaybar' conf (Text txt, info, _, as) =
  defaultBlock {full_text = txt , color = fg , background = bg , name = show as}
  where (fg, bg) = colorComponents conf (tColorsString info)
formatSwaybar' conf (Hspace n, info, i, a) =
  formatSwaybar' conf (Text (replicate (fromIntegral n) ' '), info, i, a)
formatSwaybar' _ _ = defaultBlock

formatSwaybar :: Config -> [Segment] -> String
formatSwaybar conf segs = asString elems ++ ","
  where elems = filter (not . null . full_text) (map (formatSwaybar' conf) segs)

prepare :: IO ()
prepare = startHandler >> putStrLn preamble