| 1 |
-- | The all-important theming engine! |
| 2 |
-- |
| 3 |
-- Cf |
| 4 |
-- https://hackage.haskell.org/package/vty/docs/Graphics-Vty-Attributes.html |
| 5 |
-- http://hackage.haskell.org/package/brick/docs/Brick-AttrMap.html |
| 6 |
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Util.html |
| 7 |
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Widgets-Core.html#g:5 |
| 8 |
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Widgets-Border.html |
| 9 |
|
| 10 |
{-# LANGUAGE OverloadedStrings #-} |
| 11 |
|
| 12 |
module Hledger.UI.Theme ( |
| 13 |
defaultTheme |
| 14 |
,getTheme |
| 15 |
,themes |
| 16 |
,themeNames |
| 17 |
) |
| 18 |
where |
| 19 |
|
| 20 |
import Data.Map qualified as M |
| 21 |
import Data.Maybe |
| 22 |
import Graphics.Vty |
| 23 |
import Brick |
| 24 |
import Safe (headErr) |
| 25 |
|
| 26 |
defaultTheme :: AttrMap |
| 27 |
defaultTheme = fromMaybe (snd $ headErr themesList) $ getTheme "light" -- PARTIAL headErr succeeds because themesList is non-null |
| 28 |
-- the theme named here should exist; |
| 29 |
-- otherwise it will take the first one from the list, |
| 30 |
-- which must be non-empty. |
| 31 |
|
| 32 |
-- | Look up the named theme, if it exists. |
| 33 |
getTheme :: String -> Maybe AttrMap |
| 34 |
getTheme name = M.lookup name themes |
| 35 |
|
| 36 |
-- | A selection of named themes specifying terminal colours and styles. |
| 37 |
-- One of these is active at a time. |
| 38 |
-- |
| 39 |
-- A hledger-ui theme is a vty/brick AttrMap. Each theme specifies a |
| 40 |
-- default style (Attr), plus extra styles which are applied when |
| 41 |
-- their (hierarchical) name matches the widget rendering context. |
| 42 |
-- "More specific styles, if present, are used and only fall back to |
| 43 |
-- more general ones when the more specific ones are absent, but also |
| 44 |
-- these styles get merged, so that if a more specific style only |
| 45 |
-- provides the foreground color, its more general parent style can |
| 46 |
-- set the background color, too." |
| 47 |
-- For example: rendering a widget named "b" inside a widget named "a", |
| 48 |
-- - if a style named "a" <> "b" exists, it will be used. Anything it |
| 49 |
-- does not specify will be taken from a style named "a" if that |
| 50 |
-- exists, otherwise from the default style. |
| 51 |
-- - otherwise if a style named "a" exists, it will be used, and |
| 52 |
-- anything it does not specify will be taken from the default style. |
| 53 |
-- - otherwise (you guessed it) the default style is used. |
| 54 |
-- |
| 55 |
themes :: M.Map String AttrMap |
| 56 |
themes = M.fromList themesList |
| 57 |
|
| 58 |
themeNames :: [String] |
| 59 |
themeNames = map fst themesList |
| 60 |
|
| 61 |
(&) = withStyle |
| 62 |
active = fg brightWhite & bold |
| 63 |
selectbg = cyan |
| 64 |
select = black `on` selectbg |
| 65 |
|
| 66 |
themesList :: [(String, AttrMap)] |
| 67 |
themesList = [ |
| 68 |
("light", attrMap (black `on` white) [ |
| 69 |
(attrName "border" , white `on` black & dim) |
| 70 |
,(attrName "border" <> attrName "bold" , currentAttr & bold) |
| 71 |
,(attrName "border" <> attrName "depth" , active) |
| 72 |
,(attrName "border" <> attrName "filename" , currentAttr) |
| 73 |
,(attrName "border" <> attrName "key" , active) |
| 74 |
,(attrName "border" <> attrName "minibuffer" , white `on` black & bold) |
| 75 |
,(attrName "border" <> attrName "query" , active) |
| 76 |
,(attrName "border" <> attrName "selected" , active) |
| 77 |
,(attrName "error" , fg red) |
| 78 |
,(attrName "help" , white `on` black & dim) |
| 79 |
,(attrName "help" <> attrName "heading" , fg yellow) |
| 80 |
,(attrName "help" <> attrName "key" , active) |
| 81 |
-- ,(attrName "list" , black `on` white) |
| 82 |
-- ,(attrName "list" <> attrName "amount" , currentAttr) |
| 83 |
,(attrName "list" <> attrName "amount" <> attrName "decrease" , fg red) |
| 84 |
-- ,(attrName "list" <> attrName "amount" <> attrName "increase" , fg green) |
| 85 |
,(attrName "list" <> attrName "amount" <> attrName "decrease" <> attrName "selected" , red `on` selectbg & bold) |
| 86 |
-- ,(attrName "list" <> attrName "amount" <> attrName "increase" <> attrName "selected" , green `on` selectbg & bold) |
| 87 |
,(attrName "list" <> attrName "balance" , currentAttr & bold) |
| 88 |
,(attrName "list" <> attrName "balance" <> attrName "negative" , fg red) |
| 89 |
,(attrName "list" <> attrName "balance" <> attrName "positive" , fg black) |
| 90 |
,(attrName "list" <> attrName "balance" <> attrName "negative" <> attrName "selected" , red `on` selectbg & bold) |
| 91 |
,(attrName "list" <> attrName "balance" <> attrName "positive" <> attrName "selected" , select & bold) |
| 92 |
,(attrName "list" <> attrName "selected" , select) |
| 93 |
-- ,(attrName "list" <> attrName "accounts" , white `on` brightGreen) |
| 94 |
-- ,(attrName "list" <> attrName "selected" , black `on` brightYellow) |
| 95 |
]) |
| 96 |
|
| 97 |
,("dark", attrMap (white `on` black & dim) [ |
| 98 |
(attrName "border" , white `on` black) |
| 99 |
, (attrName "border" <> attrName "bold" , currentAttr & bold) |
| 100 |
, (attrName "border" <> attrName "depth" , active) |
| 101 |
, (attrName "border" <> attrName "filename" , currentAttr) |
| 102 |
, (attrName "border" <> attrName "key" , active) |
| 103 |
, (attrName "border" <> attrName "minibuffer" , white `on` black & bold) |
| 104 |
, (attrName "border" <> attrName "query" , active) |
| 105 |
, (attrName "border" <> attrName "selected" , active) |
| 106 |
, (attrName "error" , fg red) |
| 107 |
, (attrName "help" , currentAttr & bold) |
| 108 |
, (attrName "help" <> attrName "heading" , fg blue) |
| 109 |
, (attrName "help" <> attrName "key" , active) |
| 110 |
, (attrName "list" <> attrName "amount" <> attrName "decrease" , fg red) |
| 111 |
, (attrName "list" <> attrName "amount" <> attrName "decrease" <> attrName "selected" , red `on` black & bold) |
| 112 |
, (attrName "list" <> attrName "balance" , currentAttr) |
| 113 |
, (attrName "list" <> attrName "balance" <> attrName "negative" , fg red) |
| 114 |
, (attrName "list" <> attrName "balance" <> attrName "positive" , fg white) |
| 115 |
, (attrName "list" <> attrName "balance" <> attrName "negative" <> attrName "selected" , red `on` black & bold) |
| 116 |
, (attrName "list" <> attrName "balance" <> attrName "positive" <> attrName "selected" , cyan `on` black & bold) |
| 117 |
, (attrName "list" <> attrName "selected" , cyan `on` black & bold) |
| 118 |
]) |
| 119 |
|
| 120 |
,("terminal", attrMap defAttr [ |
| 121 |
(attrName "border" , white `on` black), |
| 122 |
(attrName "list" , defAttr), |
| 123 |
(attrName "list" <> attrName "selected" , defAttr & reverseVideo) |
| 124 |
]) |
| 125 |
|
| 126 |
,("greenterm", attrMap (green `on` black) [ |
| 127 |
(attrName "list" <> attrName "selected" , black `on` green) |
| 128 |
]) |
| 129 |
|
| 130 |
] |
| 131 |
|
| 132 |
-- halfbrightattr = defAttr & dim |
| 133 |
-- reverseattr = defAttr & reverseVideo |
| 134 |
-- redattr = defAttr `withForeColor` red |
| 135 |
-- greenattr = defAttr `withForeColor` green |
| 136 |
-- reverseredattr = defAttr & reverseVideo `withForeColor` red |
| 137 |
-- reversegreenattr= defAttr & reverseVideo `withForeColor` green |