diff --git a/qbar/src/QBar/BlockOutput.hs b/qbar/src/QBar/BlockOutput.hs index f1ece12691d21c79a2688c20a6ee84c9bc117ec3..0f330e4b5d5dbc33712947283a06d4a9463d84de 100644 --- a/qbar/src/QBar/BlockOutput.hs +++ b/qbar/src/QBar/BlockOutput.hs @@ -45,6 +45,7 @@ import QBar.Prelude import Control.Lens import Data.Aeson +import Data.Char import Data.Int (Int64) import Data.Text.Lazy qualified as T import GHC.Generics @@ -209,8 +210,31 @@ printedLength (BlockText b) = sum . map segmentLength $ b mkText :: Bool -> Importance -> T.Text -> BlockText mkText active importance segmentText = BlockText [BlockTextSegment { segmentText = pangoFriendly segmentText, active, importance }] where + toHex :: Int -> T.Text + toHex 1 = "1" + toHex 2 = "2" + toHex 3 = "3" + toHex 4 = "4" + toHex 5 = "5" + toHex 6 = "6" + toHex 7 = "7" + toHex 8 = "8" + toHex 9 = "9" + toHex 10 = "A" + toHex 11 = "B" + toHex 12 = "C" + toHex 13 = "D" + toHex 14 = "E" + toHex 15 = "F" + toHex x + | x <= 0 = "0" + | otherwise = toHex (div x 16) <> toHex (mod x 16) + pangoFriendlyIcon :: Char -> T.Text + pangoFriendlyIcon x + | isAlphaNum x || isAscii x = T.singleton x + | otherwise = "&#x" <> toHex (ord x) <> ";" pangoFriendly :: T.Text -> T.Text - pangoFriendly = T.replace "<" "<" . T.replace ">" ">" . T.replace "&" "&" + pangoFriendly = T.concatMap pangoFriendlyIcon . T.replace "<" "<" . T.replace ">" ">" . T.replace "&" "&" activeImportantText :: Importance -> T.Text -> BlockText activeImportantText = mkText True