module Xmobar.Text.Output (initLoop, format) where
import Xmobar.Config.Types ( Config (..)
, TextOutputFormat (..)
, Segment
, Widget (..)
, tColorsString)
import Xmobar.Config.Parse (colorComponents)
import Xmobar.Config.Template (parseString)
import Xmobar.Text.Ansi (withAnsiColor)
import Xmobar.Text.Pango (withPangoMarkup)
import Xmobar.Text.Swaybar (formatSwaybar, prepare)
initLoop :: Config -> IO ()
initLoop :: Config -> IO ()
initLoop Config
conf = case Config -> TextOutputFormat
textOutputFormat Config
conf of
TextOutputFormat
Swaybar -> IO ()
prepare
TextOutputFormat
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
formatWithColor :: Config -> Segment -> String
formatWithColor :: Config -> Segment -> String
formatWithColor Config
conf (Text String
s, TextRenderInfo
info, Int
idx, Maybe [Action]
_) =
case Config -> TextOutputFormat
textOutputFormat Config
conf of
TextOutputFormat
Ansi -> (String, String) -> String -> String
withAnsiColor (String
fg, String
bg) String
s
TextOutputFormat
Pango -> String -> String -> String -> String -> String
withPangoMarkup String
fg String
bg String
fn String
s
TextOutputFormat
_ -> String
s
where (String
fg, String
bg) = Config -> String -> (String, String)
colorComponents Config
conf (TextRenderInfo -> String
tColorsString TextRenderInfo
info)
fonts :: [String]
fonts = Config -> [String]
additionalFonts Config
conf
fn :: String
fn = if Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> [String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
fonts
then Config -> String
font Config
conf
else [String]
fonts [String] -> Int -> String
forall a. HasCallStack => [a] -> Int -> a
!! (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
formatWithColor Config
conf (Hspace Int32
n, TextRenderInfo
i, Int
x, Maybe [Action]
y) =
Config -> Segment -> String
formatWithColor Config
conf (String -> Widget
Text (String -> Widget) -> String -> Widget
forall a b. (a -> b) -> a -> b
$ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
n) Char
' ', TextRenderInfo
i, Int
x, Maybe [Action]
y)
formatWithColor Config
_ Segment
_ = String
""
format :: Config -> String -> String
format :: Config -> String -> String
format Config
conf String
s = do
let segments :: [Segment]
segments = Config -> String -> [Segment]
parseString Config
conf String
s
case Config -> TextOutputFormat
textOutputFormat Config
conf of
TextOutputFormat
Swaybar -> Config -> [Segment] -> String
formatSwaybar Config
conf [Segment]
segments
TextOutputFormat
_ -> (Segment -> String) -> [Segment] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Config -> Segment -> String
formatWithColor Config
conf) [Segment]
segments