kino/src/Widgets.hs

65 lines
2.0 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Widgets where
import Brick
import Brick.Main
import Brick.Widgets.Center
import Brick.Widgets.Table
import Brick.AttrMap (attrMap)
import Graphics.Vty (defAttr)
import Graphics.Vty.Input.Events
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T
import Lens.Micro
import JSONTypes
import Request
import Misc
import AppTypes
select :: Widget Ident -> Widget Ident
select = (withAttr (attrName "selected"))
widgetCons :: JSONMovie -> (Widget Ident, Int, AppS) -> (Widget Ident, Int, AppS)
widgetCons m (w, i, s) =
embed $ if s ^. appCursor == i
then select . visible $
if s ^. appExpanded
then expandedWidget s m
else movieWidget s m
else movieWidget s m
where
embed x = (w <=> x, i+1, s)
movieWidgets :: AppS -> JSONListMovies -> Widget Ident
movieWidgets s m = let (items, _, _) = foldr widgetCons (emptyWidget, 0, s) (movies m)
in items
movieWidget :: AppS -> JSONMovie -> Widget Ident
movieWidget s m = txt (movie_title m) <+> padLeft Max (str (show (movie_year m)))
expandedWidget :: AppS -> JSONMovie -> Widget Ident
expandedWidget s m = movieWidget s m
<=> (padRight (Pad 3) (str "Rating") <+> str (show (movie_rating m)))
<=> (padRight (Pad 1) (str "Language") <+> txt (movie_language m))
<=> (padRight (Pad 3) (str "Genres") <+> txt (T.intercalate ", " (movie_genres m)))
<=> (padRight (Pad 2) (str "Summary") <+> txtWrap (movie_summary m))
searchWidget :: AppS -> Widget Ident
searchWidget = undefined
browseWidget :: AppS -> Widget Ident
browseWidget s =
case (s ^. appListing) of
Nothing -> center $ str "No movies found matching query."
(Just m) -> headings <=> (viewport Listing Vertical (movieWidgets s m))
where headings = str "Title" <+> padLeft Max (str "Year")
errorWidget :: AppS -> Widget Ident
errorWidget s = center $
case (s ^. appError) of
Nothing -> str "Unknown Error."
(Just e) -> str e