summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/Output.hs
blob: 6aa1d5636ae2a511795fefed5774cc275c365810 (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
-- |
-- Module: Xmobar.Text.Output
-- 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 01:10
--
--
-- Format strings emitted by Commands into output strings
--
------------------------------------------------------------------------------

module Xmobar.Text.Output (initLoop, format) where

import Xmobar.Config.Types (Config(textOutputFormat), TextOutputFormat(..))
import Xmobar.Run.Parsers ( Segment
                          , Widget(..)
                          , parseString
                          , tColorsString
                          , colorComponents)

import Xmobar.Text.Ansi (withAnsiColor)
import Xmobar.Text.Pango (withPangoColor)
import Xmobar.Text.Swaybar (formatSwaybar, preamble)

initLoop :: Config -> IO ()
initLoop conf = case textOutputFormat conf of
  Swaybar -> putStrLn preamble
  _ -> return ()

withColor :: TextOutputFormat -> (String, String) -> String -> String
withColor Ansi c = withAnsiColor c
withColor Pango c = withPangoColor c
withColor _ _ = id

formatWithColor :: Config -> Segment -> String
formatWithColor conf (Text s, info, _, _) =
  withColor (textOutputFormat conf) components s
  where components = colorComponents conf color
        color = tColorsString info
formatWithColor conf (Hspace n, i, x, y) =
   formatWithColor conf (Text $ replicate (fromIntegral n) ' ', i, x, y)
formatWithColor _ _ = ""

format :: Config -> String -> IO String
format conf s = do
  segments <- parseString conf s
  case textOutputFormat conf of
    Swaybar -> return $ formatSwaybar conf segments
    _ -> return (concatMap (formatWithColor conf) segments)