{-# LANGUAGE CPP #-}
module Test.Sandwich.Formatters.TerminalUI.Draw.Util where
import Brick
import Graphics.Vty.Image
import Lens.Micro
fixedHeightOrViewportPercent :: (Ord n, Show n) => n -> Int -> Widget n -> Widget n
fixedHeightOrViewportPercent :: forall n. (Ord n, Show n) => n -> Int -> Widget n -> Widget n
fixedHeightOrViewportPercent n
vpName Int
maxHeightPercent Widget n
w =
Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ do
Result n
result <- Widget n -> RenderM n (Result n)
forall n. Widget n -> RenderM n (Result n)
render Widget n
w
Context n
ctx <- RenderM n (Context n)
forall n. RenderM n (Context n)
getContext
#if MIN_VERSION_brick(0,56,0)
let usableHeight :: Int
usableHeight = Context n
ctx Context n -> Getting Int (Context n) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (Context n) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
windowHeightL
#else
let usableHeight = min 80 (ctx ^. availHeightL)
#endif
let maxHeight :: Int
maxHeight = Rational -> Int
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Int -> Rational
forall a. Real a => a -> Rational
toRational Int
usableHeight Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* (Int -> Rational
forall a. Real a => a -> Rational
toRational Int
maxHeightPercent Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
100))
if Image -> Int
imageHeight (Result n -> Image
forall n. Result n -> Image
image Result n
result) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxHeight
then Result n -> RenderM n (Result n)
forall a. a -> ReaderT (Context n) (State (RenderState n)) a
forall (m :: * -> *) a. Monad m => a -> m a
return Result n
result
else Widget n -> RenderM n (Result n)
forall n. Widget n -> RenderM n (Result n)
render (Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
maxHeight (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
n -> ViewportType -> Widget n -> Widget n
forall n.
(Ord n, Show n) =>
n -> ViewportType -> Widget n -> Widget n
viewport n
vpName ViewportType
Vertical (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> RenderM n (Result n) -> Widget n
forall a b. (a -> b) -> a -> b
$ Result n -> RenderM n (Result n)
forall a. a -> ReaderT (Context n) (State (RenderState n)) a
forall (m :: * -> *) a. Monad m => a -> m a
return Result n
result)