84 lines
2.7 KiB
Haskell
84 lines
2.7 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-|
|
|
Module : UI.Widgets
|
|
Description : This is the code which builds the Brick frontend
|
|
-}
|
|
|
|
module UI.Widgets where
|
|
|
|
import Brick
|
|
import Brick.Main
|
|
import Brick.Widgets.Center
|
|
import Brick.Widgets.Border
|
|
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 Data.Maybe
|
|
|
|
import JSONTypes
|
|
import Request
|
|
import Misc
|
|
import AppTypes
|
|
import Torrent
|
|
|
|
-- | Wrap a widget in the selected attribute
|
|
select :: Widget Ident -> Widget Ident
|
|
select = withAttr (attrName "selected")
|
|
|
|
-- | Given a movie and a tuple of widgets and state, append the
|
|
-- movie to the widgets. Used by movieWidgets
|
|
widgetCons :: JSONMovie -> (Widget Ident, AppS) -> (Widget Ident, AppS)
|
|
widgetCons m (w, s) =
|
|
embed $ if Just m == (s ^. appDetails)
|
|
then select . visible $
|
|
if s ^. appExpanded
|
|
then expandedWidget s m
|
|
else movieWidget s m
|
|
else movieWidget s m
|
|
where
|
|
embed x = (x <=> w, s)
|
|
|
|
-- | Returns a big list of all movies
|
|
movieWidgets :: AppS -> JSONListMovies -> Widget Ident
|
|
movieWidgets s m = let (items, _) = foldr widgetCons (emptyWidget, s) (movies m)
|
|
in items
|
|
|
|
-- | Returns a single movie listing
|
|
movieWidget :: AppS -> JSONMovie -> Widget Ident
|
|
movieWidget s m = txt (movieTitle m) <+> padLeft Max (str (show (movieYear m)))
|
|
|
|
-- | Returns an expanded movie listing showing additional info
|
|
expandedWidget :: AppS -> JSONMovie -> Widget Ident
|
|
expandedWidget s m = movieWidget s m
|
|
<=> (padRight (Pad 3) (str "Rating") <+> str (show (movieRating m)))
|
|
<=> (padRight (Pad 1) (str "Language") <+> txt (movieLanguage m))
|
|
<=> (padRight (Pad 3) (str "Genres") <+> txt (T.intercalate ", " (movieGenres m)))
|
|
<=> (padRight (Pad 2) (str "Magnets") <+> str (listTorrents m))
|
|
<=> (padRight (Pad 2) (str "Summary") <+> txtWrap (movieSummary m))
|
|
|
|
-- | The search mode widget (unimplemented)
|
|
searchWidget :: AppS -> Widget Ident
|
|
searchWidget = undefined
|
|
|
|
-- | The browse mode widget which returns a full listing of movies
|
|
-- or reports that there are no movies to list
|
|
browseWidget :: AppS -> Widget Ident
|
|
browseWidget s =
|
|
headings <=> case (s ^. appListing) of
|
|
Nothing -> center $ str "No movies found matching query."
|
|
(Just m) -> (viewport Listing Vertical (movieWidgets s m))
|
|
where
|
|
headings = str "Title" <+> padLeft Max (str "Year")
|
|
|
|
-- | The message widget which simply shows a message and informs
|
|
-- the user of how to escape
|
|
messageWidget :: AppS -> Widget Ident
|
|
messageWidget s = center $ border $
|
|
padAll 1 (hCenter (str (fromMaybe "Unkown Error!" (s ^. appMessage))))
|
|
<=> padAll 1 (hCenter (str "(Press any key to continue)"))
|