hledger/hledger-ui/Hledger/UI/Theme.hs

118 lines
4.9 KiB
Haskell
Raw Normal View History

-- | The all-important theming engine!
--
-- Cf
-- https://hackage.haskell.org/package/vty/docs/Graphics-Vty-Attributes.html
-- http://hackage.haskell.org/package/brick/docs/Brick-AttrMap.html
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Util.html
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Widgets-Core.html#g:5
-- http://hackage.haskell.org/package/brick-0.1/docs/Brick-Widgets-Border.html
{-# LANGUAGE CPP #-}
2018-10-23 16:33:21 +03:00
{-# LANGUAGE OverloadedStrings #-}
module Hledger.UI.Theme (
defaultTheme
,getTheme
,themes
,themeNames
2018-10-23 16:33:21 +03:00
)
where
import qualified Data.Map as M
import Data.Maybe
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import Graphics.Vty
import Brick
defaultTheme :: AttrMap
defaultTheme = fromMaybe (snd $ head themesList) $ getTheme "white"
-- the theme named here should exist;
-- otherwise it will take the first one from the list,
-- which must be non-empty.
-- | Look up the named theme, if it exists.
getTheme :: String -> Maybe AttrMap
getTheme name = M.lookup name themes
-- | A selection of named themes specifying terminal colours and styles.
-- One of these is active at a time.
--
-- A hledger-ui theme is a vty/brick AttrMap. Each theme specifies a
-- default style (Attr), plus extra styles which are applied when
-- their (hierarchical) name matches the widget rendering context.
-- "More specific styles, if present, are used and only fall back to
-- more general ones when the more specific ones are absent, but also
-- these styles get merged, so that if a more specific style only
-- provides the foreground color, its more general parent style can
-- set the background color, too."
-- For example: rendering a widget named "b" inside a widget named "a",
-- - if a style named "a" <> "b" exists, it will be used. Anything it
-- does not specify will be taken from a style named "a" if that
-- exists, otherwise from the default style.
-- - otherwise if a style named "a" exists, it will be used, and
-- anything it does not specify will be taken from the default style.
-- - otherwise (you guessed it) the default style is used.
--
themes :: M.Map String AttrMap
themes = M.fromList themesList
themeNames :: [String]
themeNames = map fst themesList
(&) = withStyle
active = fg brightWhite & bold
selectbg = yellow
select = black `on` selectbg
2018-10-23 16:33:21 +03:00
themesList :: [(String, AttrMap)]
themesList = [
2018-10-23 16:33:21 +03:00
("default", attrMap (black `on` white) [
("border" , white `on` black & dim)
,("border" <> "bold" , currentAttr & bold)
,("border" <> "depth" , active)
,("border" <> "filename" , currentAttr)
,("border" <> "key" , active)
2018-10-23 16:33:21 +03:00
,("border" <> "minibuffer" , white `on` black & bold)
,("border" <> "query" , active)
,("border" <> "selected" , active)
2018-10-23 16:33:21 +03:00
,("error" , fg red)
,("help" , white `on` black & dim)
,("help" <> "heading" , fg yellow)
,("help" <> "key" , active)
-- ,("list" , black `on` white)
-- ,("list" <> "amount" , currentAttr)
2018-10-23 16:33:21 +03:00
,("list" <> "amount" <> "decrease" , fg red)
-- ,("list" <> "amount" <> "increase" , fg green)
,("list" <> "amount" <> "decrease" <> "selected" , red `on` selectbg & bold)
-- ,("list" <> "amount" <> "increase" <> "selected" , green `on` selectbg & bold)
,("list" <> "balance" , currentAttr & bold)
2018-10-23 16:33:21 +03:00
,("list" <> "balance" <> "negative" , fg red)
,("list" <> "balance" <> "positive" , fg black)
,("list" <> "balance" <> "negative" <> "selected" , red `on` selectbg & bold)
,("list" <> "balance" <> "positive" <> "selected" , select & bold)
,("list" <> "selected" , select)
2018-10-23 16:33:21 +03:00
-- ,("list" <> "accounts" , white `on` brightGreen)
-- ,("list" <> "selected" , black `on` brightYellow)
])
,("greenterm", attrMap (green `on` black) [
("list" <> "selected" , black `on` green)
2018-10-23 16:33:21 +03:00
])
,("terminal", attrMap defAttr [
("border" , white `on` black),
("list" , defAttr),
("list" <> "selected" , defAttr & reverseVideo)
2018-10-23 16:33:21 +03:00
])
]
-- halfbrightattr = defAttr & dim
-- reverseattr = defAttr & reverseVideo
-- redattr = defAttr `withForeColor` red
-- greenattr = defAttr `withForeColor` green
-- reverseredattr = defAttr & reverseVideo `withForeColor` red
-- reversegreenattr= defAttr & reverseVideo `withForeColor` green