summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/Pango.hs
blob: 30ec76ff46a7751075e7186b06e17886c1c7a24b (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
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.Text.Pango
-- Copyright: (c) 2022 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Author: Pavel Kalugin
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: portable
-- Created: Fri Feb 4, 2022 01:15
--
--
-- Codification with Pango markup
--
------------------------------------------------------------------------------

module Xmobar.Text.Pango (withPangoColor, withPangoFont, withPangoMarkup) where

import Text.Printf (printf)
import Data.List (isPrefixOf)

replaceAll :: (Eq a) => a -> [a] -> [a] -> [a]
replaceAll c s = concatMap (\x -> if x == c then s else [x])

xmlEscape :: String -> String
xmlEscape s = replaceAll '"' """ $
              replaceAll '\'' "'" $
              replaceAll '<' "&lt;" $
              replaceAll '>' "&gt;" $
              replaceAll '&' "&amp;" s

withPangoColor :: (String, String) -> String -> String
withPangoColor (fg, bg) s =
  printf fmt (xmlEscape fg) (xmlEscape bg) (xmlEscape s)
  where fmt = "<span foreground=\"%s\" background=\"%s\">%s</span>"

withPangoFont :: String -> String -> String
withPangoFont font txt = printf fmt pfn (xmlEscape txt)
  where fmt = "<span font=\"%s\">%s</span>"
        pfn = if "xft:" `isPrefixOf` font then drop 4 font else font

withPangoMarkup :: String -> String -> String -> String -> String
withPangoMarkup fg bg font txt =
  printf fmt pfn (xmlEscape fg) (xmlEscape bg) (xmlEscape txt)
  where pfn = if isPrefixOf "xft:" font then drop 4 font else font
        fmt = "<span font=\"%s\" foreground=\"%s\" background=\"%s\">%s</span>"