2015-08-18 03:44:18 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
module Hledger.UI.UIUtils (
|
|
|
|
pushScreen
|
|
|
|
,popScreen
|
|
|
|
,screenEnter
|
2015-10-28 20:17:15 +03:00
|
|
|
,reload
|
2015-08-21 18:40:05 +03:00
|
|
|
,getViewportSize
|
2015-08-25 02:22:09 +03:00
|
|
|
-- ,margin
|
2015-08-23 03:46:57 +03:00
|
|
|
,withBorderAttr
|
|
|
|
,topBottomBorderWithLabel
|
2015-08-28 08:45:49 +03:00
|
|
|
,topBottomBorderWithLabels
|
2015-08-23 03:46:57 +03:00
|
|
|
,defaultLayout
|
2015-08-28 08:45:49 +03:00
|
|
|
,borderQueryStr
|
|
|
|
,borderDepthStr
|
|
|
|
,borderKeysStr
|
2015-10-30 20:42:44 +03:00
|
|
|
--
|
|
|
|
,stToggleCleared
|
2015-08-20 14:54:23 +03:00
|
|
|
) where
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2016-04-05 03:44:29 +03:00
|
|
|
import Lens.Micro ((^.))
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Control.Monad
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Control.Monad.IO.Class
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Data.Default
|
2015-08-28 08:45:49 +03:00
|
|
|
import Data.List
|
2015-08-25 16:56:04 +03:00
|
|
|
import Data.Monoid
|
2015-08-18 03:44:18 +03:00
|
|
|
import Data.Time.Calendar (Day)
|
2015-08-20 14:54:23 +03:00
|
|
|
import Brick
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Brick.Widgets.List
|
|
|
|
import Brick.Widgets.Border
|
|
|
|
import Brick.Widgets.Border.Style
|
|
|
|
import Graphics.Vty as Vty
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
import Hledger.UI.UITypes
|
2015-10-28 20:17:15 +03:00
|
|
|
import Hledger.Data.Types (Journal)
|
2015-10-30 20:42:44 +03:00
|
|
|
import Hledger.UI.UIOptions
|
|
|
|
import Hledger.Cli.CliOptions
|
|
|
|
import Hledger.Reports.ReportOptions
|
2015-08-23 03:46:57 +03:00
|
|
|
import Hledger.Utils (applyN)
|
2015-10-28 20:17:15 +03:00
|
|
|
-- import Hledger.Utils.Debug
|
|
|
|
|
2015-10-30 20:42:44 +03:00
|
|
|
stToggleCleared :: AppState -> AppState
|
|
|
|
stToggleCleared st@AppState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} =
|
|
|
|
st{aopts=uopts{cliopts_=copts{reportopts_=toggleCleared ropts}}}
|
|
|
|
|
|
|
|
-- | Toggle between showing all and showing only cleared items.
|
|
|
|
toggleCleared :: ReportOpts -> ReportOpts
|
|
|
|
toggleCleared ropts = ropts{cleared_=not $ cleared_ ropts}
|
|
|
|
|
2015-10-28 20:17:15 +03:00
|
|
|
-- | Regenerate the content for the current and previous screens, from a new journal and current date.
|
|
|
|
reload :: Journal -> Day -> AppState -> AppState
|
|
|
|
reload j d st@AppState{aScreen=s,aPrevScreens=ss} =
|
2016-02-19 04:47:21 +03:00
|
|
|
-- XXX clumsy due to entanglement of AppState and Screen.
|
2015-10-28 20:17:15 +03:00
|
|
|
-- sInitFn operates only on an appstate's current screen, so
|
|
|
|
-- remove all the screens from the appstate and then add them back
|
|
|
|
-- one at a time, regenerating as we go.
|
|
|
|
let
|
|
|
|
first:rest = reverse $ s:ss
|
|
|
|
st0 = st{ajournal=j, aScreen=first, aPrevScreens=[]}
|
|
|
|
st1 = (sInitFn first) d st0
|
|
|
|
st2 = foldl' (\st s -> (sInitFn s) d $ pushScreen s st) st1 rest
|
|
|
|
in
|
|
|
|
st2
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
pushScreen :: Screen -> AppState -> AppState
|
|
|
|
pushScreen scr st = st{aPrevScreens=(aScreen st:aPrevScreens st)
|
|
|
|
,aScreen=scr
|
|
|
|
}
|
|
|
|
|
|
|
|
popScreen :: AppState -> AppState
|
|
|
|
popScreen st@AppState{aPrevScreens=s:ss} = st{aScreen=s, aPrevScreens=ss}
|
|
|
|
popScreen st = st
|
|
|
|
|
|
|
|
-- clearScreens :: AppState -> AppState
|
|
|
|
-- clearScreens st = st{aPrevScreens=[]}
|
|
|
|
|
|
|
|
-- | Enter a new screen, saving the old screen & state in the
|
|
|
|
-- navigation history and initialising the new screen's state.
|
2015-08-28 16:36:07 +03:00
|
|
|
screenEnter :: Day -> Screen -> AppState -> AppState
|
|
|
|
screenEnter d scr st = (sInitFn scr) d $
|
|
|
|
pushScreen scr
|
|
|
|
st
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2015-08-21 18:40:05 +03:00
|
|
|
-- | In the EventM monad, get the named current viewport's width and height,
|
|
|
|
-- or (0,0) if the named viewport is not found.
|
|
|
|
getViewportSize :: Name -> EventM (Int,Int)
|
|
|
|
getViewportSize name = do
|
|
|
|
mvp <- lookupViewport name
|
|
|
|
let (w,h) = case mvp of
|
|
|
|
Just vp -> vp ^. vpSize
|
|
|
|
Nothing -> (0,0)
|
2015-08-23 03:46:57 +03:00
|
|
|
-- liftIO $ putStrLn $ show (w,h)
|
2015-08-21 18:40:05 +03:00
|
|
|
return (w,h)
|
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
defaultLayout toplabel bottomlabel =
|
|
|
|
topBottomBorderWithLabels (str " "<+>toplabel<+>str " ") (str " "<+>bottomlabel<+>str " ") .
|
2015-08-23 03:46:57 +03:00
|
|
|
margin 1 0 Nothing
|
2015-08-25 02:22:09 +03:00
|
|
|
-- topBottomBorderWithLabel2 label .
|
|
|
|
-- padLeftRight 1 -- XXX should reduce inner widget's width by 2, but doesn't
|
|
|
|
-- "the layout adjusts... if you use the core combinators"
|
2015-08-23 03:46:57 +03:00
|
|
|
|
|
|
|
topBottomBorderWithLabel label = \wrapped ->
|
|
|
|
Widget Greedy Greedy $ do
|
|
|
|
c <- getContext
|
|
|
|
let (_w,h) = (c^.availWidthL, c^.availHeightL)
|
|
|
|
h' = h - 2
|
|
|
|
wrapped' = vLimit (h') wrapped
|
|
|
|
debugmsg =
|
|
|
|
""
|
|
|
|
-- " debug: "++show (_w,h')
|
|
|
|
render $
|
|
|
|
hBorderWithLabel (label <+> str debugmsg)
|
|
|
|
<=>
|
|
|
|
wrapped'
|
|
|
|
<=>
|
|
|
|
hBorder
|
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
topBottomBorderWithLabels toplabel bottomlabel = \wrapped ->
|
|
|
|
Widget Greedy Greedy $ do
|
|
|
|
c <- getContext
|
|
|
|
let (_w,h) = (c^.availWidthL, c^.availHeightL)
|
|
|
|
h' = h - 2
|
|
|
|
wrapped' = vLimit (h') wrapped
|
|
|
|
debugmsg =
|
|
|
|
""
|
|
|
|
-- " debug: "++show (_w,h')
|
|
|
|
render $
|
|
|
|
hBorderWithLabel (toplabel <+> str debugmsg)
|
|
|
|
<=>
|
|
|
|
wrapped'
|
|
|
|
<=>
|
|
|
|
hBorderWithLabel bottomlabel
|
|
|
|
|
2015-08-25 02:22:09 +03:00
|
|
|
-- XXX should be equivalent to the above, but isn't (page down goes offscreen)
|
|
|
|
_topBottomBorderWithLabel2 label = \wrapped ->
|
|
|
|
let debugmsg = ""
|
|
|
|
in hBorderWithLabel (label <+> str debugmsg)
|
|
|
|
<=>
|
|
|
|
wrapped
|
|
|
|
<=>
|
|
|
|
hBorder
|
|
|
|
|
|
|
|
-- XXX superseded by pad, in theory
|
2015-08-23 03:46:57 +03:00
|
|
|
-- | Wrap a widget in a margin with the given horizontal and vertical
|
|
|
|
-- thickness, using the current background colour or the specified
|
2015-08-25 02:22:09 +03:00
|
|
|
-- colour.
|
|
|
|
-- XXX May disrupt border style of inner widgets.
|
|
|
|
-- XXX Should reduce the available size visible to inner widget, but doesn't seem to (cf drawRegisterScreen2).
|
2015-08-23 03:46:57 +03:00
|
|
|
margin :: Int -> Int -> Maybe Color -> Widget -> Widget
|
|
|
|
margin h v mcolour = \w ->
|
|
|
|
Widget Greedy Greedy $ do
|
|
|
|
c <- getContext
|
|
|
|
let w' = vLimit (c^.availHeightL - v*2) $ hLimit (c^.availWidthL - h*2) w
|
|
|
|
attr = maybe currentAttr (\c -> c `on` c) mcolour
|
|
|
|
render $
|
|
|
|
withBorderAttr attr $
|
|
|
|
withBorderStyle (borderStyleFromChar ' ') $
|
|
|
|
applyN v (hBorder <=>) $
|
|
|
|
applyN h (vBorder <+>) $
|
|
|
|
applyN v (<=> hBorder) $
|
|
|
|
applyN h (<+> vBorder) $
|
|
|
|
w'
|
|
|
|
|
|
|
|
-- withBorderAttr attr .
|
|
|
|
-- withBorderStyle (borderStyleFromChar ' ') .
|
|
|
|
-- applyN n border
|
|
|
|
|
|
|
|
withBorderAttr attr = updateAttrMap (applyAttrMappings [(borderAttr, attr)])
|
|
|
|
|
|
|
|
-- _ui = vCenter $ vBox [ hCenter box
|
|
|
|
-- , str " "
|
|
|
|
-- , hCenter $ str "Press Esc to exit."
|
|
|
|
-- ]
|
2015-08-25 16:56:04 +03:00
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
borderQueryStr :: String -> Widget
|
|
|
|
borderQueryStr "" = str ""
|
|
|
|
borderQueryStr qry = str " matching " <+> withAttr (borderAttr <> "query") (str qry)
|
|
|
|
|
|
|
|
borderDepthStr :: Maybe Int -> Widget
|
|
|
|
borderDepthStr Nothing = str ""
|
|
|
|
borderDepthStr (Just d) = str " to " <+> withAttr (borderAttr <> "depth") (str $ "depth "++show d)
|
|
|
|
|
2015-09-04 07:03:03 +03:00
|
|
|
borderKeysStr :: [(String,String)] -> Widget
|
|
|
|
borderKeysStr keydescs =
|
|
|
|
hBox $
|
|
|
|
intersperse sep $
|
2015-10-30 20:42:44 +03:00
|
|
|
[withAttr (borderAttr <> "keys") (str keys) <+> str ":" <+> str desc | (keys, desc) <- keydescs]
|
2015-08-28 08:45:49 +03:00
|
|
|
where
|
2015-10-30 20:42:44 +03:00
|
|
|
-- sep = str " | "
|
|
|
|
sep = str " "
|
|
|
|
|