summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2022-02-04 22:53:10 +0000
committerjao <jao@gnu.org>2022-02-06 00:24:10 +0000
commite020b92e7ace80867a93873ab5b7d40e1ae3ddfe (patch)
treec191eaca1badba8e7ccfc01ec2afe166c20ebc75 /src/Xmobar
parent6e124f046b5b92dd3cdfc4a4ea337e3aca4c025d (diff)
downloadxmobar-e020b92e7ace80867a93873ab5b7d40e1ae3ddfe.tar.gz
xmobar-e020b92e7ace80867a93873ab5b7d40e1ae3ddfe.tar.bz2
swaybar-protocol: output with colors and actions
Diffstat (limited to 'src/Xmobar')
-rw-r--r--src/Xmobar/Text/Swaybar.hs54
1 files changed, 44 insertions, 10 deletions
diff --git a/src/Xmobar/Text/Swaybar.hs b/src/Xmobar/Text/Swaybar.hs
index a4ee306..0a7b8af 100644
--- a/src/Xmobar/Text/Swaybar.hs
+++ b/src/Xmobar/Text/Swaybar.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE DeriveGeneric #-}
+
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.Text.Swaybar
@@ -16,24 +18,56 @@
module Xmobar.Text.Swaybar (preamble, formatSwaybar) where
-import Data.List (intercalate)
+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
+ , tColorsString
+ , colorComponents
)
+data Preamble =
+ Preamble {version :: !Int, click_events :: Bool} deriving (Eq,Show,Generic)
+
+asString :: ToJSON a => a -> String
+asString = toString . encode
+
preamble :: String
-preamble = "{\"version\": 1, \"click_events\": true}\x0A["
+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 -> String
-formatSwaybar' _conf (Text txt, _, _, _) =
- "{\"full_text\":\"" ++ txt ++ "\"}"
-formatSwaybar' _ _ = ""
+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 =
- "[" ++ intercalate "," (map (formatSwaybar' conf) segs) ++ "],"
+formatSwaybar conf segs = asString elems ++ ","
+ where elems = filter (not . null . full_text) (map (formatSwaybar' conf) segs)